-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-lists.js
357 lines (315 loc) · 9.19 KB
/
linked-lists.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
class SingleLinkedList {
constructor() {
this.headNode = null;
}
append(item) {
if (item === null || item === undefined) return;
const node = { data: item, next: null };
if (!this.headNode) {
this.headNode = node;
} else {
let tail = this.headNode;
while (tail.next !== null) {
tail = tail.next;
}
tail.next = node;
}
}
removeHead() {
if (this.headNode) {
this.headNode = this.headNode.next;
}
}
cursor() {
return new SingleLinkedListCursor(this);
}
getHead() {
if (this.headNode) {
return this.headNode.data;
} else {
return null;
}
}
isEmpty() {
return this.headNode === null;
}
count() {
const cursor = this.cursor();
let count = 0;
while (!cursor.isAtEnd()) {
count += 1;
cursor.moveToNext();
}
return count;
}
}
class SingleLinkedListCursor {
constructor(list) {
this._list = list;
this._node = list.headNode;
this._previous = null;
}
moveToNext() {
if (this._node !== null) {
this._previous = this._node;
this._node = this._node.next;
}
}
/** Go to last node */
moveToTail() {
while (this._node && this._node.next) {
this.moveToNext();
}
}
/** Go past the last node, can be useful to append a node to the list. */
moveToEnd() {
while (this._node) {
this.moveToNext();
}
}
moveToHead() {
this._previous = null;
this._node = this._list.headNode;
}
/** Insert a new node before the current node, if the cursor is past the end
* of the list then the node will be appended.
*
* The cursor will be on the inserted node after this operation. */
insertBefore(item) {
if (item === null || item === undefined) return false;
const node = { data: item, next: this._node, };
if (this._previous) {
this._previous.next = node;
} else {
this._list.headNode = node;
}
this._node = node;
return true;
}
/** Remove current node and leave cursor at next node. */
remove() {
if (this._node === null) return false;
const nextNode = this._node.next;
// Remove link from previous node:
if (this._previous !== null) {
this._previous.next = nextNode;
}
// Remove link from list:
if (this._list.headNode === this._node) {
this._list.headNode = nextNode;
}
// Update cursor:
this._node = nextNode;
return true;
}
current() {
if (this._node !== null) {
return this._node.data;
} else {
return null;
}
}
/** Returns `true` if we have reached the end of the list. */
isAtEnd() {
return this._node === null;
}
}
class DoubleLinkedList {
constructor() {
this.headNode = null;
}
append(item) {
if (item === null || item === undefined) return false;
const node = { data: item, next: null, prev: null };
if (!this.headNode) {
this.headNode = node;
node.prev = node;
node.next = node;
} else {
let head = this.headNode;
let tail = this.headNode.prev;
// Last node so next -> wraps to first node:
node.next = head;
// Last node so prev -> the last node before adding it:
node.prev = tail;
// After the current last node:
tail.next = node;
// Before the first node (since we wrap):
head.prev = node;
}
return true;
}
removeHead() {
this.cursor().remove();
}
cursor() {
return new DoubleLinkedListCursor(this);
}
getHead() {
if (this.headNode) {
return this.headNode.data;
} else {
return null;
}
}
isEmpty() {
return this.headNode === null;
}
count() {
if (this.isEmpty()) return 0;
const cursor = this.cursor();
let count = 0;
while (true) {
count += 1;
cursor.moveToNext();
if (cursor.isAtHead()) break;
}
return count;
}
}
class DoubleLinkedListCursor {
constructor(list) {
this._list = list;
this._node = list.headNode;
}
moveToPrevious() {
if (this._node !== null) {
this._node = this._node.prev;
}
}
moveToNext() {
if (this._node !== null) {
this._node = this._node.next;
}
}
/** Go to last node */
moveToTail() {
if (!this._list.headNode) {
this._node = null;
return false;
}
this._node = this._list.headNode.prev;
return true;
}
moveToHead() {
if (!this._list.headNode) {
this._node = null;
return false;
}
this._node = this._list.headNode;
return true;
}
/** Insert a new node before the current node.
*
* The cursor will be on the inserted node after this operation. */
insertBefore(item) {
if (item === null || item === undefined) return;
if (!this._node) this.moveToHead();
if (this.isDisconnectedNode()) return;
if (!this._list.headNode) {
// 0 items in list:
this._list.append(item);
this.moveToTail();
return;
}
const node = { data: item, next: null, prev: null };
if (this._node) {
const current = this._node;
const prev = current.prev;
// (new) -> current
node.next = current;
// prev <- (new)
node.prev = prev;
// (prev) -> new
prev.next = node;
// new <- (current)
current.prev = node;
}
if (this._node === this._list.headNode) {
// Inserted before head, so update head as well:
this._list.headNode = node;
}
this._node = node;
}
/** Insert a new node before the current node.
*
* The cursor will be on the inserted node after this operation. */
insertAfter(item) {
if (item === null || item === undefined) return;
if (!this._node) this.moveToHead();
if (this.isDisconnectedNode()) return;
if (!this._list.headNode) {
// 0 items in list:
this._list.append(item);
this.moveToTail();
return;
}
if (this._node.next === this._list.headNode) {
// Cursor is at the tail node so just append to the list:
this._list.append(item);
this.moveToTail();
return;
}
// Insert before next node:
this.moveToNext();
this.insertBefore(item);
}
/** Remove current node and leave cursor at next node. */
remove() {
if (this._node === null) return false;
if (this.isDisconnectedNode()) return false;
const current = this._node;
const prev = current.prev;
const next = current.next;
// Remove any links from other nodes:
// Change (prev -> current) to (prev -> next):
prev.next = next;
// Change (current <- next) to (prev <- next):
next.prev = prev;
// Reset links from removed node (since this node won't be used again
// this isn't necessary but we might as well):
current.next = null;
current.prev = null;
// The node that cursor and list should point to after the removal (if
// there is only a single node then point to null):
const nextNode = next === current ? null : next;
// Remove possible link from list:
if (this._list.headNode === current) {
this._list.headNode = nextNode;
}
// Remove cursor's link to the node:
this._node = nextNode;
return true;
}
/** Create a new cursor at the current node. */
clone() {
const cloned = new DoubleLinkedListCursor(this._list);
cloned._node = this._node;
return cloned;
}
current() {
if (this._node !== null) {
return this._node.data;
} else {
return null;
}
}
isAtHead() {
return this._node === this._list.headNode;
}
isAtTail() {
if (this._list.headNode === null) {
return this._node === null;
} else {
return this._node === this._list.headNode.prev;
}
}
/** The node at the cursor's current position is disconnected from the list
* (it has been deleted). Create a new cursor or use the `moveToTail` or
* `moveToHead` methods to move the cursor to a connected node. */
isDisconnectedNode() {
if (this._node === null) return this._list.headNode !== null;
// The next or prev fields are only null if the node has been removed:
return this._node.next === null;
}
}