data-structure-typed 2.5.3 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +1 -1
- package/MIGRATION.md +48 -0
- package/README.md +20 -2
- package/README_CN.md +20 -2
- package/SPECIFICATION.md +24 -0
- package/SPECIFICATION.zh-CN.md +24 -0
- package/dist/cjs/binary-tree.cjs +1897 -19
- package/dist/cjs/graph.cjs +174 -0
- package/dist/cjs/hash.cjs +33 -0
- package/dist/cjs/heap.cjs +71 -0
- package/dist/cjs/index.cjs +2383 -3
- package/dist/cjs/linked-list.cjs +224 -2
- package/dist/cjs/matrix.cjs +24 -0
- package/dist/cjs/priority-queue.cjs +71 -0
- package/dist/cjs/queue.cjs +221 -1
- package/dist/cjs/stack.cjs +59 -0
- package/dist/cjs/trie.cjs +62 -0
- package/dist/cjs-legacy/binary-tree.cjs +1897 -19
- package/dist/cjs-legacy/graph.cjs +174 -0
- package/dist/cjs-legacy/hash.cjs +33 -0
- package/dist/cjs-legacy/heap.cjs +71 -0
- package/dist/cjs-legacy/index.cjs +2383 -3
- package/dist/cjs-legacy/linked-list.cjs +224 -2
- package/dist/cjs-legacy/matrix.cjs +24 -0
- package/dist/cjs-legacy/priority-queue.cjs +71 -0
- package/dist/cjs-legacy/queue.cjs +221 -1
- package/dist/cjs-legacy/stack.cjs +59 -0
- package/dist/cjs-legacy/trie.cjs +62 -0
- package/dist/esm/binary-tree.mjs +1897 -19
- package/dist/esm/graph.mjs +174 -0
- package/dist/esm/hash.mjs +33 -0
- package/dist/esm/heap.mjs +71 -0
- package/dist/esm/index.mjs +2383 -3
- package/dist/esm/linked-list.mjs +224 -2
- package/dist/esm/matrix.mjs +24 -0
- package/dist/esm/priority-queue.mjs +71 -0
- package/dist/esm/queue.mjs +221 -1
- package/dist/esm/stack.mjs +59 -0
- package/dist/esm/trie.mjs +62 -0
- package/dist/esm-legacy/binary-tree.mjs +1897 -19
- package/dist/esm-legacy/graph.mjs +174 -0
- package/dist/esm-legacy/hash.mjs +33 -0
- package/dist/esm-legacy/heap.mjs +71 -0
- package/dist/esm-legacy/index.mjs +2383 -3
- package/dist/esm-legacy/linked-list.mjs +224 -2
- package/dist/esm-legacy/matrix.mjs +24 -0
- package/dist/esm-legacy/priority-queue.mjs +71 -0
- package/dist/esm-legacy/queue.mjs +221 -1
- package/dist/esm-legacy/stack.mjs +59 -0
- package/dist/esm-legacy/trie.mjs +62 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +90 -1
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/umd/data-structure-typed.js +2383 -3
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/jest.integration.config.js +1 -2
- package/package.json +51 -50
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +32 -3
- package/src/data-structures/base/linear-base.ts +13 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -493
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
- package/src/data-structures/binary-tree/bst.ts +158 -1010
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
- package/src/data-structures/binary-tree/segment-tree.ts +73 -333
- package/src/data-structures/binary-tree/tree-map.ts +462 -4749
- package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
- package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
- package/src/data-structures/binary-tree/tree-set.ts +437 -4443
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -532
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -484
- package/src/data-structures/hash/hash-map.ts +154 -549
- package/src/data-structures/heap/heap.ts +200 -753
- package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
- package/src/data-structures/matrix/matrix.ts +179 -494
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +241 -807
- package/src/data-structures/queue/queue.ts +102 -589
- package/src/data-structures/stack/stack.ts +76 -475
- package/src/data-structures/trie/trie.ts +98 -592
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
|
|
10
9
|
import { LinearLinkedBase, LinkedListNode } from '../base/linear-base';
|
|
11
10
|
|
|
@@ -21,7 +20,6 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
21
20
|
* @param value - Element value to store.
|
|
22
21
|
* @returns New node instance.
|
|
23
22
|
*/
|
|
24
|
-
|
|
25
23
|
constructor(value: E) {
|
|
26
24
|
super(value);
|
|
27
25
|
this._value = value;
|
|
@@ -35,7 +33,6 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
35
33
|
* @remarks Time O(1), Space O(1)
|
|
36
34
|
* @returns Next node or undefined.
|
|
37
35
|
*/
|
|
38
|
-
|
|
39
36
|
override get next(): SinglyLinkedListNode<E> | undefined {
|
|
40
37
|
return this._next;
|
|
41
38
|
}
|
|
@@ -46,7 +43,6 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
46
43
|
* @param value - Next node or undefined.
|
|
47
44
|
* @returns void
|
|
48
45
|
*/
|
|
49
|
-
|
|
50
46
|
override set next(value: SinglyLinkedListNode<E> | undefined) {
|
|
51
47
|
this._next = value;
|
|
52
48
|
}
|
|
@@ -192,8 +188,6 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
192
188
|
* console.log(result); // [10, 20, 30];
|
|
193
189
|
*/
|
|
194
190
|
export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
|
|
195
|
-
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
196
|
-
|
|
197
191
|
/**
|
|
198
192
|
* Create a SinglyLinkedList and optionally bulk-insert elements.
|
|
199
193
|
* @remarks Time O(N), Space O(N)
|
|
@@ -201,7 +195,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
201
195
|
* @param [options] - Options such as maxLen and toElementFn.
|
|
202
196
|
* @returns New SinglyLinkedList instance.
|
|
203
197
|
*/
|
|
204
|
-
|
|
205
198
|
constructor(
|
|
206
199
|
elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>> = [],
|
|
207
200
|
options?: SinglyLinkedListOptions<E, R>
|
|
@@ -217,7 +210,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
217
210
|
* @remarks Time O(1), Space O(1)
|
|
218
211
|
* @returns Head node or undefined.
|
|
219
212
|
*/
|
|
220
|
-
|
|
221
213
|
get head(): SinglyLinkedListNode<E> | undefined {
|
|
222
214
|
return this._head;
|
|
223
215
|
}
|
|
@@ -229,7 +221,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
229
221
|
* @remarks Time O(1), Space O(1)
|
|
230
222
|
* @returns Tail node or undefined.
|
|
231
223
|
*/
|
|
232
|
-
|
|
233
224
|
get tail(): SinglyLinkedListNode<E> | undefined {
|
|
234
225
|
return this._tail;
|
|
235
226
|
}
|
|
@@ -241,7 +232,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
241
232
|
* @remarks Time O(1), Space O(1)
|
|
242
233
|
* @returns Current length.
|
|
243
234
|
*/
|
|
244
|
-
|
|
245
235
|
get length(): number {
|
|
246
236
|
return this._length;
|
|
247
237
|
}
|
|
@@ -251,7 +241,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
251
241
|
* @remarks Time O(1), Space O(1)
|
|
252
242
|
* @returns First element or undefined.
|
|
253
243
|
*/
|
|
254
|
-
|
|
255
244
|
get first(): E | undefined {
|
|
256
245
|
return this.head?.value;
|
|
257
246
|
}
|
|
@@ -261,7 +250,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
261
250
|
* @remarks Time O(1), Space O(1)
|
|
262
251
|
* @returns Last element or undefined.
|
|
263
252
|
*/
|
|
264
|
-
|
|
265
253
|
get last(): E | undefined {
|
|
266
254
|
return this.tail?.value;
|
|
267
255
|
}
|
|
@@ -277,7 +265,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
277
265
|
* @param [options] - Options forwarded to the constructor.
|
|
278
266
|
* @returns A new list populated with the iterable's elements.
|
|
279
267
|
*/
|
|
280
|
-
|
|
281
268
|
static from<E, R = any, S extends SinglyLinkedList<E, R> = SinglyLinkedList<E, R>>(
|
|
282
269
|
this: new (
|
|
283
270
|
elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>,
|
|
@@ -296,62 +283,22 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
296
283
|
* @remarks Time O(1), Space O(1)
|
|
297
284
|
* @param elementOrNode - Element or node to append.
|
|
298
285
|
* @returns True when appended.
|
|
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
|
-
* @example
|
|
339
|
-
* // basic SinglyLinkedList creation and push operation
|
|
340
|
-
* // Create a simple SinglyLinkedList with initial values
|
|
341
|
-
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
342
|
-
*
|
|
343
|
-
* // Verify the list maintains insertion order
|
|
344
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
345
|
-
*
|
|
346
|
-
* // Check length
|
|
347
|
-
* console.log(list.length); // 5;
|
|
348
|
-
*
|
|
349
|
-
* // Push a new element to the end
|
|
350
|
-
* list.push(6);
|
|
351
|
-
* console.log(list.length); // 6;
|
|
352
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
286
|
+
* @example
|
|
287
|
+
* // basic SinglyLinkedList creation and push operation
|
|
288
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
289
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
290
|
+
*
|
|
291
|
+
* // Verify the list maintains insertion order
|
|
292
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
293
|
+
*
|
|
294
|
+
* // Check length
|
|
295
|
+
* console.log(list.length); // 5;
|
|
296
|
+
*
|
|
297
|
+
* // Push a new element to the end
|
|
298
|
+
* list.push(6);
|
|
299
|
+
* console.log(list.length); // 6;
|
|
300
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
353
301
|
*/
|
|
354
|
-
|
|
355
302
|
push(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
356
303
|
const newNode = this._ensureNode(elementOrNode);
|
|
357
304
|
if (!this.head) {
|
|
@@ -369,62 +316,22 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
369
316
|
* Remove and return the tail element.
|
|
370
317
|
* @remarks Time O(N), Space O(1)
|
|
371
318
|
* @returns Removed element or undefined.
|
|
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
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
* @example
|
|
412
|
-
* // SinglyLinkedList pop and shift operations
|
|
413
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
414
|
-
*
|
|
415
|
-
* // Pop removes from the end
|
|
416
|
-
* const last = list.pop();
|
|
417
|
-
* console.log(last); // 50;
|
|
418
|
-
*
|
|
419
|
-
* // Shift removes from the beginning
|
|
420
|
-
* const first = list.shift();
|
|
421
|
-
* console.log(first); // 10;
|
|
422
|
-
*
|
|
423
|
-
* // Verify remaining elements
|
|
424
|
-
* console.log([...list]); // [20, 30, 40];
|
|
425
|
-
* console.log(list.length); // 3;
|
|
319
|
+
* @example
|
|
320
|
+
* // SinglyLinkedList pop and shift operations
|
|
321
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
322
|
+
*
|
|
323
|
+
* // Pop removes from the end
|
|
324
|
+
* const last = list.pop();
|
|
325
|
+
* console.log(last); // 50;
|
|
326
|
+
*
|
|
327
|
+
* // Shift removes from the beginning
|
|
328
|
+
* const first = list.shift();
|
|
329
|
+
* console.log(first); // 10;
|
|
330
|
+
*
|
|
331
|
+
* // Verify remaining elements
|
|
332
|
+
* console.log([...list]); // [20, 30, 40];
|
|
333
|
+
* console.log(list.length); // 3;
|
|
426
334
|
*/
|
|
427
|
-
|
|
428
335
|
pop(): E | undefined {
|
|
429
336
|
if (!this.head) return undefined;
|
|
430
337
|
if (this.head === this.tail) {
|
|
@@ -447,52 +354,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
447
354
|
* Remove and return the head element.
|
|
448
355
|
* @remarks Time O(1), Space O(1)
|
|
449
356
|
* @returns Removed element or undefined.
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
* @example
|
|
490
|
-
* // Remove from the front
|
|
491
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
492
|
-
* console.log(list.shift()); // 10;
|
|
493
|
-
* console.log(list.length); // 2;
|
|
357
|
+
* @example
|
|
358
|
+
* // Remove from the front
|
|
359
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
360
|
+
* console.log(list.shift()); // 10;
|
|
361
|
+
* console.log(list.length); // 2;
|
|
494
362
|
*/
|
|
495
|
-
|
|
496
363
|
shift(): E | undefined {
|
|
497
364
|
if (!this.head) return undefined;
|
|
498
365
|
const removed = this.head;
|
|
@@ -507,67 +374,27 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
507
374
|
* @remarks Time O(1), Space O(1)
|
|
508
375
|
* @param elementOrNode - Element or node to prepend.
|
|
509
376
|
* @returns True when prepended.
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
* @example
|
|
550
|
-
* // SinglyLinkedList unshift and forward traversal
|
|
551
|
-
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
552
|
-
*
|
|
553
|
-
* // Unshift adds to the beginning
|
|
554
|
-
* list.unshift(10);
|
|
555
|
-
* console.log([...list]); // [10, 20, 30, 40];
|
|
556
|
-
*
|
|
557
|
-
* // Access elements (forward traversal only for singly linked)
|
|
558
|
-
* const second = list.at(1);
|
|
559
|
-
* console.log(second); // 20;
|
|
560
|
-
*
|
|
561
|
-
* // SinglyLinkedList allows forward iteration only
|
|
562
|
-
* const elements: number[] = [];
|
|
563
|
-
* for (const item of list) {
|
|
564
|
-
* elements.push(item);
|
|
565
|
-
* }
|
|
566
|
-
* console.log(elements); // [10, 20, 30, 40];
|
|
567
|
-
*
|
|
568
|
-
* console.log(list.length); // 4;
|
|
377
|
+
* @example
|
|
378
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
379
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
380
|
+
*
|
|
381
|
+
* // Unshift adds to the beginning
|
|
382
|
+
* list.unshift(10);
|
|
383
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
384
|
+
*
|
|
385
|
+
* // Access elements (forward traversal only for singly linked)
|
|
386
|
+
* const second = list.at(1);
|
|
387
|
+
* console.log(second); // 20;
|
|
388
|
+
*
|
|
389
|
+
* // SinglyLinkedList allows forward iteration only
|
|
390
|
+
* const elements: number[] = [];
|
|
391
|
+
* for (const item of list) {
|
|
392
|
+
* elements.push(item);
|
|
393
|
+
* }
|
|
394
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
395
|
+
*
|
|
396
|
+
* console.log(list.length); // 4;
|
|
569
397
|
*/
|
|
570
|
-
|
|
571
398
|
unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
572
399
|
const newNode = this._ensureNode(elementOrNode);
|
|
573
400
|
if (!this.head) {
|
|
@@ -586,7 +413,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
586
413
|
* @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
587
414
|
* @returns Array of per-element success flags.
|
|
588
415
|
*/
|
|
589
|
-
|
|
590
416
|
pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[] {
|
|
591
417
|
const ans: boolean[] = [];
|
|
592
418
|
for (const el of elements) {
|
|
@@ -602,7 +428,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
602
428
|
* @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
|
|
603
429
|
* @returns Array of per-element success flags.
|
|
604
430
|
*/
|
|
605
|
-
|
|
606
431
|
unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[] {
|
|
607
432
|
const ans: boolean[] = [];
|
|
608
433
|
for (const el of elements) {
|
|
@@ -618,7 +443,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
618
443
|
* @param elementNodeOrPredicate - Element, node, or node predicate to match.
|
|
619
444
|
* @returns Matched value or undefined.
|
|
620
445
|
*/
|
|
621
|
-
|
|
622
446
|
search(
|
|
623
447
|
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
624
448
|
): E | undefined {
|
|
@@ -636,53 +460,13 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
636
460
|
* @remarks Time O(N), Space O(1)
|
|
637
461
|
* @param index - Zero-based index.
|
|
638
462
|
* @returns Element or undefined.
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
* @example
|
|
679
|
-
* // Access element by index
|
|
680
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
681
|
-
* console.log(list.at(0)); // 'a';
|
|
682
|
-
* console.log(list.at(2)); // 'c';
|
|
683
|
-
* console.log(list.at(3)); // 'd';
|
|
463
|
+
* @example
|
|
464
|
+
* // Access element by index
|
|
465
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
466
|
+
* console.log(list.at(0)); // 'a';
|
|
467
|
+
* console.log(list.at(2)); // 'c';
|
|
468
|
+
* console.log(list.at(3)); // 'd';
|
|
684
469
|
*/
|
|
685
|
-
|
|
686
470
|
at(index: number): E | undefined {
|
|
687
471
|
if (index < 0 || index >= this._length) return undefined;
|
|
688
472
|
let current = this.head;
|
|
@@ -696,7 +480,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
696
480
|
* @param elementNodeOrPredicate - Element, node, or predicate.
|
|
697
481
|
* @returns True if the value is a SinglyLinkedListNode.
|
|
698
482
|
*/
|
|
699
|
-
|
|
700
483
|
isNode(
|
|
701
484
|
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
702
485
|
): elementNodeOrPredicate is SinglyLinkedListNode<E> {
|
|
@@ -708,48 +491,11 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
708
491
|
* @remarks Time O(N), Space O(1)
|
|
709
492
|
* @param index - Zero-based index.
|
|
710
493
|
* @returns Node or undefined.
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
* @example
|
|
748
|
-
* // Get node at index
|
|
749
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
750
|
-
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
494
|
+
* @example
|
|
495
|
+
* // Get node at index
|
|
496
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
497
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
751
498
|
*/
|
|
752
|
-
|
|
753
499
|
getNodeAt(index: number): SinglyLinkedListNode<E> | undefined {
|
|
754
500
|
if (index < 0 || index >= this._length) return undefined;
|
|
755
501
|
let current = this.head;
|
|
@@ -762,49 +508,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
762
508
|
* @remarks Time O(N), Space O(1)
|
|
763
509
|
* @param index - Zero-based index.
|
|
764
510
|
* @returns Removed element or undefined.
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
* @example
|
|
802
|
-
* // Remove by index
|
|
803
|
-
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
804
|
-
* list.deleteAt(1);
|
|
805
|
-
* console.log(list.toArray()); // ['a', 'c'];
|
|
511
|
+
* @example
|
|
512
|
+
* // Remove by index
|
|
513
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
514
|
+
* list.deleteAt(1);
|
|
515
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
806
516
|
*/
|
|
807
|
-
|
|
808
517
|
deleteAt(index: number): E | undefined {
|
|
809
518
|
if (index < 0 || index >= this._length) return undefined;
|
|
810
519
|
if (index === 0) return this.shift();
|
|
@@ -822,55 +531,17 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
822
531
|
* @remarks Time O(N), Space O(1)
|
|
823
532
|
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
824
533
|
* @returns True if removed.
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
* @example
|
|
862
|
-
* // Remove first occurrence
|
|
863
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
864
|
-
* list.delete(2);
|
|
865
|
-
* console.log(list.toArray()); // [1, 3, 2];
|
|
534
|
+
* @example
|
|
535
|
+
* // Remove first occurrence
|
|
536
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
537
|
+
* list.delete(2);
|
|
538
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
866
539
|
*/
|
|
867
|
-
|
|
868
540
|
delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
|
|
869
541
|
if (elementOrNode === undefined || !this.head) return false;
|
|
870
542
|
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
871
543
|
if (!node) return false;
|
|
872
544
|
const prevNode = this._getPrevNode(node);
|
|
873
|
-
|
|
874
545
|
if (!prevNode) {
|
|
875
546
|
this._head = node.next;
|
|
876
547
|
if (node === this.tail) this._tail = undefined;
|
|
@@ -888,49 +559,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
888
559
|
* @param index - Zero-based index.
|
|
889
560
|
* @param newElementOrNode - Element or node to insert.
|
|
890
561
|
* @returns True if inserted.
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
* @example
|
|
928
|
-
* // Insert at index
|
|
929
|
-
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
930
|
-
* list.addAt(1, 2);
|
|
931
|
-
* console.log(list.toArray()); // [1, 2, 3];
|
|
562
|
+
* @example
|
|
563
|
+
* // Insert at index
|
|
564
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
565
|
+
* list.addAt(1, 2);
|
|
566
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
932
567
|
*/
|
|
933
|
-
|
|
934
568
|
addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
935
569
|
if (index < 0 || index > this._length) return false;
|
|
936
570
|
if (index === 0) return this.unshift(newElementOrNode);
|
|
@@ -950,7 +584,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
950
584
|
* @param value - New value.
|
|
951
585
|
* @returns True if updated.
|
|
952
586
|
*/
|
|
953
|
-
|
|
954
587
|
setAt(index: number, value: E): boolean {
|
|
955
588
|
const node = this.getNodeAt(index);
|
|
956
589
|
if (!node) return false;
|
|
@@ -962,48 +595,10 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
962
595
|
* Check whether the list is empty.
|
|
963
596
|
* @remarks Time O(1), Space O(1)
|
|
964
597
|
* @returns True if length is 0.
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
* @example
|
|
1003
|
-
* // Check empty
|
|
1004
|
-
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
598
|
+
* @example
|
|
599
|
+
* // Check empty
|
|
600
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
1005
601
|
*/
|
|
1006
|
-
|
|
1007
602
|
isEmpty(): boolean {
|
|
1008
603
|
return this._length === 0;
|
|
1009
604
|
}
|
|
@@ -1012,50 +607,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1012
607
|
* Remove all nodes and reset length.
|
|
1013
608
|
* @remarks Time O(N), Space O(1)
|
|
1014
609
|
* @returns void
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
* @example
|
|
1053
|
-
* // Remove all
|
|
1054
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1055
|
-
* list.clear();
|
|
1056
|
-
* console.log(list.isEmpty()); // true;
|
|
610
|
+
* @example
|
|
611
|
+
* // Remove all
|
|
612
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
613
|
+
* list.clear();
|
|
614
|
+
* console.log(list.isEmpty()); // true;
|
|
1057
615
|
*/
|
|
1058
|
-
|
|
1059
616
|
clear(): void {
|
|
1060
617
|
this._head = undefined;
|
|
1061
618
|
this._tail = undefined;
|
|
@@ -1066,52 +623,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1066
623
|
* Reverse the list in place.
|
|
1067
624
|
* @remarks Time O(N), Space O(1)
|
|
1068
625
|
* @returns This list.
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
* @example
|
|
1109
|
-
* // Reverse the list in-place
|
|
1110
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1111
|
-
* list.reverse();
|
|
1112
|
-
* console.log([...list]); // [4, 3, 2, 1];
|
|
626
|
+
* @example
|
|
627
|
+
* // Reverse the list in-place
|
|
628
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
629
|
+
* list.reverse();
|
|
630
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
1113
631
|
*/
|
|
1114
|
-
|
|
1115
632
|
reverse(): this {
|
|
1116
633
|
if (!this.head || this.head === this.tail) return this;
|
|
1117
634
|
let prev: SinglyLinkedListNode<E> | undefined;
|
|
@@ -1133,7 +650,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1133
650
|
* @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
|
|
1134
651
|
* @returns Matching node or undefined.
|
|
1135
652
|
*/
|
|
1136
|
-
|
|
1137
653
|
getNode(
|
|
1138
654
|
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined
|
|
1139
655
|
): SinglyLinkedListNode<E> | undefined {
|
|
@@ -1155,7 +671,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1155
671
|
* @param newElementOrNode - Element or node to insert.
|
|
1156
672
|
* @returns True if inserted.
|
|
1157
673
|
*/
|
|
1158
|
-
|
|
1159
674
|
addBefore(
|
|
1160
675
|
existingElementOrNode: E | SinglyLinkedListNode<E>,
|
|
1161
676
|
newElementOrNode: E | SinglyLinkedListNode<E>
|
|
@@ -1164,7 +679,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1164
679
|
if (!existingNode) return false;
|
|
1165
680
|
const prevNode = this._getPrevNode(existingNode);
|
|
1166
681
|
const newNode = this._ensureNode(newElementOrNode);
|
|
1167
|
-
|
|
1168
682
|
if (!prevNode) {
|
|
1169
683
|
newNode.next = this._head;
|
|
1170
684
|
this._head = newNode;
|
|
@@ -1185,7 +699,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1185
699
|
* @param newElementOrNode - Element or node to insert.
|
|
1186
700
|
* @returns True if inserted.
|
|
1187
701
|
*/
|
|
1188
|
-
|
|
1189
702
|
addAfter(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
1190
703
|
const existingNode = this.getNode(existingElementOrNode);
|
|
1191
704
|
if (!existingNode) return false;
|
|
@@ -1205,16 +718,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1205
718
|
* @param [items] - Elements to insert after `start`.
|
|
1206
719
|
* @returns A new list containing the removed elements (typed as `this`).
|
|
1207
720
|
*/
|
|
1208
|
-
|
|
1209
721
|
override splice(start: number, deleteCount = 0, ...items: E[]): this {
|
|
1210
722
|
start = Math.max(0, Math.min(start, this.length));
|
|
1211
723
|
deleteCount = Math.max(0, deleteCount);
|
|
1212
|
-
|
|
1213
724
|
const removedList = this._createInstance();
|
|
1214
|
-
|
|
1215
725
|
const prevNode = start === 0 ? undefined : this.getNodeAt(start - 1);
|
|
1216
726
|
let cur = prevNode ? prevNode.next : this.head;
|
|
1217
|
-
|
|
1218
727
|
let removedCount = 0;
|
|
1219
728
|
while (removedCount < deleteCount && cur) {
|
|
1220
729
|
removedList.push(cur.value);
|
|
@@ -1222,14 +731,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1222
731
|
removedCount++;
|
|
1223
732
|
}
|
|
1224
733
|
const afterNode = cur;
|
|
1225
|
-
|
|
1226
734
|
if (prevNode) {
|
|
1227
735
|
prevNode.next = afterNode;
|
|
1228
736
|
} else {
|
|
1229
737
|
this._head = afterNode;
|
|
1230
738
|
}
|
|
1231
739
|
if (!afterNode) this._tail = prevNode;
|
|
1232
|
-
|
|
1233
740
|
if (items.length > 0) {
|
|
1234
741
|
let firstInserted: SinglyLinkedListNode<E> | undefined;
|
|
1235
742
|
let lastInserted: SinglyLinkedListNode<E> | undefined;
|
|
@@ -1241,17 +748,14 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1241
748
|
}
|
|
1242
749
|
if (prevNode) prevNode.next = firstInserted!;
|
|
1243
750
|
else this._head = firstInserted!;
|
|
1244
|
-
|
|
1245
751
|
lastInserted!.next = afterNode;
|
|
1246
752
|
if (!afterNode) this._tail = lastInserted!;
|
|
1247
753
|
}
|
|
1248
|
-
|
|
1249
754
|
this._length += items.length - removedCount;
|
|
1250
755
|
if (this._length === 0) {
|
|
1251
756
|
this._head = undefined;
|
|
1252
757
|
this._tail = undefined;
|
|
1253
758
|
}
|
|
1254
|
-
|
|
1255
759
|
return removedList as unknown as this;
|
|
1256
760
|
}
|
|
1257
761
|
|
|
@@ -1261,7 +765,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1261
765
|
* @param elementOrNode - Element, node, or node predicate to match.
|
|
1262
766
|
* @returns Number of matches in the list.
|
|
1263
767
|
*/
|
|
1264
|
-
|
|
1265
768
|
countOccurrences(elementOrNode: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number {
|
|
1266
769
|
const predicate = elementOrPredicate(elementOrNode, this._equals);
|
|
1267
770
|
let count = 0;
|
|
@@ -1279,7 +782,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1279
782
|
* @param equals - Equality predicate (a, b) → boolean.
|
|
1280
783
|
* @returns This list.
|
|
1281
784
|
*/
|
|
1282
|
-
|
|
1283
785
|
setEquality(equals: (a: E, b: E) => boolean): this {
|
|
1284
786
|
this._equals = equals;
|
|
1285
787
|
return this;
|
|
@@ -1291,7 +793,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1291
793
|
* @param predicate - Predicate (value, index, list) → boolean to decide deletion.
|
|
1292
794
|
* @returns True if a node was removed.
|
|
1293
795
|
*/
|
|
1294
|
-
|
|
1295
796
|
deleteWhere(predicate: (value: E, index: number, list: this) => boolean): boolean {
|
|
1296
797
|
let prev: SinglyLinkedListNode<E> | undefined;
|
|
1297
798
|
let current = this.head;
|
|
@@ -1318,52 +819,14 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1318
819
|
* Deep clone this list (values are copied by reference).
|
|
1319
820
|
* @remarks Time O(N), Space O(N)
|
|
1320
821
|
* @returns A new list with the same element sequence.
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
* @example
|
|
1359
|
-
* // Deep copy
|
|
1360
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1361
|
-
* const copy = list.clone();
|
|
1362
|
-
* copy.pop();
|
|
1363
|
-
* console.log(list.length); // 3;
|
|
1364
|
-
* console.log(copy.length); // 2;
|
|
822
|
+
* @example
|
|
823
|
+
* // Deep copy
|
|
824
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
825
|
+
* const copy = list.clone();
|
|
826
|
+
* copy.pop();
|
|
827
|
+
* console.log(list.length); // 3;
|
|
828
|
+
* console.log(copy.length); // 2;
|
|
1365
829
|
*/
|
|
1366
|
-
|
|
1367
830
|
clone(): this {
|
|
1368
831
|
const out = this._createInstance();
|
|
1369
832
|
for (const v of this) out.push(v);
|
|
@@ -1376,62 +839,22 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1376
839
|
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
1377
840
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1378
841
|
* @returns A new list with kept values.
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
* @example
|
|
1419
|
-
* // SinglyLinkedList filter and map operations
|
|
1420
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1421
|
-
*
|
|
1422
|
-
* // Filter even numbers
|
|
1423
|
-
* const filtered = list.filter(value => value % 2 === 0);
|
|
1424
|
-
* console.log(filtered.length); // 2;
|
|
1425
|
-
*
|
|
1426
|
-
* // Map to double values
|
|
1427
|
-
* const doubled = list.map(value => value * 2);
|
|
1428
|
-
* console.log(doubled.length); // 5;
|
|
1429
|
-
*
|
|
1430
|
-
* // Use reduce to sum
|
|
1431
|
-
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1432
|
-
* console.log(sum); // 15;
|
|
842
|
+
* @example
|
|
843
|
+
* // SinglyLinkedList filter and map operations
|
|
844
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
845
|
+
*
|
|
846
|
+
* // Filter even numbers
|
|
847
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
848
|
+
* console.log(filtered.length); // 2;
|
|
849
|
+
*
|
|
850
|
+
* // Map to double values
|
|
851
|
+
* const doubled = list.map(value => value * 2);
|
|
852
|
+
* console.log(doubled.length); // 5;
|
|
853
|
+
*
|
|
854
|
+
* // Use reduce to sum
|
|
855
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
856
|
+
* console.log(sum); // 15;
|
|
1433
857
|
*/
|
|
1434
|
-
|
|
1435
858
|
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
1436
859
|
const out = this._createInstance();
|
|
1437
860
|
let index = 0;
|
|
@@ -1446,7 +869,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1446
869
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1447
870
|
* @returns A new list with mapped values.
|
|
1448
871
|
*/
|
|
1449
|
-
|
|
1450
872
|
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
|
|
1451
873
|
const out = this._createInstance();
|
|
1452
874
|
let index = 0;
|
|
@@ -1466,52 +888,12 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1466
888
|
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
1467
889
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1468
890
|
* @returns A new SinglyLinkedList with mapped values.
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
* @example
|
|
1509
|
-
* // Transform elements
|
|
1510
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1511
|
-
* const doubled = list.map(n => n * 2);
|
|
1512
|
-
* console.log([...doubled]); // [2, 4, 6];
|
|
891
|
+
* @example
|
|
892
|
+
* // Transform elements
|
|
893
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
894
|
+
* const doubled = list.map(n => n * 2);
|
|
895
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
1513
896
|
*/
|
|
1514
|
-
|
|
1515
897
|
map<EM, RM = any>(
|
|
1516
898
|
callback: ElementCallback<E, R, EM>,
|
|
1517
899
|
options?: SinglyLinkedListOptions<EM, RM>,
|
|
@@ -1523,13 +905,14 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1523
905
|
return out;
|
|
1524
906
|
}
|
|
1525
907
|
|
|
908
|
+
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
909
|
+
|
|
1526
910
|
/**
|
|
1527
911
|
* (Protected) Create a node from a value.
|
|
1528
912
|
* @remarks Time O(1), Space O(1)
|
|
1529
913
|
* @param value - Value to wrap in a node.
|
|
1530
914
|
* @returns A new SinglyLinkedListNode instance.
|
|
1531
915
|
*/
|
|
1532
|
-
|
|
1533
916
|
protected createNode(value: E): SinglyLinkedListNode<E> {
|
|
1534
917
|
return new SinglyLinkedListNode<E>(value);
|
|
1535
918
|
}
|
|
@@ -1540,7 +923,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1540
923
|
* @param elementNodeOrPredicate - Element, node, or node predicate.
|
|
1541
924
|
* @returns True if input is a predicate function.
|
|
1542
925
|
*/
|
|
1543
|
-
|
|
1544
926
|
protected _isPredicate(
|
|
1545
927
|
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
1546
928
|
): elementNodeOrPredicate is (node: SinglyLinkedListNode<E>) => boolean {
|
|
@@ -1553,7 +935,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1553
935
|
* @param elementOrNode - Element or node.
|
|
1554
936
|
* @returns A SinglyLinkedListNode for the provided input.
|
|
1555
937
|
*/
|
|
1556
|
-
|
|
1557
938
|
protected _ensureNode(elementOrNode: E | SinglyLinkedListNode<E>) {
|
|
1558
939
|
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
1559
940
|
return this.createNode(elementOrNode);
|
|
@@ -1565,7 +946,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1565
946
|
* @param elementNodeOrPredicate - Element, node, or predicate.
|
|
1566
947
|
* @returns A predicate taking a node and returning true/false.
|
|
1567
948
|
*/
|
|
1568
|
-
|
|
1569
949
|
protected _ensurePredicate(
|
|
1570
950
|
elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)
|
|
1571
951
|
) {
|
|
@@ -1581,7 +961,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1581
961
|
* @param node - A node in the list.
|
|
1582
962
|
* @returns Previous node or undefined.
|
|
1583
963
|
*/
|
|
1584
|
-
|
|
1585
964
|
protected _getPrevNode(node: SinglyLinkedListNode<E>): SinglyLinkedListNode<E> | undefined {
|
|
1586
965
|
if (!this.head || this.head === node) return undefined;
|
|
1587
966
|
let current = this.head;
|
|
@@ -1594,7 +973,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1594
973
|
* @remarks Time O(N), Space O(1)
|
|
1595
974
|
* @returns Iterator of values (E).
|
|
1596
975
|
*/
|
|
1597
|
-
|
|
1598
976
|
protected *_getIterator(): IterableIterator<E> {
|
|
1599
977
|
let current = this.head;
|
|
1600
978
|
while (current) {
|
|
@@ -1608,7 +986,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1608
986
|
* @remarks Time O(N), Space O(N)
|
|
1609
987
|
* @returns Iterator of values (E).
|
|
1610
988
|
*/
|
|
1611
|
-
|
|
1612
989
|
protected *_getReverseIterator(): IterableIterator<E> {
|
|
1613
990
|
const reversedArr = [...this].reverse();
|
|
1614
991
|
for (const item of reversedArr) yield item;
|
|
@@ -1619,7 +996,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1619
996
|
* @remarks Time O(N), Space O(1)
|
|
1620
997
|
* @returns Iterator of nodes.
|
|
1621
998
|
*/
|
|
1622
|
-
|
|
1623
999
|
protected *_getNodeIterator(): IterableIterator<SinglyLinkedListNode<E>> {
|
|
1624
1000
|
let current = this.head;
|
|
1625
1001
|
while (current) {
|
|
@@ -1634,7 +1010,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1634
1010
|
* @param [options] - Options forwarded to the constructor.
|
|
1635
1011
|
* @returns An empty like-kind list instance.
|
|
1636
1012
|
*/
|
|
1637
|
-
|
|
1638
1013
|
protected _createInstance(options?: SinglyLinkedListOptions<E, R>): this {
|
|
1639
1014
|
const Ctor = this.constructor as new (
|
|
1640
1015
|
elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>,
|
|
@@ -1652,7 +1027,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1652
1027
|
* @param [options] - Options forwarded to the constructor.
|
|
1653
1028
|
* @returns A like-kind SinglyLinkedList instance.
|
|
1654
1029
|
*/
|
|
1655
|
-
|
|
1656
1030
|
protected _createLike<EM, RM>(
|
|
1657
1031
|
elements: Iterable<EM> | Iterable<RM> | Iterable<SinglyLinkedListNode<EM>> = [],
|
|
1658
1032
|
options?: SinglyLinkedListOptions<EM, RM>
|
|
@@ -1672,7 +1046,6 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
1672
1046
|
* @param [options] - Options forwarded to the constructor.
|
|
1673
1047
|
* @returns An empty like-kind SinglyLinkedList instance.
|
|
1674
1048
|
*/
|
|
1675
|
-
|
|
1676
1049
|
protected _spawnLike<EM, RM>(options?: SinglyLinkedListOptions<EM, RM>): SinglyLinkedList<EM, RM> {
|
|
1677
1050
|
return this._createLike<EM, RM>([], options);
|
|
1678
1051
|
}
|