-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathLinkedList.js
397 lines (331 loc) · 9.43 KB
/
LinkedList.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* 由于链表在空间的合理利用上和插入,删除时不需要移动等的有点,因此在很多场合下,它是线性表的首选存储结构。然而,它也存在着实现某些基本操作,如求线性表长度时不如顺序存储结构的缺点;另一方面,由于在链表中,结点之间的关系使用指针来表示,则数据元素在线性表中的“位序”的概念已淡化,而被数据元素在线性链表中的“位置”所代替。为此,从实际出发重新定义线性链表及其基本操作
*/
class Node {
constructor(data = null, next = null){
this.data = data;
this.next = next;
}
}
export default class LinkedList {
constructor(sqList = []){
this.head = null;
this.tail = null;
if (sqList) {
for (let i = 0, len = sqList.length; i < len; ++i)
this.push(sqList[i]);
}
}
/**
* merge list, note: this operation will delete a and b nodes.
* @param {LinkedList} a
* @param {LinkedList} b
* @param {*} compare
*/
static mergeList (a, b, compare = compFn) {
let ha = a.head;
let hb = b.head;
let pa = ha;
let pb = hb;
let c = new LinkedList();
let q;
while (pa && pb) {
let data1 = pa.data;
let data2 = pb.data;
if (!compare(data1, data2)) {
// delete head node
q = a.shift();
// append the node to c linkedList
c.append(q);
pa = a.head;
} else {
q = b.shift();
c.append(q);
pb = b.head;
}
}
if (pa) {
c.append(pa);
c.tail = a.tail;
}
else {
c.append(pb);
c.tail = b.tail;
}
return c;
}
/**
* remove the first element and return it
*/
shift () {
let head = this.head;
this.head = this.head.next;
head.next = null;
if (this.head === null) this.tail = null;
return head;
}
/**
* remove the last element and return it
*/
pop(){
let current = this.head;
let previous = this.head;
let elem;
while (current !== null) {
if (this.tail === current) {
if (current === this.head) {
elem = this.tail.data;
this.head = null;
break;
}
this.tail = previous;
previous.next = current.next;
elem = current.data;
break;
}
previous = current;
current = current.next;
}
if (this.head === null) this.tail = null;
return elem ? elem : false;
}
/**
* append node
* @param {Node} node
*/
append (node) {
if (this.head !== null) {
this.tail.next = node;
this.tail = this.tail.next;
} else {
this.head = node;
this.tail = node;
}
}
/**
* add data
* @param {*} data
*/
push (data) {
if (this.head === null) {
this.head = new Node(data);
this.tail = this.head;
} else {
this.tail.next = new Node(data);
this.tail = this.tail.next;
}
this.tail.data = data;
}
/**
* remove data
* @param {*} data
*/
remove (data) {
let current = this.head;
let previous = this.head;
let elem;
while (current !== null) {
if (data === current.data) {
if (current === this.head) {
this.head = current.next;
elem = current.data;
break;
}
if (current === this.tail) this.tail = previous;
previous.next = current.next;
elem = current.data;
break;
}
previous = current;
current = current.next;
}
if (this.head === null) this.tail = null;
return elem ? elem : false;
}
/**
* find the index of matched data
* @param {*} data
*/
indexOf(data){
let current = this.head;
let index = -1;
while (current !== null) {
++index;
if (current.data === data) {
return index;
}
current = current.next;
}
return index;
}
/**
* add data to the front
* @param {*} data
*/
unshift (data) {
let temp = new Node(data);
temp.next = this.head;
this.head = temp;
}
/**
*
* @param {*} target
* @param {*} data
*/
insertAfter (target, data) {
let current = this.head;
while (current !== null) {
if (current.data === target) {
let temp = new Node(data);
temp.next = current.next;
if (current === this.tail) this.tail = temp;
current.next = temp;
return;
}
current = current.next;
}
}
item (index) {
let current = this.head;
while (current !== null) {
if (--index === 0) return current;
current = current.next;
}
return null;
}
forEach (callback) {
if (typeof callback !== 'function') return;
for (let current = this.head, index = 0; current; current = current.next) {
if (callback(current.data, index++)) break;
}
}
*[Symbol.iterator](){
for(let current = this.head; current; current = current.next){
yield current.data;
}
}
get size () {
let current = this.head;
let size = 0;
while (current !== null) {
++size;
current = current.next;
}
return size;
}
toString () {
let str = '';
this.forEach((node) => {
str += node.data + (node.next ? ',' : '');
});
return str;
}
/**
* insert element by order
* @param {*} data
* @param {Function} cmp
*/
orderInsert (data, cmp) {
cmp = typeof cmp === 'function' ? cmp : (a, b) => {
if (a > b)
return 1;
else if (a === b)
return 0;
else
return -1;
};
let previous = this.head;
let current = this.head;
if (current === null) {
this.head = this.tail = new Node(data);
return;
}
let me = this;
while (current) {
let ret = cmp(data, current.data);
// 如果插入元素大于当前元素,准备下次遍历
if (ret > 0) {
previous = current;
current = current.next;
// 如果等于,直接插入到后面
} else if (ret === 0) {
return insertBetween(data, previous, current);
// 如果小于则插入到前节点和当前节点中
// 因为已经是排序了,所以不需要多余判断了
} else {
if (this.head === previous && previous === current)
return this.unshift(data);
else
return insertBetween(data, previous, current);
}
}
// 插入到最后一个结点
previous.next = new Node(data);
this.tail = previous.next;
function insertBetween(data, a, b) {
if (a == b) {
if (a == me.head)
return me.unshift(data);
} else {
let temp = new Node(data);
temp.next = b;
a.next = temp;
return true;
}
}
}
// 删除元素递增排列的链表中值大于min,且小于max的所有元素
deleteBetween (min, max) {
let p = this.head;
// p是最后一个不大于min的元素
while (p.next && p.next.data <= min) p = p.next;
// 如果还有比min更大的元素
let q;
if (p.next) {
q = p.next;
// q是第一个不小于max的元素
while (q && q.data < max) q = q.next;
p.next = q;
}
let last = q || p;
while (last.next) last = last.next;
this.tail = last;
}
// 删除元素递增排列的链表的重复元素
deleteEqual () {
let p = this.head;
let q = p.next;
while (p.next) {
// 当相邻两元素不相等时,p,q都向后移
if (p.data !== q.data) {
p = p.next;
q = p.next;
} else {
while (q.data === p.data) q = q.next;
// 删除
p.next = q;
p = q;
q = p.next;
}
}
}
reverse () {
let p = this.head;
let q = p.next;
let s = q.next;
this.tail = p;
p.next = null;
while (s.next) {
q.next = p;
p = q;
q = s;
s = s.next;
}
q.next = p;
s.next = q;
this.head = s;
}
}
function compFn(a, b) {
return a - b;
}