0%

structure-link

单链表

普通单链表

普通单链表(无头结点)

MySingleLinkWithoutHeadNode.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
* 3. 无头结点时操作第一个元素需要更新head指针
*/
public class MySingleLinkWithoutHeadNode {
private static class MyNode {
public String value;
public MyNode next;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = null;

public MySingleLinkWithoutHeadNode() {

}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

node.next = head;

// update head pointer
head = node;

// error code: p is updated but head not
// MyNode p = head;
// node.next = p;
// p = node;

return true;
}

public String removeFirst() {
// empty link
if (head == null) {
return null;
}

MyNode node = head;

// update head pointer
head = node.next;

// error code: p is updated but head not
// MyNode p = head;
// node = p;
// p = node.next;

return node.value;
}

/**
* 在尾部添加时需要找到倒数第一个
* 使用p.next != null为循环条件和p.next == null为结束条件
* 当链表为空时为了防止开始的时候p为null时导致空指针异常
* 无头结点时需要特殊处理
* 有头结点时任何时候p不可能为null
*/
public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = head;

// no one node, specifically handle as add first node
if (p == null) {
// return addFirst(s);

node.next = head;

// update head pointer
head = node;

return true;
}

// use p.next to find the last node
while (p.next != null) {
p = p.next;
}

node.next = p.next;

p.next = node;

return true;
}

/**
* 在尾部删除时需要找到倒数第二个
* 使用p.next.next != null为循环条件和p.next.next == null为结束条件
* 因为当链表为空时无法执行删除操作
* 所以在头部和尾部删除时空链表需要特殊处理
* 即需要判断链表为空时就不进行后续处理并返回
* 当链表元素只有一个时为了防止开始的时候p.next为null时导致空指针异常
* 无头结点时链表时需要特殊处理
* 有头结点时任何时候p.next不可能为null
*/
public String removeLast() {
// empty link
if (head == null) {
return null;
}

MyNode p = head;

// only one node, specifically handle as remove first node
if (p.next == null) {
// return removeFirst();

MyNode node = head;

// update head pointer
head = node.next;

return node.value;
}

// use p.next.next to find the penult node
while (p.next.next != null) {
p = p.next;
}

MyNode node = p.next;

p.next = node.next;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head;
while (p != null) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head;

int i = 0;
while (p != null) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MySingleLinkWithoutHeadNode link = new MySingleLinkWithoutHeadNode();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MySingleLinkWithoutHeadNode link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MySingleLinkWithoutHeadNode link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

普通单链表(有头结点)

MySingleLink.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MySingleLink {
private static class MyNode {
public String value;
public MyNode next;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);

public MySingleLink() {

}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

node.next = head.next;

// update head pointer
head.next = node;

// error code: p.next is updated but head.next not
// MyNode p = head;
// node.next = p.next;
// p.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next == null) {
return null;
}

MyNode node = head.next;

// update head pointer
head.next = node.next;

// error code: p.next is updated but head.next not
// MyNode p = head;
// node = p.next;
// p.next = node.next;

return node.value;
}

/**
* 在尾部添加时需要找到倒数第一个
* 使用p.next != null为循环条件和p.next == null为结束条件
* 当链表为空时为了防止开始的时候p为null时导致空指针异常
* 无头结点时需要特殊处理
* 有头结点时任何时候p不可能为null
*/
public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = head;

// use p.next to find the last node
while (p.next != null) {
p = p.next;
}

node.next = p.next;

p.next = node;

return true;
}

/**
* 在尾部删除时需要找到倒数第二个
* 使用p.next.next != null为循环条件和p.next.next == null为结束条件
* 因为当链表为空时无法执行删除操作
* 所以在头部和尾部删除时空链表需要特殊处理
* 即需要判断链表为空时就不进行后续处理并返回
* 当链表元素只有一个时为了防止开始的时候p.next为null时导致空指针异常
* 无头结点时链表时需要特殊处理
* 有头结点时任何时候p.next不可能为null
*/
public String removeLast() {
// empty link
if (head.next == null) {
return null;
}

MyNode p = head;

// use p.next.next to find the penult node
while (p.next.next != null) {
p = p.next;
}

MyNode node = p.next;

p.next = node.next;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p != null) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p != null) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MySingleLink link = new MySingleLink();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MySingleLink link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MySingleLink link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

普通单链表(有头结点,有尾指针)

MySingleLinkWithTailPointer.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MySingleLinkWithTailPointer {
private static class MyNode {
public String value;
public MyNode next;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);
private MyNode tail = head;

public MySingleLinkWithTailPointer() {

}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

// empty link
if (head.next == null) {
tail = node;
}

node.next = head.next;

// update head pointer
head.next = node;

// error code: p.next is updated but head.next not
// MyNode p = head;
// node.next = p.next;
// p.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next == null) {
return null;
}

MyNode node = head.next;

// update head pointer
head.next = node.next;

// error code: p.next is updated but head.next not
// MyNode p = head;
// node = p.next;
// p.next = node.next;

// empty link
if (head.next == null) {
tail = head;
}

return node.value;
}

public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = tail;

p.next = node;

// tail = p.next;
// tail = tail.next;
tail = node;

return true;
}

public String removeLast() {
// empty link
if (head.next == null) {
return null;
}

MyNode p = head;

// use p.next.next to find the penult node
while (p.next.next != null) {
p = p.next;
}

MyNode node = p.next;

p.next = node.next;

tail = p;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p != null) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p != null) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MySingleLink link = new MySingleLink();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MySingleLink link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MySingleLink link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

循环单链表

循环单链表(有头结点)

MySingleLoopLink.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MySingleLoopLink {
private static class MyNode {
public String value;
public MyNode next;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);

public MySingleLoopLink() {
head.next = head;
}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

node.next = head.next;

// update head pointer
head.next = node;

// error code: p.next is updated but head.next not
// MyNode p = head;
// node.next = p.next;
// p.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next == head) {
return null;
}

MyNode node = head.next;

// update head pointer
head.next = node.next;

// error code: p.next is updated but head.next not
// MyNode p = head;
// node = p.next;
// p.next = node.next;

return node.value;
}

public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = head;

// use p.next to find the last node
while (p.next != head) {
p = p.next;
}

node.next = p.next;

p.next = node;

return true;
}

public String removeLast() {
// empty link
if (head.next == head) {
return null;
}

MyNode p = head;

// use p.next.next to find the penult node
while (p.next.next != head) {
p = p.next;
}

MyNode node = p.next;

p.next = node.next;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p != head) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p != head) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MySingleLoopLink link = new MySingleLoopLink();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MySingleLoopLink link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MySingleLoopLink link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

双链表

普通双链表

普通双链表(有头结点,无尾结点,无尾指针)

MyDoubleLinkWithoutTailNodeAndTailPointer.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MyDoubleLinkWithoutTailNodeAndTailPointer {
private static class MyNode {
public String value;
public MyNode next;
public MyNode prev;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);

public MyDoubleLinkWithoutTailNodeAndTailPointer() {

}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

node.next = head.next;
node.prev = head;

if (node.next != null) {
node.next.prev = node;
}

node.prev.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next == null) {
return null;
}

MyNode node = head.next;

if (node.next != null) {
node.next.prev = node.prev;
}

node.prev.next = node.next;

return node.value;
}

public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = head;

// use p.next to find the last node
while (p.next != null) {
p = p.next;
}

node.next = p.next;
node.prev = p;

if (node.next != null) {
node.next.prev = node;
}

node.prev.next = node;

return true;
}

public String removeLast() {
// empty link
if (head.next == null) {
return null;
}

MyNode p = head;

// use p.next to find the last node
while (p.next != null) {
p = p.next;
}

MyNode node = p;

if (node.next != null) {
node.next.prev = node.prev;
}

node.prev.next = node.next;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p != null) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p != null) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MyDoubleLinkWithoutTailNodeAndTailPointer link = new MyDoubleLinkWithoutTailNodeAndTailPointer();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MyDoubleLinkWithoutTailNodeAndTailPointer link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MyDoubleLinkWithoutTailNodeAndTailPointer link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

普通双链表(有头结点,有尾结点,无尾指针)

MyDoubleLinkWithTailNode.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MyDoubleLinkWithTailNode {
private static class MyNode {
public String value;
public MyNode next;
public MyNode prev;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);

public MyDoubleLinkWithTailNode() {
MyNode tail = new MyNode(null);

head.next = tail;
tail.prev = head;
}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

node.next = head.next;
node.prev = head;

node.next.prev = node;
node.prev.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next.next == null) {
return null;
}

MyNode node = head.next;

node.next.prev = node.prev;
node.prev.next = node.next;

return node.value;
}

public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = head;

// use p.next.next to find the last node
while (p.next.next != null) {
p = p.next;
}

node.next = p.next;
node.prev = p;

node.next.prev = node;
node.prev.next = node;

return true;
}

public String removeLast() {
// empty link
if (head.next.next == null) {
return null;
}

MyNode p = head;

// use p.next.next to find the last node
while (p.next.next != null) {
p = p.next;
}

MyNode node = p;

node.next.prev = node.prev;
node.prev.next = node.next;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p.next != null) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p.next != null) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MyDoubleLinkWithTailNode link = new MyDoubleLinkWithTailNode();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MyDoubleLinkWithTailNode link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MyDoubleLinkWithTailNode link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

普通双链表(有头结点,无尾结点,有尾指针)

MyDoubleLinkWithTailPointer.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MyDoubleLinkWithTailPointer {
private static class MyNode {
public String value;
public MyNode next;
public MyNode prev;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);
private MyNode tail = head;

public MyDoubleLinkWithTailPointer() {

}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

// empty link
if (head.next == null) {
tail = node;
}

node.next = head.next;
node.prev = head;

if (node.next != null) {
node.next.prev = node;
}

node.prev.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next == null) {
return null;
}

MyNode node = head.next;

if (node.next != null) {
node.next.prev = node.prev;
}

node.prev.next = node.next;

// empty link
if (head.next == null) {
tail = head;
}

return node.value;
}

public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = tail;

node.next = p.next;
node.prev = p;

if (node.next != null) {
node.next.prev = node;
}

node.prev.next = node;

// tail = p.next;
// tail = tail.next;
tail = node;

return true;
}

public String removeLast() {
// empty link
if (head.next == null) {
return null;
}

MyNode p = head;

// use p.next to find the last node
while (p.next != null) {
p = p.next;
}

MyNode node = p;

if (node.next != null) {
node.next.prev = node.prev;
}

node.prev.next = node.next;

tail = p;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p != null) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p != null) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MyDoubleLinkWithoutTailNodeAndTailPointer link = new MyDoubleLinkWithoutTailNodeAndTailPointer();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MyDoubleLinkWithoutTailNodeAndTailPointer link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MyDoubleLinkWithoutTailNodeAndTailPointer link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

普通双链表(有头结点,有尾结点,有尾指针)

MyDoubleLink.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MyDoubleLink {
private static class MyNode {
public String value;
public MyNode next;
public MyNode prev;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);
private MyNode tail = new MyNode(null);

public MyDoubleLink() {
head.next = tail;
tail.prev = head;
}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

node.next = head.next;
node.prev = head;

node.next.prev = node;
node.prev.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next == tail) {
return null;
}

MyNode node = head.next;

node.next.prev = node.prev;
node.prev.next = node.next;

return node.value;
}

public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = tail.prev;

node.next = p.next;
node.prev = p;

node.next.prev = node;
node.prev.next = node;

return true;
}

public String removeLast() {
// empty link
if (head.next == tail) {
return null;
}

MyNode node = tail.prev;

node.next.prev = node.prev;
node.prev.next = node.next;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p != tail) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p != tail) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MyDoubleLink link = new MyDoubleLink();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MyDoubleLink link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MyDoubleLink link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}

循环双链表

循环单链表(有头结点)

MyDoubleLoopLink.javaview raw
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package dsa.link;

import java.util.Arrays;

/**
* 1. 插入操作不需要考虑空链表
* 2. 删除操作需要考虑空链表
*/
public class MyDoubleLoopLink {
private static class MyNode {
public String value;
public MyNode next;
public MyNode prev;

public MyNode(String value) {
this.value = value;
}
}

private MyNode head = new MyNode(null);

public MyDoubleLoopLink() {
head.next = head;
head.prev = head;
}

public boolean addFirst(String s) {
MyNode node = new MyNode(s);

node.next = head.next;
node.prev = head;

node.next.prev = node;
node.prev.next = node;

return true;
}

public String removeFirst() {
// empty link
if (head.next == head) {
return null;
}

MyNode node = head.next;

node.next.prev = node.prev;
node.prev.next = node.next;

return node.value;
}

public boolean addLast(String s) {
MyNode node = new MyNode(s);

MyNode p = head.prev;

node.next = p.next;
node.prev = p;

node.next.prev = node;
node.prev.next = node;

return true;
}

public String removeLast() {
// empty link
if (head.next == head) {
return null;
}

MyNode node = head.prev;

node.next.prev = node.prev;
node.prev.next = node.next;

return node.value;
}

public int size() {
int size = 0;

MyNode p = head.next;
while (p != head) {
size++;

p = p.next;
}

return size;
}

public String[] toArray() {
int size = size();

String[] array = new String[size];

MyNode p = head.next;

int i = 0;
while (p != head) {
array[i] = p.value;

i++;

p = p.next;
}

return array;
}

public String toString() {
return Arrays.toString(toArray());
}

public static void main(String[] args) {
int capacity = 7;
MyDoubleLoopLink link = new MyDoubleLoopLink();

// test ops for k times
for (int k = 0; k < 3; k++) {
test1(link, capacity);
test2(link, capacity);
}
}

public static void test1(MyDoubleLoopLink link, int capacity) {
// test for addLast and removeFirst
System.out.println("\n---test for addLast and removeFirst---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeFirst---\n");
while (true) {
String s = link.removeFirst();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addLast---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addLast(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}

public static void test2(MyDoubleLoopLink link, int capacity) {
// test for addFirst and removeLast
System.out.println("\n---test for addFirst and removeLast---\n");

// test ops for k times
for (int k = 0; k < 3; k++) {
System.out.println("\n---test for removeLast---\n");
while (true) {
String s = link.removeLast();

System.out.println(String.format("remove value %2s , link is %s", s, link.toString()));

if (s == null) {
break;
}
}

System.out.println("\n---test for addFirst---\n");
for (int i = 0; i <= capacity; i++) {
String s = "" + i;

link.addFirst(s);

System.out.println(String.format("add value %2s , link is %s", s, link.toString()));
}
}
}
}
只想买包辣条