binary-tree-typed 2.4.4 → 2.5.0
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/README.md +0 -84
- package/dist/cjs/index.cjs +965 -420
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +962 -417
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +965 -421
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +962 -418
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/common/error.d.ts +23 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +313 -66
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
- package/dist/umd/binary-tree-typed.js +959 -414
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +3 -3
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/error.ts +60 -0
- package/src/common/index.ts +2 -0
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +134 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
- package/src/data-structures/binary-tree/binary-tree.ts +542 -121
- package/src/data-structures/binary-tree/bst.ts +346 -37
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
- package/src/data-structures/binary-tree/segment-tree.ts +372 -248
- package/src/data-structures/binary-tree/tree-map.ts +1292 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
- package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
- package/src/data-structures/binary-tree/tree-set.ts +1143 -15
- package/src/data-structures/graph/abstract-graph.ts +106 -1
- package/src/data-structures/graph/directed-graph.ts +223 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +299 -59
- package/src/data-structures/hash/hash-map.ts +243 -79
- package/src/data-structures/heap/heap.ts +291 -102
- package/src/data-structures/heap/max-heap.ts +48 -3
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
- package/src/data-structures/matrix/matrix.ts +425 -22
- package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +343 -68
- package/src/data-structures/queue/queue.ts +211 -42
- package/src/data-structures/stack/stack.ts +174 -32
- package/src/data-structures/trie/trie.ts +215 -44
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/queue/deque.ts +7 -0
- package/src/utils/utils.ts +4 -2
|
@@ -63,71 +63,6 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
63
63
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
64
64
|
* Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
|
|
65
65
|
* @example
|
|
66
|
-
* // basic SinglyLinkedList creation and push operation
|
|
67
|
-
* // Create a simple SinglyLinkedList with initial values
|
|
68
|
-
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
69
|
-
*
|
|
70
|
-
* // Verify the list maintains insertion order
|
|
71
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
72
|
-
*
|
|
73
|
-
* // Check length
|
|
74
|
-
* console.log(list.length); // 5;
|
|
75
|
-
*
|
|
76
|
-
* // Push a new element to the end
|
|
77
|
-
* list.push(6);
|
|
78
|
-
* console.log(list.length); // 6;
|
|
79
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
80
|
-
* @example
|
|
81
|
-
* // SinglyLinkedList pop and shift operations
|
|
82
|
-
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
83
|
-
*
|
|
84
|
-
* // Pop removes from the end
|
|
85
|
-
* const last = list.pop();
|
|
86
|
-
* console.log(last); // 50;
|
|
87
|
-
*
|
|
88
|
-
* // Shift removes from the beginning
|
|
89
|
-
* const first = list.shift();
|
|
90
|
-
* console.log(first); // 10;
|
|
91
|
-
*
|
|
92
|
-
* // Verify remaining elements
|
|
93
|
-
* console.log([...list]); // [20, 30, 40];
|
|
94
|
-
* console.log(list.length); // 3;
|
|
95
|
-
* @example
|
|
96
|
-
* // SinglyLinkedList unshift and forward traversal
|
|
97
|
-
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
98
|
-
*
|
|
99
|
-
* // Unshift adds to the beginning
|
|
100
|
-
* list.unshift(10);
|
|
101
|
-
* console.log([...list]); // [10, 20, 30, 40];
|
|
102
|
-
*
|
|
103
|
-
* // Access elements (forward traversal only for singly linked)
|
|
104
|
-
* const second = list.at(1);
|
|
105
|
-
* console.log(second); // 20;
|
|
106
|
-
*
|
|
107
|
-
* // SinglyLinkedList allows forward iteration only
|
|
108
|
-
* const elements: number[] = [];
|
|
109
|
-
* for (const item of list) {
|
|
110
|
-
* elements.push(item);
|
|
111
|
-
* }
|
|
112
|
-
* console.log(elements); // [10, 20, 30, 40];
|
|
113
|
-
*
|
|
114
|
-
* console.log(list.length); // 4;
|
|
115
|
-
* @example
|
|
116
|
-
* // SinglyLinkedList filter and map operations
|
|
117
|
-
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
118
|
-
*
|
|
119
|
-
* // Filter even numbers
|
|
120
|
-
* const filtered = list.filter(value => value % 2 === 0);
|
|
121
|
-
* console.log(filtered.length); // 2;
|
|
122
|
-
*
|
|
123
|
-
* // Map to double values
|
|
124
|
-
* const doubled = list.map(value => value * 2);
|
|
125
|
-
* console.log(doubled.length); // 5;
|
|
126
|
-
*
|
|
127
|
-
* // Use reduce to sum
|
|
128
|
-
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
129
|
-
* console.log(sum); // 15;
|
|
130
|
-
* @example
|
|
131
66
|
* // SinglyLinkedList for sequentially processed data stream
|
|
132
67
|
* interface LogEntry {
|
|
133
68
|
* timestamp: number;
|
|
@@ -244,6 +179,17 @@ export class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
244
179
|
* editor.moveCursor(1);
|
|
245
180
|
* editor.insert('a');
|
|
246
181
|
* console.log(editor.getText()); // 'Haello';
|
|
182
|
+
* @example
|
|
183
|
+
* // Find first matching element
|
|
184
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
185
|
+
* console.log(list.find(n => n > 3)); // 4;
|
|
186
|
+
* console.log(list.find(n => n > 10)); // undefined;
|
|
187
|
+
* @example
|
|
188
|
+
* // Iterate over elements
|
|
189
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
190
|
+
* const result: number[] = [];
|
|
191
|
+
* list.forEach(n => result.push(n));
|
|
192
|
+
* console.log(result); // [10, 20, 30];
|
|
247
193
|
*/
|
|
248
194
|
export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
|
|
249
195
|
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
@@ -350,6 +296,32 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
350
296
|
* @remarks Time O(1), Space O(1)
|
|
351
297
|
* @param elementOrNode - Element or node to append.
|
|
352
298
|
* @returns True when appended.
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
* @example
|
|
311
|
+
* // basic SinglyLinkedList creation and push operation
|
|
312
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
313
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
314
|
+
*
|
|
315
|
+
* // Verify the list maintains insertion order
|
|
316
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
317
|
+
*
|
|
318
|
+
* // Check length
|
|
319
|
+
* console.log(list.length); // 5;
|
|
320
|
+
*
|
|
321
|
+
* // Push a new element to the end
|
|
322
|
+
* list.push(6);
|
|
323
|
+
* console.log(list.length); // 6;
|
|
324
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
353
325
|
*/
|
|
354
326
|
|
|
355
327
|
push(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
@@ -369,6 +341,32 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
369
341
|
* Remove and return the tail element.
|
|
370
342
|
* @remarks Time O(N), Space O(1)
|
|
371
343
|
* @returns Removed element or undefined.
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
* @example
|
|
356
|
+
* // SinglyLinkedList pop and shift operations
|
|
357
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
358
|
+
*
|
|
359
|
+
* // Pop removes from the end
|
|
360
|
+
* const last = list.pop();
|
|
361
|
+
* console.log(last); // 50;
|
|
362
|
+
*
|
|
363
|
+
* // Shift removes from the beginning
|
|
364
|
+
* const first = list.shift();
|
|
365
|
+
* console.log(first); // 10;
|
|
366
|
+
*
|
|
367
|
+
* // Verify remaining elements
|
|
368
|
+
* console.log([...list]); // [20, 30, 40];
|
|
369
|
+
* console.log(list.length); // 3;
|
|
372
370
|
*/
|
|
373
371
|
|
|
374
372
|
pop(): E | undefined {
|
|
@@ -393,6 +391,22 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
393
391
|
* Remove and return the head element.
|
|
394
392
|
* @remarks Time O(1), Space O(1)
|
|
395
393
|
* @returns Removed element or undefined.
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
* @example
|
|
406
|
+
* // Remove from the front
|
|
407
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
408
|
+
* console.log(list.shift()); // 10;
|
|
409
|
+
* console.log(list.length); // 2;
|
|
396
410
|
*/
|
|
397
411
|
|
|
398
412
|
shift(): E | undefined {
|
|
@@ -409,6 +423,37 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
409
423
|
* @remarks Time O(1), Space O(1)
|
|
410
424
|
* @param elementOrNode - Element or node to prepend.
|
|
411
425
|
* @returns True when prepended.
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
* @example
|
|
438
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
439
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
440
|
+
*
|
|
441
|
+
* // Unshift adds to the beginning
|
|
442
|
+
* list.unshift(10);
|
|
443
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
444
|
+
*
|
|
445
|
+
* // Access elements (forward traversal only for singly linked)
|
|
446
|
+
* const second = list.at(1);
|
|
447
|
+
* console.log(second); // 20;
|
|
448
|
+
*
|
|
449
|
+
* // SinglyLinkedList allows forward iteration only
|
|
450
|
+
* const elements: number[] = [];
|
|
451
|
+
* for (const item of list) {
|
|
452
|
+
* elements.push(item);
|
|
453
|
+
* }
|
|
454
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
455
|
+
*
|
|
456
|
+
* console.log(list.length); // 4;
|
|
412
457
|
*/
|
|
413
458
|
|
|
414
459
|
unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
@@ -479,6 +524,23 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
479
524
|
* @remarks Time O(N), Space O(1)
|
|
480
525
|
* @param index - Zero-based index.
|
|
481
526
|
* @returns Element or undefined.
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
* @example
|
|
539
|
+
* // Access element by index
|
|
540
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
541
|
+
* console.log(list.at(0)); // 'a';
|
|
542
|
+
* console.log(list.at(2)); // 'c';
|
|
543
|
+
* console.log(list.at(3)); // 'd';
|
|
482
544
|
*/
|
|
483
545
|
|
|
484
546
|
at(index: number): E | undefined {
|
|
@@ -506,6 +568,18 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
506
568
|
* @remarks Time O(N), Space O(1)
|
|
507
569
|
* @param index - Zero-based index.
|
|
508
570
|
* @returns Node or undefined.
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
* @example
|
|
580
|
+
* // Get node at index
|
|
581
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
582
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
509
583
|
*/
|
|
510
584
|
|
|
511
585
|
getNodeAt(index: number): SinglyLinkedListNode<E> | undefined {
|
|
@@ -520,6 +594,19 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
520
594
|
* @remarks Time O(N), Space O(1)
|
|
521
595
|
* @param index - Zero-based index.
|
|
522
596
|
* @returns Removed element or undefined.
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
* @example
|
|
606
|
+
* // Remove by index
|
|
607
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
608
|
+
* list.deleteAt(1);
|
|
609
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
523
610
|
*/
|
|
524
611
|
|
|
525
612
|
deleteAt(index: number): E | undefined {
|
|
@@ -539,6 +626,19 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
539
626
|
* @remarks Time O(N), Space O(1)
|
|
540
627
|
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
541
628
|
* @returns True if removed.
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
* @example
|
|
638
|
+
* // Remove first occurrence
|
|
639
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
640
|
+
* list.delete(2);
|
|
641
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
542
642
|
*/
|
|
543
643
|
|
|
544
644
|
delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
|
|
@@ -564,6 +664,19 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
564
664
|
* @param index - Zero-based index.
|
|
565
665
|
* @param newElementOrNode - Element or node to insert.
|
|
566
666
|
* @returns True if inserted.
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
* @example
|
|
676
|
+
* // Insert at index
|
|
677
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
678
|
+
* list.addAt(1, 2);
|
|
679
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
567
680
|
*/
|
|
568
681
|
|
|
569
682
|
addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
@@ -597,6 +710,18 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
597
710
|
* Check whether the list is empty.
|
|
598
711
|
* @remarks Time O(1), Space O(1)
|
|
599
712
|
* @returns True if length is 0.
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
* @example
|
|
723
|
+
* // Check empty
|
|
724
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
600
725
|
*/
|
|
601
726
|
|
|
602
727
|
isEmpty(): boolean {
|
|
@@ -607,6 +732,20 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
607
732
|
* Remove all nodes and reset length.
|
|
608
733
|
* @remarks Time O(N), Space O(1)
|
|
609
734
|
* @returns void
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
* @example
|
|
745
|
+
* // Remove all
|
|
746
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
747
|
+
* list.clear();
|
|
748
|
+
* console.log(list.isEmpty()); // true;
|
|
610
749
|
*/
|
|
611
750
|
|
|
612
751
|
clear(): void {
|
|
@@ -619,6 +758,22 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
619
758
|
* Reverse the list in place.
|
|
620
759
|
* @remarks Time O(N), Space O(1)
|
|
621
760
|
* @returns This list.
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
* @example
|
|
773
|
+
* // Reverse the list in-place
|
|
774
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
775
|
+
* list.reverse();
|
|
776
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
622
777
|
*/
|
|
623
778
|
|
|
624
779
|
reverse(): this {
|
|
@@ -827,6 +982,22 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
827
982
|
* Deep clone this list (values are copied by reference).
|
|
828
983
|
* @remarks Time O(N), Space O(N)
|
|
829
984
|
* @returns A new list with the same element sequence.
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
* @example
|
|
995
|
+
* // Deep copy
|
|
996
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
997
|
+
* const copy = list.clone();
|
|
998
|
+
* copy.pop();
|
|
999
|
+
* console.log(list.length); // 3;
|
|
1000
|
+
* console.log(copy.length); // 2;
|
|
830
1001
|
*/
|
|
831
1002
|
|
|
832
1003
|
clone(): this {
|
|
@@ -841,6 +1012,32 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
841
1012
|
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
842
1013
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
843
1014
|
* @returns A new list with kept values.
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
* @example
|
|
1027
|
+
* // SinglyLinkedList filter and map operations
|
|
1028
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1029
|
+
*
|
|
1030
|
+
* // Filter even numbers
|
|
1031
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1032
|
+
* console.log(filtered.length); // 2;
|
|
1033
|
+
*
|
|
1034
|
+
* // Map to double values
|
|
1035
|
+
* const doubled = list.map(value => value * 2);
|
|
1036
|
+
* console.log(doubled.length); // 5;
|
|
1037
|
+
*
|
|
1038
|
+
* // Use reduce to sum
|
|
1039
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1040
|
+
* console.log(sum); // 15;
|
|
844
1041
|
*/
|
|
845
1042
|
|
|
846
1043
|
filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): this {
|
|
@@ -877,6 +1074,22 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
877
1074
|
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
878
1075
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
879
1076
|
* @returns A new SinglyLinkedList with mapped values.
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
* @example
|
|
1089
|
+
* // Transform elements
|
|
1090
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1091
|
+
* const doubled = list.map(n => n * 2);
|
|
1092
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
880
1093
|
*/
|
|
881
1094
|
|
|
882
1095
|
map<EM, RM = any>(
|