class DoubleLoopNode {
// 上一个节点
DoubleLoopNode pre;
// 下一个节点
DoubleLoopNode next;
// 节点的内容
int data;
public DoubleLoopNode(int value) {
this.pre = this;
this.next = this;
this.data = value;
}
// 插入节点
public void append(DoubleLoopNode node) {
// 原来的下一个节点
DoubleLoopNode nextNode = this.next;
// 把新节点作为当前节点的下一个节点
this.next = node;
// 把当前节点作为新节点的前一个节点
node.pre = this;
// 让原来的下一个节点作为新节点的下一个节点
node.next = nextNode;
// 让原来的下一个节点的上一个节点为新节点
nextNode.pre = node;
}
// 获取下一个节点
public DoubleLoopNode next() {
return this.next;
}
// 获取上一个节点
public DoubleLoopNode pre() {
return this.pre;
}
// 获取数据
public int getData() {
return this.data;
}
}
public class Main {
public static void main(String[] args) {
// 创建节点
DoubleLoopNode dln1 = new DoubleLoopNode(1);
DoubleLoopNode dln2 = new DoubleLoopNode(2);
DoubleLoopNode dln3 = new DoubleLoopNode(3);
// 追加节点
dln1.append(dln2);
dln2.append(dln3);
// 查看所有节点
System.out.println(dln2.pre().getData());
System.out.println(dln2.getData());
System.out.println(dln2.next().getData());
}
}