max-priority-queue-typed 2.4.5 → 2.5.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/README.md +63 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -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 +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -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 +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/max-priority-queue-typed.js +691 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- 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 +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -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,53 @@ 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
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
* @example
|
|
332
|
+
* // basic SinglyLinkedList creation and push operation
|
|
333
|
+
* // Create a simple SinglyLinkedList with initial values
|
|
334
|
+
* const list = new SinglyLinkedList([1, 2, 3, 4, 5]);
|
|
335
|
+
*
|
|
336
|
+
* // Verify the list maintains insertion order
|
|
337
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
338
|
+
*
|
|
339
|
+
* // Check length
|
|
340
|
+
* console.log(list.length); // 5;
|
|
341
|
+
*
|
|
342
|
+
* // Push a new element to the end
|
|
343
|
+
* list.push(6);
|
|
344
|
+
* console.log(list.length); // 6;
|
|
345
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
353
346
|
*/
|
|
354
347
|
|
|
355
348
|
push(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
@@ -369,6 +362,53 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
369
362
|
* Remove and return the tail element.
|
|
370
363
|
* @remarks Time O(N), Space O(1)
|
|
371
364
|
* @returns Removed element or undefined.
|
|
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
|
+
* @example
|
|
398
|
+
* // SinglyLinkedList pop and shift operations
|
|
399
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
400
|
+
*
|
|
401
|
+
* // Pop removes from the end
|
|
402
|
+
* const last = list.pop();
|
|
403
|
+
* console.log(last); // 50;
|
|
404
|
+
*
|
|
405
|
+
* // Shift removes from the beginning
|
|
406
|
+
* const first = list.shift();
|
|
407
|
+
* console.log(first); // 10;
|
|
408
|
+
*
|
|
409
|
+
* // Verify remaining elements
|
|
410
|
+
* console.log([...list]); // [20, 30, 40];
|
|
411
|
+
* console.log(list.length); // 3;
|
|
372
412
|
*/
|
|
373
413
|
|
|
374
414
|
pop(): E | undefined {
|
|
@@ -393,6 +433,43 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
393
433
|
* Remove and return the head element.
|
|
394
434
|
* @remarks Time O(1), Space O(1)
|
|
395
435
|
* @returns Removed element or undefined.
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
* @example
|
|
469
|
+
* // Remove from the front
|
|
470
|
+
* const list = new SinglyLinkedList<number>([10, 20, 30]);
|
|
471
|
+
* console.log(list.shift()); // 10;
|
|
472
|
+
* console.log(list.length); // 2;
|
|
396
473
|
*/
|
|
397
474
|
|
|
398
475
|
shift(): E | undefined {
|
|
@@ -409,6 +486,58 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
409
486
|
* @remarks Time O(1), Space O(1)
|
|
410
487
|
* @param elementOrNode - Element or node to prepend.
|
|
411
488
|
* @returns True when prepended.
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
* @example
|
|
522
|
+
* // SinglyLinkedList unshift and forward traversal
|
|
523
|
+
* const list = new SinglyLinkedList<number>([20, 30, 40]);
|
|
524
|
+
*
|
|
525
|
+
* // Unshift adds to the beginning
|
|
526
|
+
* list.unshift(10);
|
|
527
|
+
* console.log([...list]); // [10, 20, 30, 40];
|
|
528
|
+
*
|
|
529
|
+
* // Access elements (forward traversal only for singly linked)
|
|
530
|
+
* const second = list.at(1);
|
|
531
|
+
* console.log(second); // 20;
|
|
532
|
+
*
|
|
533
|
+
* // SinglyLinkedList allows forward iteration only
|
|
534
|
+
* const elements: number[] = [];
|
|
535
|
+
* for (const item of list) {
|
|
536
|
+
* elements.push(item);
|
|
537
|
+
* }
|
|
538
|
+
* console.log(elements); // [10, 20, 30, 40];
|
|
539
|
+
*
|
|
540
|
+
* console.log(list.length); // 4;
|
|
412
541
|
*/
|
|
413
542
|
|
|
414
543
|
unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
@@ -479,6 +608,44 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
479
608
|
* @remarks Time O(N), Space O(1)
|
|
480
609
|
* @param index - Zero-based index.
|
|
481
610
|
* @returns Element or undefined.
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
* @example
|
|
644
|
+
* // Access element by index
|
|
645
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c', 'd']);
|
|
646
|
+
* console.log(list.at(0)); // 'a';
|
|
647
|
+
* console.log(list.at(2)); // 'c';
|
|
648
|
+
* console.log(list.at(3)); // 'd';
|
|
482
649
|
*/
|
|
483
650
|
|
|
484
651
|
at(index: number): E | undefined {
|
|
@@ -506,6 +673,39 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
506
673
|
* @remarks Time O(N), Space O(1)
|
|
507
674
|
* @param index - Zero-based index.
|
|
508
675
|
* @returns Node or undefined.
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
* @example
|
|
706
|
+
* // Get node at index
|
|
707
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
708
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
509
709
|
*/
|
|
510
710
|
|
|
511
711
|
getNodeAt(index: number): SinglyLinkedListNode<E> | undefined {
|
|
@@ -520,6 +720,40 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
520
720
|
* @remarks Time O(N), Space O(1)
|
|
521
721
|
* @param index - Zero-based index.
|
|
522
722
|
* @returns Removed element or undefined.
|
|
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
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
* @example
|
|
753
|
+
* // Remove by index
|
|
754
|
+
* const list = new SinglyLinkedList<string>(['a', 'b', 'c']);
|
|
755
|
+
* list.deleteAt(1);
|
|
756
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
523
757
|
*/
|
|
524
758
|
|
|
525
759
|
deleteAt(index: number): E | undefined {
|
|
@@ -539,6 +773,40 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
539
773
|
* @remarks Time O(N), Space O(1)
|
|
540
774
|
* @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
|
|
541
775
|
* @returns True if removed.
|
|
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
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
* @example
|
|
806
|
+
* // Remove first occurrence
|
|
807
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 2]);
|
|
808
|
+
* list.delete(2);
|
|
809
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
542
810
|
*/
|
|
543
811
|
|
|
544
812
|
delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
|
|
@@ -564,6 +832,40 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
564
832
|
* @param index - Zero-based index.
|
|
565
833
|
* @param newElementOrNode - Element or node to insert.
|
|
566
834
|
* @returns True if inserted.
|
|
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
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
* @example
|
|
865
|
+
* // Insert at index
|
|
866
|
+
* const list = new SinglyLinkedList<number>([1, 3]);
|
|
867
|
+
* list.addAt(1, 2);
|
|
868
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
567
869
|
*/
|
|
568
870
|
|
|
569
871
|
addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean {
|
|
@@ -597,6 +899,39 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
597
899
|
* Check whether the list is empty.
|
|
598
900
|
* @remarks Time O(1), Space O(1)
|
|
599
901
|
* @returns True if length is 0.
|
|
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
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
* @example
|
|
933
|
+
* // Check empty
|
|
934
|
+
* console.log(new SinglyLinkedList().isEmpty()); // true;
|
|
600
935
|
*/
|
|
601
936
|
|
|
602
937
|
isEmpty(): boolean {
|
|
@@ -607,6 +942,41 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
607
942
|
* Remove all nodes and reset length.
|
|
608
943
|
* @remarks Time O(N), Space O(1)
|
|
609
944
|
* @returns void
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
* @example
|
|
976
|
+
* // Remove all
|
|
977
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
978
|
+
* list.clear();
|
|
979
|
+
* console.log(list.isEmpty()); // true;
|
|
610
980
|
*/
|
|
611
981
|
|
|
612
982
|
clear(): void {
|
|
@@ -619,6 +989,43 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
619
989
|
* Reverse the list in place.
|
|
620
990
|
* @remarks Time O(N), Space O(1)
|
|
621
991
|
* @returns This list.
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
* @example
|
|
1025
|
+
* // Reverse the list in-place
|
|
1026
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4]);
|
|
1027
|
+
* list.reverse();
|
|
1028
|
+
* console.log([...list]); // [4, 3, 2, 1];
|
|
622
1029
|
*/
|
|
623
1030
|
|
|
624
1031
|
reverse(): this {
|
|
@@ -827,6 +1234,43 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
827
1234
|
* Deep clone this list (values are copied by reference).
|
|
828
1235
|
* @remarks Time O(N), Space O(N)
|
|
829
1236
|
* @returns A new list with the same element sequence.
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
* @example
|
|
1268
|
+
* // Deep copy
|
|
1269
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1270
|
+
* const copy = list.clone();
|
|
1271
|
+
* copy.pop();
|
|
1272
|
+
* console.log(list.length); // 3;
|
|
1273
|
+
* console.log(copy.length); // 2;
|
|
830
1274
|
*/
|
|
831
1275
|
|
|
832
1276
|
clone(): this {
|
|
@@ -841,9 +1285,56 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
841
1285
|
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
842
1286
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
843
1287
|
* @returns A new list with kept values.
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
* @example
|
|
1321
|
+
* // SinglyLinkedList filter and map operations
|
|
1322
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1323
|
+
*
|
|
1324
|
+
* // Filter even numbers
|
|
1325
|
+
* const filtered = list.filter(value => value % 2 === 0);
|
|
1326
|
+
* console.log(filtered.length); // 2;
|
|
1327
|
+
*
|
|
1328
|
+
* // Map to double values
|
|
1329
|
+
* const doubled = list.map(value => value * 2);
|
|
1330
|
+
* console.log(doubled.length); // 5;
|
|
1331
|
+
*
|
|
1332
|
+
* // Use reduce to sum
|
|
1333
|
+
* const sum = list.reduce((acc, value) => acc + value, 0);
|
|
1334
|
+
* console.log(sum); // 15;
|
|
844
1335
|
*/
|
|
845
1336
|
|
|
846
|
-
filter(callback: ElementCallback<E, R, boolean>, thisArg?:
|
|
1337
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
847
1338
|
const out = this._createInstance();
|
|
848
1339
|
let index = 0;
|
|
849
1340
|
for (const value of this) if (callback.call(thisArg, value, index++, this)) out.push(value);
|
|
@@ -858,7 +1349,7 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
858
1349
|
* @returns A new list with mapped values.
|
|
859
1350
|
*/
|
|
860
1351
|
|
|
861
|
-
mapSame(callback: ElementCallback<E, R, E>, thisArg?:
|
|
1352
|
+
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
|
|
862
1353
|
const out = this._createInstance();
|
|
863
1354
|
let index = 0;
|
|
864
1355
|
for (const value of this) {
|
|
@@ -877,12 +1368,49 @@ export class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, S
|
|
|
877
1368
|
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
878
1369
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
879
1370
|
* @returns A new SinglyLinkedList with mapped values.
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
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
|
+
* @example
|
|
1404
|
+
* // Transform elements
|
|
1405
|
+
* const list = new SinglyLinkedList<number>([1, 2, 3]);
|
|
1406
|
+
* const doubled = list.map(n => n * 2);
|
|
1407
|
+
* console.log([...doubled]); // [2, 4, 6];
|
|
880
1408
|
*/
|
|
881
1409
|
|
|
882
1410
|
map<EM, RM = any>(
|
|
883
1411
|
callback: ElementCallback<E, R, EM>,
|
|
884
1412
|
options?: SinglyLinkedListOptions<EM, RM>,
|
|
885
|
-
thisArg?:
|
|
1413
|
+
thisArg?: unknown
|
|
886
1414
|
): SinglyLinkedList<EM, RM> {
|
|
887
1415
|
const out = this._createLike<EM, RM>([], { ...(options ?? {}), maxLen: this._maxLen as number });
|
|
888
1416
|
let index = 0;
|