Java数据结构-双向循环链表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java数据结构-双向循环链表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());
}
}。