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;
}
//删除下一个节点
public void removeNext() {
//取出下下个节点
Node newNext = next.next;
//把下下一个节点设置为当前节点的下一个节点
this.next = newNext;
}
//显示所有节点信息
public void show() {
Node currentNode = this;
while(true) {
System.out.print(currentNode.data+" ");
currentNode = currentNode.next;
if(currentNode == null) {
break;
}
}
}
//插入一个节点作为当前的下一个节点
public void after(Node node) {
//取出下一个节点,作为下下一个节点
Node nextNext = next;
//把所节点作为当前节点的下一个节点
this.next = node;
//把下下一个节点设置为新节点的下一个节点
node.next = nextNext;
}
//获取下一个节点
public Node next() {
return this.next;
}
//获取节点中的数据
public int getData() {
return this.data;
}
//当前节点是否最后一个节点
public boolean isLast() {
return next == null;
}
}
数据结构——单向链表(java实现)
最新推荐文章于 2025-01-14 23:20:21 发布