链表

一、单链表

 

//一个节点
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;
			//如果下一个节点为null,当前节点已经是最后一个节点
			if(nextNode == null) {
				break;
			}
			//赋给当前节点
			currentNode = nextNode;
		}
		//把需要追回的节点追加为找到的当前节点的下一个节点
		currentNode.next = node;
		return this;  //前面设置返回值为Node
	}
	
	//显示所有节点信息
	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 insert(Node node) {
		
		//取出下一个节点,作为下下个节点
		Node nextNode = next;
		this.next = node;
		node.next = nextNode;
	}
	
	//删除下一个节点
	public void removeNext() {
		//取出下下个节点
		Node newNext = next.next;
		//把下下个节点设置为当前节点的下一个节点
		this.next = newNext;
	}
	
	//获取下一个节点
	public Node next() {
		return this.next;
	}
	
	public int getData() {
		return data;
	}
	
	//判断是否为最后一个节点
	public boolean isLast() {
		return next==null;
	}
}
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);
//		n1.append(n3);
		
		//若前面如此写return this;  //前面设置返回值为Node  则可以连串写追加
		n1.append(n2).append(n3).append(new Node(4));
		
		//取出下一个节点的数据
		//System.out.println(n1.next().next().getData());
		
		
		
//		System.out.println(n1.next().next().next().getData());
//		System.out.println(n1.isLast());
		
		n1.show();
		//若要删除3  则
		//n1.next().removeNext();
//		n1.removeNext();
//		n1.show();
		
		Node node = new Node(9);
		n1.next().insert(node);
		n1.show();
	}
}

二、循环链表

 

//一个节点
public class Node {
	//节点内容
	int data;
	//下一个节点
	Node next = this;   //与单链表的区别: 尾节点,指向头节点     如果只有两个元素,互相指向
	
	public Node(int data) {
		this.data = data;
	}
	
	//插入节点
	public void insert(Node node) {
		
		//取出下一个节点,作为下下个节点
		Node nextNode = next;
		this.next = node;
		node.next = nextNode;
	}
	
	//删除下一个节点
	public void removeNext() {
		//取出下下个节点
		Node newNext = next.next;
		//把下下个节点设置为当前节点的下一个节点
		this.next = newNext;
	}
	
	//获取下一个节点
	public Node next() {
		return this.next;
	}
	
	public int getData() {
		return data;
	}
}
public class TestNode {
	public static void main(String[] args) {
		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		
		//增加节点
		n1.insert(n2);
		n2.insert(n3);
		n3.insert(n4);
		
		System.out.println(n1.next().getData());
		System.out.println(n2.next().getData());
		System.out.println(n3.next().getData());
		System.out.println(n4.next().getData());
		
	}
}

 

三、双向循环链表

 

//一个节点
public class Node {
	//上一个节点
	Node pre = this;
	//下一个节点
	Node next = this;
	//节点数据
	int data;
	
	public Node(int data) {
		this.data = data;
	}
	
	//新增节点
	public void after(Node node) {
		//原来的下一个节点
		Node nextNext = next;
		//将新节点作为当前节点的下一个节点
		this.next = node;
		node.pre = this;
		node.next = nextNext;
		nextNext.pre = node;
	}
	
	//获取下一个节点
	public Node next() {
		return this.next;
	}
	
	//获取上一个节点
	public Node pre() {
		return this.pre;
	}
	
	//获取数据
	public int getData() {
		return this.data;
	}
}
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.after(n2);
		n2.after(n3);
		
		System.out.println(n2.pre().getData());
		System.out.println(n2.getData());
		System.out.println(n2.next().getData());
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值