单链表

https://www.jianshu.com/u/0c7569d9705d

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* 单链表结点
* @author zhongjiahong
*
*/
//一个节点
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 after(Node node){
//取出下一个节点,作为下下一个节点
Node nextNext = next;
//把新节点做为当前节点的下一个节点
this.next=node;
//把下下一个节点设置为新节点的下一个节点
node.next=nextNext;
}

//删除下一个节点
public void removeNext(){
//取出下下一个节点
Node newNext = next.next;
//把下下一个节点设置为当前节点的下一个节点
this.next=newNext;
}

//获取下一个节点
public Node next(){
return this.next;
}

//获取节点中的数据
public int getData(){
return this.data;
}

//显示所有节点信息
public void show(){
Node currentNode = this;
while(true){
System.out.println(currentNode.data+"");
//取出下一个节点
currentNode = currentNode.next;
//如果是最后一个节点
if(currentNode==null){
break;
}
}
}
}

##单项循环链表

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
public class LoopNode {

//节点内容
int data;
//下一个节点(和单链表的区别,每一个节点都是一个循环链表)
LoopNode next=this;

public LoopNode(int data){
this.data = data;
}


//插入一个节点做为当前节点的下一个节点
public void after(LoopNode node){
//取出下一个节点,作为下下一个节点
LoopNode nextNext = next;
//把新节点做为当前节点的下一个节点
this.next=node;
//把下下一个节点设置为新节点的下一个节点
node.next=nextNext;
}

//删除下一个节点
public void removeNext(){
//取出下下一个节点
LoopNode newNext = next.next;
//把下下一个节点设置为当前节点的下一个节点
this.next=newNext;
}

//获取下一个节点
public LoopNode next(){
return this.next;
}

//获取节点中的数据
public int getData(){
return this.data;
}
}