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
|
@@ -87,50 +87,6 @@ export class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
87
87
|
* 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.
|
|
88
88
|
* 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).
|
|
89
89
|
* @example
|
|
90
|
-
* // basic DoublyLinkedList creation and push operation
|
|
91
|
-
* // Create a simple DoublyLinkedList with initial values
|
|
92
|
-
* const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
93
|
-
*
|
|
94
|
-
* // Verify the list maintains insertion order
|
|
95
|
-
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
96
|
-
*
|
|
97
|
-
* // Check length
|
|
98
|
-
* console.log(list.length); // 5;
|
|
99
|
-
*
|
|
100
|
-
* // Push a new element to the end
|
|
101
|
-
* list.push(6);
|
|
102
|
-
* console.log(list.length); // 6;
|
|
103
|
-
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
104
|
-
* @example
|
|
105
|
-
* // DoublyLinkedList pop and shift operations
|
|
106
|
-
* const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
107
|
-
*
|
|
108
|
-
* // Pop removes from the end
|
|
109
|
-
* const last = list.pop();
|
|
110
|
-
* console.log(last); // 50;
|
|
111
|
-
*
|
|
112
|
-
* // Shift removes from the beginning
|
|
113
|
-
* const first = list.shift();
|
|
114
|
-
* console.log(first); // 10;
|
|
115
|
-
*
|
|
116
|
-
* // Verify remaining elements
|
|
117
|
-
* console.log([...list]); // [20, 30, 40];
|
|
118
|
-
* console.log(list.length); // 3;
|
|
119
|
-
* @example
|
|
120
|
-
* // DoublyLinkedList for...of iteration and map operation
|
|
121
|
-
* const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
122
|
-
*
|
|
123
|
-
* // Iterate through list
|
|
124
|
-
* const doubled = list.map(value => value * 2);
|
|
125
|
-
* console.log(doubled.length); // 5;
|
|
126
|
-
*
|
|
127
|
-
* // Use for...of loop
|
|
128
|
-
* const result: number[] = [];
|
|
129
|
-
* for (const item of list) {
|
|
130
|
-
* result.push(item);
|
|
131
|
-
* }
|
|
132
|
-
* console.log(result); // [1, 2, 3, 4, 5];
|
|
133
|
-
* @example
|
|
134
90
|
* // Browser history
|
|
135
91
|
* const browserHistory = new DoublyLinkedList<string>();
|
|
136
92
|
*
|
|
@@ -180,6 +136,16 @@ export class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
|
|
|
180
136
|
* // Access entry (in real LRU, this would move it to end)
|
|
181
137
|
* const foundEntry = [...cacheList].find(entry => entry.key === 'user:2');
|
|
182
138
|
* console.log(foundEntry?.value); // 'Bob';
|
|
139
|
+
* @example
|
|
140
|
+
* // Find first matching element
|
|
141
|
+
* const list = new DoublyLinkedList<number>([5, 10, 15, 20]);
|
|
142
|
+
* console.log(list.find(n => n >= 12)); // 15;
|
|
143
|
+
* @example
|
|
144
|
+
* // Iterate over elements
|
|
145
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3]);
|
|
146
|
+
* const sum: number[] = [];
|
|
147
|
+
* list.forEach(n => sum.push(n));
|
|
148
|
+
* console.log(sum); // [1, 2, 3];
|
|
183
149
|
*/
|
|
184
150
|
export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, DoublyLinkedListNode<E>> {
|
|
185
151
|
protected _equals: (a: E, b: E) => boolean = (a, b) => Object.is(a, b);
|
|
@@ -302,6 +268,53 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
302
268
|
* @remarks Time O(1), Space O(1)
|
|
303
269
|
* @param elementOrNode - Element or node to append.
|
|
304
270
|
* @returns True when appended.
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
* @example
|
|
304
|
+
* // basic DoublyLinkedList creation and push operation
|
|
305
|
+
* // Create a simple DoublyLinkedList with initial values
|
|
306
|
+
* const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
307
|
+
*
|
|
308
|
+
* // Verify the list maintains insertion order
|
|
309
|
+
* console.log([...list]); // [1, 2, 3, 4, 5];
|
|
310
|
+
*
|
|
311
|
+
* // Check length
|
|
312
|
+
* console.log(list.length); // 5;
|
|
313
|
+
*
|
|
314
|
+
* // Push a new element to the end
|
|
315
|
+
* list.push(6);
|
|
316
|
+
* console.log(list.length); // 6;
|
|
317
|
+
* console.log([...list]); // [1, 2, 3, 4, 5, 6];
|
|
305
318
|
*/
|
|
306
319
|
|
|
307
320
|
push(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
@@ -323,6 +336,53 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
323
336
|
* Remove and return the tail element.
|
|
324
337
|
* @remarks Time O(1), Space O(1)
|
|
325
338
|
* @returns Removed element or undefined.
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
* @example
|
|
372
|
+
* // DoublyLinkedList pop and shift operations
|
|
373
|
+
* const list = new DoublyLinkedList<number>([10, 20, 30, 40, 50]);
|
|
374
|
+
*
|
|
375
|
+
* // Pop removes from the end
|
|
376
|
+
* const last = list.pop();
|
|
377
|
+
* console.log(last); // 50;
|
|
378
|
+
*
|
|
379
|
+
* // Shift removes from the beginning
|
|
380
|
+
* const first = list.shift();
|
|
381
|
+
* console.log(first); // 10;
|
|
382
|
+
*
|
|
383
|
+
* // Verify remaining elements
|
|
384
|
+
* console.log([...list]); // [20, 30, 40];
|
|
385
|
+
* console.log(list.length); // 3;
|
|
326
386
|
*/
|
|
327
387
|
|
|
328
388
|
pop(): E | undefined {
|
|
@@ -343,6 +403,43 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
343
403
|
* Remove and return the head element.
|
|
344
404
|
* @remarks Time O(1), Space O(1)
|
|
345
405
|
* @returns Removed element or undefined.
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
* @example
|
|
439
|
+
* // Remove from the front
|
|
440
|
+
* const list = new DoublyLinkedList<number>([10, 20, 30]);
|
|
441
|
+
* console.log(list.shift()); // 10;
|
|
442
|
+
* console.log(list.first); // 20;
|
|
346
443
|
*/
|
|
347
444
|
|
|
348
445
|
shift(): E | undefined {
|
|
@@ -364,6 +461,43 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
364
461
|
* @remarks Time O(1), Space O(1)
|
|
365
462
|
* @param elementOrNode - Element or node to prepend.
|
|
366
463
|
* @returns True when prepended.
|
|
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
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
* @example
|
|
497
|
+
* // Add to the front
|
|
498
|
+
* const list = new DoublyLinkedList<number>([2, 3]);
|
|
499
|
+
* list.unshift(1);
|
|
500
|
+
* console.log([...list]); // [1, 2, 3];
|
|
367
501
|
*/
|
|
368
502
|
|
|
369
503
|
unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
@@ -418,6 +552,43 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
418
552
|
* @remarks Time O(N), Space O(1)
|
|
419
553
|
* @param index - Zero-based index.
|
|
420
554
|
* @returns Element or undefined.
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
* @example
|
|
588
|
+
* // Access by index
|
|
589
|
+
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
590
|
+
* console.log(list.at(1)); // 'b';
|
|
591
|
+
* console.log(list.at(2)); // 'c';
|
|
421
592
|
*/
|
|
422
593
|
|
|
423
594
|
at(index: number): E | undefined {
|
|
@@ -432,6 +603,39 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
432
603
|
* @remarks Time O(N), Space O(1)
|
|
433
604
|
* @param index - Zero-based index.
|
|
434
605
|
* @returns Node or undefined.
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
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
|
+
* @example
|
|
636
|
+
* // Get node at index
|
|
637
|
+
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
638
|
+
* console.log(list.getNodeAt(1)?.value); // 'b';
|
|
435
639
|
*/
|
|
436
640
|
|
|
437
641
|
getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
|
|
@@ -486,6 +690,40 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
486
690
|
* @param index - Zero-based index.
|
|
487
691
|
* @param newElementOrNode - Element or node to insert.
|
|
488
692
|
* @returns True if inserted.
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
* @example
|
|
723
|
+
* // Insert at position
|
|
724
|
+
* const list = new DoublyLinkedList<number>([1, 3]);
|
|
725
|
+
* list.addAt(1, 2);
|
|
726
|
+
* console.log(list.toArray()); // [1, 2, 3];
|
|
489
727
|
*/
|
|
490
728
|
|
|
491
729
|
addAt(index: number, newElementOrNode: E | DoublyLinkedListNode<E>): boolean {
|
|
@@ -575,6 +813,40 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
575
813
|
* @remarks Time O(N), Space O(1)
|
|
576
814
|
* @param index - Zero-based index.
|
|
577
815
|
* @returns Removed element or undefined.
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
* @example
|
|
846
|
+
* // Remove by index
|
|
847
|
+
* const list = new DoublyLinkedList<string>(['a', 'b', 'c']);
|
|
848
|
+
* list.deleteAt(1);
|
|
849
|
+
* console.log(list.toArray()); // ['a', 'c'];
|
|
578
850
|
*/
|
|
579
851
|
|
|
580
852
|
deleteAt(index: number): E | undefined {
|
|
@@ -596,6 +868,40 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
596
868
|
* @remarks Time O(N), Space O(1)
|
|
597
869
|
* @param [elementOrNode] - Element or node to remove.
|
|
598
870
|
* @returns True if removed.
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
* @example
|
|
901
|
+
* // Remove first occurrence
|
|
902
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 2]);
|
|
903
|
+
* list.delete(2);
|
|
904
|
+
* console.log(list.toArray()); // [1, 3, 2];
|
|
599
905
|
*/
|
|
600
906
|
|
|
601
907
|
delete(elementOrNode: E | DoublyLinkedListNode<E> | undefined): boolean {
|
|
@@ -618,6 +924,39 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
618
924
|
* Check whether the list is empty.
|
|
619
925
|
* @remarks Time O(1), Space O(1)
|
|
620
926
|
* @returns True if length is 0.
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
* @example
|
|
958
|
+
* // Check empty
|
|
959
|
+
* console.log(new DoublyLinkedList().isEmpty()); // true;
|
|
621
960
|
*/
|
|
622
961
|
|
|
623
962
|
isEmpty(): boolean {
|
|
@@ -628,6 +967,41 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
628
967
|
* Remove all nodes and reset length.
|
|
629
968
|
* @remarks Time O(N), Space O(1)
|
|
630
969
|
* @returns void
|
|
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
|
+
* @example
|
|
1001
|
+
* // Remove all
|
|
1002
|
+
* const list = new DoublyLinkedList<number>([1, 2]);
|
|
1003
|
+
* list.clear();
|
|
1004
|
+
* console.log(list.isEmpty()); // true;
|
|
631
1005
|
*/
|
|
632
1006
|
|
|
633
1007
|
clear(): void {
|
|
@@ -641,6 +1015,40 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
641
1015
|
* @remarks Time O(N), Space O(1)
|
|
642
1016
|
* @param elementNodeOrPredicate - Element, node, or predicate to match.
|
|
643
1017
|
* @returns Matched value or undefined.
|
|
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
|
+
* @example
|
|
1048
|
+
* // Search with predicate
|
|
1049
|
+
* const list = new DoublyLinkedList<number>([10, 20, 30]);
|
|
1050
|
+
* const found = list.search(node => node.value > 15);
|
|
1051
|
+
* console.log(found); // 20;
|
|
644
1052
|
*/
|
|
645
1053
|
|
|
646
1054
|
search(
|
|
@@ -660,6 +1068,41 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
660
1068
|
* @remarks Time O(N), Space O(1)
|
|
661
1069
|
* @param elementNodeOrPredicate - Element, node, or predicate to match.
|
|
662
1070
|
* @returns Matched value or undefined.
|
|
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
|
+
* @example
|
|
1101
|
+
* // Find value scanning from tail
|
|
1102
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 4]);
|
|
1103
|
+
* // getBackward scans from tail to head, returns first match
|
|
1104
|
+
* const found = list.getBackward(node => node.value < 4);
|
|
1105
|
+
* console.log(found); // 3;
|
|
663
1106
|
*/
|
|
664
1107
|
|
|
665
1108
|
getBackward(
|
|
@@ -678,6 +1121,43 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
678
1121
|
* Reverse the list in place.
|
|
679
1122
|
* @remarks Time O(N), Space O(1)
|
|
680
1123
|
* @returns This list.
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
* @example
|
|
1157
|
+
* // Reverse in-place
|
|
1158
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3]);
|
|
1159
|
+
* list.reverse();
|
|
1160
|
+
* console.log([...list]); // [3, 2, 1];
|
|
681
1161
|
*/
|
|
682
1162
|
|
|
683
1163
|
reverse(): this {
|
|
@@ -707,6 +1187,42 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
707
1187
|
* Deep clone this list (values are copied by reference).
|
|
708
1188
|
* @remarks Time O(N), Space O(N)
|
|
709
1189
|
* @returns A new list with the same element sequence.
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
* @example
|
|
1221
|
+
* // Deep copy
|
|
1222
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3]);
|
|
1223
|
+
* const copy = list.clone();
|
|
1224
|
+
* copy.pop();
|
|
1225
|
+
* console.log(list.length); // 3;
|
|
710
1226
|
*/
|
|
711
1227
|
|
|
712
1228
|
clone(): this {
|
|
@@ -721,9 +1237,46 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
721
1237
|
* @param callback - Predicate (value, index, list) → boolean to keep value.
|
|
722
1238
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
723
1239
|
* @returns A new list with kept values.
|
|
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
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
* @example
|
|
1273
|
+
* // Filter elements
|
|
1274
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1275
|
+
* const evens = list.filter(n => n % 2 === 0);
|
|
1276
|
+
* console.log([...evens]); // [2, 4];
|
|
724
1277
|
*/
|
|
725
1278
|
|
|
726
|
-
filter(callback: ElementCallback<E, R, boolean>, thisArg?:
|
|
1279
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
727
1280
|
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
728
1281
|
let index = 0;
|
|
729
1282
|
for (const v of this) if (callback.call(thisArg, v, index++, this)) out.push(v);
|
|
@@ -738,7 +1291,7 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
738
1291
|
* @returns A new list with mapped values.
|
|
739
1292
|
*/
|
|
740
1293
|
|
|
741
|
-
mapSame(callback: ElementCallback<E, R, E>, thisArg?:
|
|
1294
|
+
mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
|
|
742
1295
|
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
743
1296
|
let index = 0;
|
|
744
1297
|
for (const v of this) {
|
|
@@ -757,12 +1310,58 @@ export class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, D
|
|
|
757
1310
|
* @param [options] - Options for the output list (e.g., maxLen, toElementFn).
|
|
758
1311
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
759
1312
|
* @returns A new DoublyLinkedList with mapped values.
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
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
|
+
* @example
|
|
1346
|
+
* // DoublyLinkedList for...of iteration and map operation
|
|
1347
|
+
* const list = new DoublyLinkedList<number>([1, 2, 3, 4, 5]);
|
|
1348
|
+
*
|
|
1349
|
+
* // Iterate through list
|
|
1350
|
+
* const doubled = list.map(value => value * 2);
|
|
1351
|
+
* console.log(doubled.length); // 5;
|
|
1352
|
+
*
|
|
1353
|
+
* // Use for...of loop
|
|
1354
|
+
* const result: number[] = [];
|
|
1355
|
+
* for (const item of list) {
|
|
1356
|
+
* result.push(item);
|
|
1357
|
+
* }
|
|
1358
|
+
* console.log(result); // [1, 2, 3, 4, 5];
|
|
760
1359
|
*/
|
|
761
1360
|
|
|
762
1361
|
map<EM, RM>(
|
|
763
1362
|
callback: ElementCallback<E, R, EM>,
|
|
764
1363
|
options?: DoublyLinkedListOptions<EM, RM>,
|
|
765
|
-
thisArg?:
|
|
1364
|
+
thisArg?: unknown
|
|
766
1365
|
): DoublyLinkedList<EM, RM> {
|
|
767
1366
|
const out = this._createLike<EM, RM>([], { ...(options ?? {}), maxLen: this._maxLen });
|
|
768
1367
|
let index = 0;
|