forked from qufei1993/Examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoubly-linked-list.js
280 lines (228 loc) · 6.23 KB
/
doubly-linked-list.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const CircularJSON = require('circular-json');
class DoublyLinkedList {
constructor() {
this.node = function(element) {
return {
element,
next: null,
prev: null, // 新增
}
};
this.length = 0;
this.head = null;
this.tail = null; // 新增
}
isEmpty() {
return this.length === 0 ? true : false;
}
getLength() {
return this.length;
}
/**
* 尾部插入
*/
insertTail(e) {
let node = this.node(e);
let current;
if (this.head === null) { // 列表中还没有元素
this.head = node;
} else {
current = this.head;
while (current.next) { // 下个节点存在
current = current.next;
}
current.next = node;
}
this.length++;
}
/**
* 在任意位置插入元素
* @param { Number } i 插入的元素位置
* @param { * } e 插入的元素
*/
insert(i, e) {
if (i < 0 || i > this.length) {
return false;
}
let node = this.node(e);
let current = this.head;
let previous;
if (i === 0) { // 有修改
if (current) {
node.next = current;
current.prev = node;
this.head = node;
} else {
this.head = this.tail = node;
}
} else if (i === this.length) { // 新增加
current = this.tail;
current.next = node;
node.prev = current;
this.tail = node;
} else {
for (let k=0; k<i; k++) {
previous = current;
current = current.next; // 保存当前节点的下一个节点
}
node.next = current;
previous.next = node; // 注意,这块涉及到对象的引用关系
current.prev = node; // 新增加
node.prev = previous; // 新增加
}
this.length++;
return true;
}
/**
* 移除指定位置的元素
* @param { Number} i 位置
*/
delete(i) {
// 要删除的元素位置不能超过链表的最后一位
if (i < 0 || i >= this.length) {
return false;
}
let current = this.head;
let previous;
if (i === 0) {
this.head = current.next;
if (this.length === 1) {
this.tail = null;
} else {
this.head.prev = null;
}
} else if (i === this.length -1) {
current = this.tail;
this.tail = current.prev;
this.tail.next = null;
} else {
for (let k=0; k<i; k++) {
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous; // 新增加
}
this.length--;
return current.element;
}
/**
* 获取指定位置元素,类似于 delete 方法,可做参考
*/
getElement(i) {
// 要删除的元素位置不能超过链表的最后一位
if (i < 0 || i >= this.length) {
return false;
}
let current = this.head;
let previous;
for (let k=0; k<=i; k++) {
previous = current
current = current.next;
}
return previous.element;
}
/**
* 返回链表中第 1 个与传入的元素相匹配的位置,否则返回 -1
* @param {*} e 需要查找的元素
*/
locateElement(e) {
let current = this.head;
let index = 0;
while (current.next) { // 下个节点存在
if (index === 0) {
if (current.element === e) {
return index;
}
}
current = current.next;
index++;
if (current.element === e) {
return index;
}
}
return -1;
}
/**
* 获取链表指定元素的前驱
* @param {*} e 元素
*/
priorElement(e) {
let current = this.head;
let previous;
if (current.element === e) { // 第 0 个节点
return false; // 没有前驱
} else {
while (current.next) { // 下个节点存在
previous = current;
current = current.next;
if (current.element === e) {
return previous.element;
}
}
}
return false;
}
/**
* 获取指定元素后继
* @param {*} e 元素
*/
nextElement(e) {
let current = this.head;
while (current.next) { // 下个节点存在
if (current.element === e) {
return current.next.element;
}
current = current.next;
}
return false;
}
/**
* 先找到头节点,如果头节点的 next 为 null,说明链表为空。
* 如果不为空,就要依此寻找下限
*/
clear() {
/* let current = this.head.next;
while (current) {
let temp = current.next;
current = null
delete current;
current = temp;
}
this.head.next = null; */
this.head = null;
}
/**
* 析构函数,清除内存
* 先调用 clear()
*/
destroy() {
this.head = null;
}
traversing(){
console.log(CircularJSON.stringify(this.head));
let current = this.head,
string = '';
while (current) {
string += current.element + ' ';
current = current.next;
}
console.log(string);
return string;
}
getHead() {
return this.head;
}
getTail() {
return this.tail;
}
}
const list = new DoublyLinkedList();
list.insert(0, 'b');
list.insert(0, 'a');
list.insert(2, 'c');
list.insert(3, 'd');
list.traversing();
console.log(list.getLength());
console.log(CircularJSON.stringify(list.getHead()));
console.log(CircularJSON.stringify(list.getTail()));