-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathone-way-linked-list.java
More file actions
104 lines (98 loc) · 2.11 KB
/
one-way-linked-list.java
File metadata and controls
104 lines (98 loc) · 2.11 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 链表的数据域,可以扩增数据
class Node{
int date;
Node(){
this(0);
}
Node(int date){
this.date = date;
}
}
// 链表类,使用 creatNewLink 方法创建头结点对象,其他操作全部使用该对象
public class Link{
Node node;
Link next;
Link(){
node = null;
next = null;
}
Link(int date){
node = new Node(date);
next = null;
}
// 创建头结点,静态方法,直接赋给对象
public static Link creatNewLink(int date){
Link headLink = new Link(date);
return headLink;
}
// 正插法,在 headLink 后插入链表
public void addLink(int date){
Link newLink = new Link(date);
newLink.next = this.next;
this.next = newLink;
}
// 查找,Node 里有大量数据时才有意义,利用 key 来获取其他信息
public Link find(int date){
Link findNode = this;
Link errorNode = null;
while(findNode.next != null){
if(findNode.node.date == date){
return findNode;
}else{
findNode = findNode.next;
}
}
return errorNode;
}
// 删除
public int remove(int date){
Link removeNode = this;
Link topNode = this;
while(removeNode.next != null){ if(removeNode.node.date == date){
topNode.next = removeNode.next;
removeNode.next = null;
return 1;
}else{
topNode = removeNode;
removeNode = removeNode.next;
}
}
if(removeNode.node.date == date){
topNode.next = removeNode.next;
removeNode.next = null;
return 1;
}
return 0;
}
// 修改
public int alter(int date,int newDate){
Link alterNode = find(date);
if(alterNode != null){
alterNode.node.date = newDate;
return 1;
}else{
return 0;
}
}
// 遍历链表并输出,默认10个数据为1行。
public void print(){
Link nextNode = this;
int i = 0;
while(nextNode != null){
System.out.printf("%5d",nextNode.node.date);
nextNode = nextNode.next;
i++;
if(i%10 == 0){
System.out.println();
}
}
}
}
// 上面的插入实际上是后来的插到head后,有尾插法:
public void insert(){
Link newLink = new Link(data);
newLink.top.next = newLink;
newLink.top = newLink;
}
// top 指向最末位
top = this;