public class LoopNode {
int data; //节点内容
LoopNode next = this; //下一个节点 -- 只有一个节点 这个节点的下一个是自己
public LoopNode(int value) {
this.data = value;
}
//获取下一个节点
public LoopNode next() {
return this.next;
}
//获取节点的数据
public int getData() {
return this.data;
}
//删除下一个节点
public void removeNext() {
LoopNode newNext = next.next;
this.next = newNext;
}
//追加节点 -- 在这之前每一个节点都是指向自己单一的节点
public void insert(LoopNode node) {
LoopNode nextNext = next;
this.next = node;
node.next = nextNext;
}
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.insert(n2);
n2.insert(n3);
n3.insert(n4);
System.out.println(n4.next.getData());
}
}
java实现循环链表
最新推荐文章于 2024-01-25 07:45:00 发布