-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SingleLinkedList.ts
397 lines (353 loc) · 9.05 KB
/
SingleLinkedList.ts
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
import { List } from "./interfaces";
import { CursorBase, RangeBase } from "./util";
interface Node<T> {
next: Node<T> | null;
value: T;
}
/**
* @ignore
*/
export class SingleLinkedListCursor<T> extends CursorBase<T> {
constructor(protected _list: SingleLinkedList<T>, public _node: Node<T>) {
super();
}
get value() {
return this._node.value;
}
set value(newValue: T) {
this._node.value = newValue;
}
public prev() {
const prev = this._list._findPrev(this._node);
if (prev === null) {
return null;
}
return new SingleLinkedListCursor<T>(this._list, prev);
}
public next() {
if (this._node.next === null) {
return null;
}
return new SingleLinkedListCursor<T>(this._list, this._node.next);
}
}
/**
* @ignore
*/
export class SingleLinkedListRange<T> extends RangeBase<T> {
constructor(protected _list: SingleLinkedList<T>, protected _startNode: Node<T> | null, protected _endNode: Node<T> | null, public readonly reversed: boolean) {
super();
}
get size() {
if (this._startNode === this._list._firstNode && this._endNode === this._list._lastNode) {
return this._list.size;
}
let count = 0;
let node = this._startNode;
while (node !== null) {
count++;
if (node === this._endNode) {
break;
}
node = node.next;
}
return count;
}
public reverse() {
return new SingleLinkedListRange<T>(this._list, this._startNode, this._endNode, !this.reversed);
}
public *[Symbol.iterator]() {
if (!this.reversed) {
let node = this._startNode;
while (node !== null) {
yield node.value;
if (node === this._endNode) {
break;
}
node = node.next;
}
} else {
let node = this._endNode;
while (node !== null) {
yield node.value;
if (node === this._startNode) {
break;
}
node = this._list._findPrev(node);
}
}
}
public *cursors() {
if (!this.reversed) {
let node = this._startNode;
while (node !== null) {
yield new SingleLinkedListCursor<T>(this._list, node);
if (node === this._endNode) {
break;
}
node = node.next;
}
} else {
let node = this._endNode;
while (node !== null) {
yield new SingleLinkedListCursor<T>(this._list, node);
if (node === this._startNode) {
break;
}
node = this._list._findPrev(node);
}
}
}
}
/**
* A singly-linked list, which is sometimes slower than a doubly-linked list
* but consumes less memory.
*
* ```ts
* import { SingleLinkedList } from "scl"
* ```
*
* The following table summarises the time complexity of the most commonly used
* properties.
*
* | Property name | Worst-case |
* |------------------------------------------------------|------------|
* | {@link SingleLinkedList.append append()} | O(n) |
* | {@link SingleLinkedList.insertAfter insertAfter()} | O(1) |
* | {@link SingleLinkedList.insertBefore insertBefore()} | O(n) |
* | {@link SingleLinkedList.deleteAt deleteAt()} | O(n) |
* | {@link SingleLinkedList.prepend prepend()} | O(1) |
* | {@link SingleLinkedList.size size} | O(1) |
*
* Some remarks:
*
* - Deleting at the beginning of a singly-linked list is guaranteed to be in `O(1)`.
*
* @see [[DoubleLinkedList]]
* @see [[Vector]]
*
* @typeparam T The type of element in this collection.
*/
export class SingleLinkedList<T> implements List<T> {
/**
* @ignore
*/
public _firstNode: Node<T> | null = null;
/**
* @ignore
*/
public _lastNode: Node<T> | null = null;
/**
* @ignore
*/
public _size = 0;
/**
* Construct a singly-linked list.
*
* ```ts
* const l = new SingleLinkedList();
* ```
*
* You can also constrcut a linked list from any iterable, like so:
*
* ```ts
* const l = new SingleinkedList([1, 2, 3])
* ```
*/
constructor(iterable?: Iterable<T>) {
if (iterable !== undefined) {
for (const element of iterable) {
this.append(element);
}
}
}
/**
* @ignore
*/
public _findPrev(node: Node<T>) {
let prev = this._firstNode;
while (prev !== null && prev.next !== node) {
prev = prev.next;
}
return prev;
}
public add(element: T): [boolean, SingleLinkedListCursor<T>] {
return [true, this.prepend(element)];
}
public getAt(index: number) {
return this.at(index).value;
}
public first() {
if (this._firstNode === null) {
throw new Error(`Cannot get first element: collection is empty.`);
}
return this._firstNode.value;
}
public last() {
if (this._lastNode === null) {
throw new Error(`Cannot get last element: collection is empty.`);
}
return this._lastNode.value;
}
public insertBefore(pos: SingleLinkedListCursor<T>, el: T) {
const newNode = { next: pos._node, value: el };
if (pos._node === this._firstNode) {
this._firstNode = newNode;
} else {
const prev = this._findPrev(pos._node)!;
prev.next = newNode;
}
++this._size;
return new SingleLinkedListCursor<T>(this, newNode);
}
public insertAfter(pos: SingleLinkedListCursor<T>, el: T) {
const newNode = { value: el, next: pos._node.next };
if (pos._node.next === null) {
this._lastNode = newNode;
}
pos._node.next = newNode;
++this._size;
return new SingleLinkedListCursor<T>(this, newNode);
}
public prepend(el: T) {
const newNode = { next: this._firstNode, value: el };
this._firstNode = newNode;
if (this._lastNode === null) {
this._lastNode = newNode;
}
++this._size;
return new SingleLinkedListCursor<T>(this, newNode);
}
public append(el: T) {
const newNode = { next: null, value: el };
if (this._lastNode === null) {
this._firstNode = newNode;
this._lastNode = newNode;
} else {
this._lastNode.next = newNode;
this._lastNode = newNode;
}
++this._size;
return new SingleLinkedListCursor<T>(this, newNode);
}
get size() {
return this._size;
}
public has(el: T) {
let node = this._firstNode;
while (node !== null) {
if (node.value === el) {
return true;
}
node = node.next;
}
return false;
}
public *[Symbol.iterator](): IterableIterator<T> {
let node = this._firstNode;
while (node !== null) {
yield node.value;
node = node.next;
}
}
public at(position: number) {
let curr = this._firstNode;
let i = position;
while (i > 0) {
if (curr === null) {
throw new RangeError(`Could not get element at i ${position}: index out of bounds.`);
}
curr = curr.next;
--i;
}
if (curr === null) {
throw new RangeError(`Could not get element at position ${position}: index out of bounds.`);
}
return new SingleLinkedListCursor<T>(this, curr);
}
public deleteAt(pos: SingleLinkedListCursor<T>) {
const prev = this._findPrev(pos._node)
, next = pos._node.next;
if (prev === null) {
this._firstNode = next;
} else {
prev.next = next;
}
if (next === null) {
this._lastNode = prev;
}
--this._size;
}
public delete(element: T): boolean {
let node: Node<T> | null = this._firstNode;
let prev = null;
while (node !== null) {
if (node.value === element) {
const next = node.next;
if (prev === null) {
this._firstNode = next;
} else {
prev.next = node.next;
}
if (next === null) {
this._lastNode = prev;
}
this._size--;
return true;
}
prev = node;
node = node.next;
}
return false;
}
public deleteAll(el: T): number {
let count = 0;
let node: Node<T> | null = this._firstNode;
let prev = null;
while (node !== null) {
if (node.value === el) {
node = node.next;
this._size--;
while (node !== null && node.value === el) {
this._size--;
node = node.next;
}
if (prev === null) {
this._firstNode = node;
} else {
prev.next = node;
}
if (node === null) {
this._lastNode = prev;
}
count++;
} else {
prev = node;
node = node.next;
}
}
return count;
}
public toRange() {
return new SingleLinkedListRange<T>(this, this._firstNode, this._lastNode, false);
}
public rest(): List<T> {
if (this._firstNode === null) {
throw new Error(`Could not get rest of list: list is empty.`);
}
const list = new SingleLinkedList<T>();
list._firstNode = this._firstNode.next;
list._lastNode = this._lastNode;
list._size = this._size - 1;
return list;
}
public clone() {
return new SingleLinkedList<T>(this);
}
public clear() {
this._firstNode = null;
this._lastNode = null;
this._size = 0;
}
}
export default SingleLinkedList;