Java数据结构_5.链表(单链表、循环链表、双向循环链表)

一、单链表

在这里插入图片描述

代码实现

package com.xianxing.lianbiao;

//一个节点
public class Node {
	// 节点内容
	int data;

	// 下一个节点
	Node next;

	public Node(int data) {
		this.data = data;
	}

	// 为节点追加节点
	public Node append(Node node) {
		// 当前节点
		Node currentNode = this;
		while (true) {
			Node nextNode = currentNode.next;
			if (nextNode == null) {
				break;
			}
			currentNode = nextNode;
		}
		// 把需要追加的节点追加为找到的当前节点的下一节点
		currentNode.next = node;
		return this;
	}

	// 获取下一个节点
	public Node next() {
		return this.next;
	}

	// 显示所有节点信息
	public void show() {
		Node currentNode = this;
		while (true) {
			System.out.print(currentNode.data + " ");
			currentNode = currentNode.next;
			if (currentNode == null) {
				break;
			}
		}
		System.out.println();
	}

	// 插入一个节点作为当前节点的下一节点
	public void after(Node node) {
		// 取出下一节点,作为下下节点
		Node nextNext = next;
		// 把新节点作为当前节点的下一节点
		this.next = node;
		// 把下下节点设置为新节点的下一节点
		node.next = nextNext;
	}

	// 删除下一节点
	public void removeNext() {
		// 取出下下节点
		Node newNext = next.next;
		// 将下下节点设置为当前节点的下一节点
		this.next = newNext;
	}

	// 获取节点中的数据
	public int getData() {
		return this.data;
	}

	// 判断当前节点是否是最后节点
	public boolean isLast() {
		return this.next == null;
	}

}

package com.xianxing.lianbiao;

public class TestNode {

	public static void main(String[] args) {
		// 创建节点
		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		// 追加节点
		n1.append(n2).append(n3).append(new Node(4));
		// 取出下一个节点
		System.out.println(n1.next().getData());// 2
		System.out.println(n1.next().next().getData());// 3
		// 判断当前节点是否为空
		System.out.println(n1.next().isLast());// false
		System.out.println(n1.next().next().next().isLast());// true

		// 显示所有节点
		n1.show();// 1 2 3 4

		// 删除下一节点
		n1.next().removeNext();
		n1.show();// 1 2 4

		// 插入一个节点
		Node node = new Node(3);
		n1.next().after(node);
		n1.show();
	}

}

二、循环链表

在这里插入图片描述
代码实现

package com.xianxing.xunhuanlianbiao;

//一个节点
public class LoopNode {
	// 节点内容
	int data;

	// 下一个节点
	LoopNode next = this;

	public LoopNode(int data) {
		this.data = data;
	}

	// 获取下一个节点
	public LoopNode next() {
		return this.next;
	}

	// 插入一个节点作为当前节点的下一节点
	public void after(LoopNode node) {
		// 取出下一节点,作为下下节点
		LoopNode nextNext = next;
		// 把新节点作为当前节点的下一节点
		this.next = node;
		// 把下下节点设置为新节点的下一节点
		node.next = nextNext;
	}

	// 删除下一节点
	public void removeNext() {
		// 取出下下节点
		LoopNode newNext = next.next;
		// 将下下节点设置为当前节点的下一节点
		this.next = newNext;
	}

	// 获取节点中的数据
	public int getData() {
		return this.data;
	}

}

package com.xianxing.xunhuanlianbiao;

public class TestLoopNode {

	public static void main(String[] args) {
		LoopNode n1 = new LoopNode(1);
		LoopNode n2 = new LoopNode(2);
		LoopNode n3 = new LoopNode(3);
		LoopNode n4 = new LoopNode(4);
		// 增加节点
		n1.after(n2);
		n2.after(n3);
		n3.after(n4);
		System.out.println(n1.next().getData());// 2
		System.out.println(n2.next().getData());// 3
		System.out.println(n3.next().getData());// 4
		System.out.println(n4.next().getData());// 1
	}

}

三、双向循环链表

在这里插入图片描述
代码实现

package com.xianxing.shuangxianglianbiao;

/**
 * 双向循环链表
 * 
 * @author l1
 *
 */
public class DoubleNode {
	// 上一个节点
	DoubleNode pre = this;
	// 下一个节点
	DoubleNode next = this;
	// 节点数据
	int data;

	public DoubleNode(int data) {
		this.data = data;
	}

	// 增加节点
	public void after(DoubleNode node) {
		DoubleNode nextNext = next;
		// 当前节点和新节点的指向,当前节点下一节点指向新节点,新节点上一节点指向当前节点
		this.next = node;
		node.pre = this;
		// 新节点和下一节点的指向
		node.next = nextNext;
		nextNext.pre = node;
	}

	// 下一节点
	public DoubleNode next() {
		return this.next;
	}

	// 上一节点
	public DoubleNode pre() {
		return this.pre;
	}

	public int getData() {
		return this.data;
	}
}

package com.xianxing.shuangxianglianbiao;

public class TestDoubleNode {

	public static void main(String[] args) {
		DoubleNode n1 = new DoubleNode(1);
		DoubleNode n2 = new DoubleNode(2);
		DoubleNode n3 = new DoubleNode(3);

		// 查看n1、上一节点和上一节点的内容(n1与其他还未产生关系)
		System.out.println(n1.data);// 1
		System.out.println(n1.pre().getData());// 1
		System.out.println(n1.next().getData());// 1

		// 追加节点
		n1.after(n2);
		n2.after(n3);
		System.out.println(n2.pre().getData());// 1
		System.out.println(n2.data);// 2
		System.out.println(n2.next().getData());// 3
		System.out.println(n3.next().getData());// 1
		System.out.println(n1.pre().getData());// 3

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值