-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
60 lines (52 loc) · 1.34 KB
/
Copy pathNode.java
File metadata and controls
60 lines (52 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cx.Linear;
public class Node {
private int data;
private Node next;
//构造函数
public Node(int value) {
data = value;
}
//加入节点
public Node append(Node node) {
Node currentNode = this;
while (true) {
Node nextNode = currentNode.next;
if (nextNode == null) break;
currentNode = nextNode;
}
currentNode.next = node;
return this;
}
//获取next节点
public Node next() {
return this.next;
}
//获取节点值
public int getData() {
return this.data;
}
//显示所有列表
public void show() {
Node currentNode = this;
while (true) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
if (currentNode == null) {
System.out.println();
break;
}
}
}
//删除下一个节点
public void removeNext() {
Node newNext = this.next;
if (newNext == null) throw new RuntimeException("this node has no next");
this.next = newNext.next;
}
//插入一个节点作为当前节点的下一个节点
public void after(Node node){
Node nextNext=this.next;
this.next=node;
node.next=nextNext;
}
}