data-structure-typed 1.47.4 → 1.47.5
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/CHANGELOG.md +1 -1
- package/benchmark/report.html +2 -2
- package/benchmark/report.json +12 -18
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +10 -4
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/cjs/data-structures/hash/hash-table.js +50 -5
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -1
- package/dist/cjs/data-structures/heap/heap.js +51 -9
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -49
- package/dist/cjs/data-structures/queue/deque.js +79 -72
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +45 -0
- package/dist/cjs/data-structures/queue/queue.js +77 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +18 -5
- package/dist/cjs/data-structures/stack/stack.js +56 -7
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/cjs/data-structures/trie/trie.js +47 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +10 -4
- package/dist/mjs/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/mjs/data-structures/hash/hash-table.js +50 -5
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -1
- package/dist/mjs/data-structures/heap/heap.js +51 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -49
- package/dist/mjs/data-structures/queue/deque.js +79 -72
- package/dist/mjs/data-structures/queue/queue.d.ts +45 -0
- package/dist/mjs/data-structures/queue/queue.js +77 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +18 -5
- package/dist/mjs/data-structures/stack/stack.js +56 -7
- package/dist/mjs/data-structures/trie/trie.d.ts +5 -0
- package/dist/mjs/data-structures/trie/trie.js +47 -0
- package/dist/umd/data-structure-typed.js +545 -276
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +60 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
- package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
- package/src/data-structures/queue/deque.ts +84 -77
- package/src/data-structures/queue/queue.ts +84 -0
- package/src/data-structures/stack/stack.ts +64 -8
- package/src/data-structures/trie/trie.ts +53 -0
- package/test/integration/conversion.test.ts +0 -0
- package/test/performance/data-structures/heap/heap.test.ts +13 -4
- package/test/unit/data-structures/hash/hash-table.test.ts +58 -2
- package/test/unit/data-structures/heap/min-heap.test.ts +48 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/unit/data-structures/queue/queue.test.ts +37 -0
- package/test/unit/data-structures/stack/stack.test.ts +55 -5
- package/test/unit/data-structures/trie/trie.test.ts +33 -0
|
@@ -403,6 +403,46 @@ export class DoublyLinkedList {
|
|
|
403
403
|
}
|
|
404
404
|
return false;
|
|
405
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
408
|
+
* Space Complexity: O(1)
|
|
409
|
+
*/
|
|
410
|
+
/**
|
|
411
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
412
|
+
* Space Complexity: O(1)
|
|
413
|
+
*
|
|
414
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
415
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
416
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
417
|
+
* itself.
|
|
418
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
419
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
420
|
+
* existing value or node is not found in the doubly linked list.
|
|
421
|
+
*/
|
|
422
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
423
|
+
let existingNode;
|
|
424
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
425
|
+
existingNode = existingValueOrNode;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
429
|
+
}
|
|
430
|
+
if (existingNode) {
|
|
431
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
432
|
+
newNode.next = existingNode.next;
|
|
433
|
+
if (existingNode.next) {
|
|
434
|
+
existingNode.next.prev = newNode;
|
|
435
|
+
}
|
|
436
|
+
newNode.prev = existingNode;
|
|
437
|
+
existingNode.next = newNode;
|
|
438
|
+
if (existingNode === this.tail) {
|
|
439
|
+
this._tail = newNode;
|
|
440
|
+
}
|
|
441
|
+
this._length++;
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
406
446
|
/**
|
|
407
447
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
408
448
|
* Space Complexity: O(1)
|
|
@@ -472,26 +512,6 @@ export class DoublyLinkedList {
|
|
|
472
512
|
}
|
|
473
513
|
return false;
|
|
474
514
|
}
|
|
475
|
-
/**
|
|
476
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
477
|
-
* Space Complexity: O(n)
|
|
478
|
-
*/
|
|
479
|
-
/**
|
|
480
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
481
|
-
* Space Complexity: O(n)
|
|
482
|
-
*
|
|
483
|
-
* The `toArray` function converts a linked list into an array.
|
|
484
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
485
|
-
*/
|
|
486
|
-
toArray() {
|
|
487
|
-
const array = [];
|
|
488
|
-
let current = this.head;
|
|
489
|
-
while (current) {
|
|
490
|
-
array.push(current.value);
|
|
491
|
-
current = current.next;
|
|
492
|
-
}
|
|
493
|
-
return array;
|
|
494
|
-
}
|
|
495
515
|
/**
|
|
496
516
|
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
497
517
|
* @returns A boolean value is being returned.
|
|
@@ -582,6 +602,25 @@ export class DoublyLinkedList {
|
|
|
582
602
|
}
|
|
583
603
|
return null;
|
|
584
604
|
}
|
|
605
|
+
/**
|
|
606
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
607
|
+
* Space Complexity: O(1)
|
|
608
|
+
*/
|
|
609
|
+
/**
|
|
610
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
611
|
+
* Space Complexity: O(1)
|
|
612
|
+
*
|
|
613
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
614
|
+
*/
|
|
615
|
+
reverse() {
|
|
616
|
+
let current = this.head;
|
|
617
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
618
|
+
while (current) {
|
|
619
|
+
const next = current.next;
|
|
620
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
621
|
+
current = next;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
585
624
|
/**
|
|
586
625
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
587
626
|
* Space Complexity: O(n)
|
|
@@ -590,35 +629,46 @@ export class DoublyLinkedList {
|
|
|
590
629
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
591
630
|
* Space Complexity: O(n)
|
|
592
631
|
*
|
|
593
|
-
* The `
|
|
594
|
-
* @returns The `
|
|
632
|
+
* The `toArray` function converts a linked list into an array.
|
|
633
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
595
634
|
*/
|
|
596
|
-
|
|
635
|
+
toArray() {
|
|
597
636
|
const array = [];
|
|
598
|
-
let current = this.
|
|
637
|
+
let current = this.head;
|
|
599
638
|
while (current) {
|
|
600
639
|
array.push(current.value);
|
|
601
|
-
current = current.
|
|
640
|
+
current = current.next;
|
|
602
641
|
}
|
|
603
642
|
return array;
|
|
604
643
|
}
|
|
605
644
|
/**
|
|
606
645
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
607
|
-
* Space Complexity: O(
|
|
646
|
+
* Space Complexity: O(n)
|
|
608
647
|
*/
|
|
609
648
|
/**
|
|
610
649
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
611
|
-
* Space Complexity: O(
|
|
650
|
+
* Space Complexity: O(n)
|
|
612
651
|
*
|
|
613
|
-
* The `
|
|
652
|
+
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
653
|
+
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
614
654
|
*/
|
|
615
|
-
|
|
655
|
+
toReversedArray() {
|
|
656
|
+
const array = [];
|
|
657
|
+
let current = this.tail;
|
|
658
|
+
while (current) {
|
|
659
|
+
array.push(current.value);
|
|
660
|
+
current = current.prev;
|
|
661
|
+
}
|
|
662
|
+
return array;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
666
|
+
*/
|
|
667
|
+
*[Symbol.iterator]() {
|
|
616
668
|
let current = this.head;
|
|
617
|
-
[this._head, this._tail] = [this.tail, this.head];
|
|
618
669
|
while (current) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
current = next;
|
|
670
|
+
yield current.value;
|
|
671
|
+
current = current.next;
|
|
622
672
|
}
|
|
623
673
|
}
|
|
624
674
|
/**
|
|
@@ -635,11 +685,9 @@ export class DoublyLinkedList {
|
|
|
635
685
|
* current node in the linked list.
|
|
636
686
|
*/
|
|
637
687
|
forEach(callback) {
|
|
638
|
-
let current = this.head;
|
|
639
688
|
let index = 0;
|
|
640
|
-
|
|
641
|
-
callback(
|
|
642
|
-
current = current.next;
|
|
689
|
+
for (const el of this) {
|
|
690
|
+
callback(el, index, this);
|
|
643
691
|
index++;
|
|
644
692
|
}
|
|
645
693
|
}
|
|
@@ -651,21 +699,22 @@ export class DoublyLinkedList {
|
|
|
651
699
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
652
700
|
* Space Complexity: O(n)
|
|
653
701
|
*
|
|
654
|
-
* The `
|
|
655
|
-
*
|
|
656
|
-
* @param callback - The callback parameter is a function that takes a value of type E
|
|
657
|
-
*
|
|
658
|
-
* DoublyLinkedList
|
|
659
|
-
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
702
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
703
|
+
* elements that satisfy the given callback function.
|
|
704
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
705
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
706
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
660
707
|
*/
|
|
661
|
-
|
|
662
|
-
const
|
|
663
|
-
let
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
708
|
+
filter(callback) {
|
|
709
|
+
const filteredList = new DoublyLinkedList();
|
|
710
|
+
let index = 0;
|
|
711
|
+
for (const current of this) {
|
|
712
|
+
if (callback(current, index, this)) {
|
|
713
|
+
filteredList.push(current);
|
|
714
|
+
}
|
|
715
|
+
index++;
|
|
667
716
|
}
|
|
668
|
-
return
|
|
717
|
+
return filteredList;
|
|
669
718
|
}
|
|
670
719
|
/**
|
|
671
720
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -675,22 +724,21 @@ export class DoublyLinkedList {
|
|
|
675
724
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
676
725
|
* Space Complexity: O(n)
|
|
677
726
|
*
|
|
678
|
-
* The `
|
|
679
|
-
*
|
|
680
|
-
* @param callback - The
|
|
681
|
-
*
|
|
682
|
-
*
|
|
727
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
728
|
+
* DoublyLinkedList with the transformed values.
|
|
729
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
730
|
+
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
731
|
+
* DoublyLinkedList).
|
|
732
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
|
|
683
733
|
*/
|
|
684
|
-
|
|
685
|
-
const
|
|
686
|
-
let
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
}
|
|
691
|
-
current = current.next;
|
|
734
|
+
map(callback) {
|
|
735
|
+
const mappedList = new DoublyLinkedList();
|
|
736
|
+
let index = 0;
|
|
737
|
+
for (const current of this) {
|
|
738
|
+
mappedList.push(callback(current, index, this));
|
|
739
|
+
index++;
|
|
692
740
|
}
|
|
693
|
-
return
|
|
741
|
+
return mappedList;
|
|
694
742
|
}
|
|
695
743
|
/**
|
|
696
744
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -704,68 +752,18 @@ export class DoublyLinkedList {
|
|
|
704
752
|
* single value.
|
|
705
753
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
706
754
|
* used to perform a specific operation on each element of the linked list.
|
|
707
|
-
* @param {
|
|
755
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
708
756
|
* point for the reduction operation.
|
|
709
757
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
710
758
|
* elements in the linked list.
|
|
711
759
|
*/
|
|
712
760
|
reduce(callback, initialValue) {
|
|
713
761
|
let accumulator = initialValue;
|
|
714
|
-
let
|
|
715
|
-
|
|
716
|
-
accumulator = callback(accumulator, current
|
|
717
|
-
|
|
762
|
+
let index = 0;
|
|
763
|
+
for (const current of this) {
|
|
764
|
+
accumulator = callback(accumulator, current, index, this);
|
|
765
|
+
index++;
|
|
718
766
|
}
|
|
719
767
|
return accumulator;
|
|
720
768
|
}
|
|
721
|
-
/**
|
|
722
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
723
|
-
* Space Complexity: O(1)
|
|
724
|
-
*/
|
|
725
|
-
/**
|
|
726
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
727
|
-
* Space Complexity: O(1)
|
|
728
|
-
*
|
|
729
|
-
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
730
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
731
|
-
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
732
|
-
* itself.
|
|
733
|
-
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
734
|
-
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
735
|
-
* existing value or node is not found in the doubly linked list.
|
|
736
|
-
*/
|
|
737
|
-
insertAfter(existingValueOrNode, newValue) {
|
|
738
|
-
let existingNode;
|
|
739
|
-
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
740
|
-
existingNode = existingValueOrNode;
|
|
741
|
-
}
|
|
742
|
-
else {
|
|
743
|
-
existingNode = this.getNode(existingValueOrNode);
|
|
744
|
-
}
|
|
745
|
-
if (existingNode) {
|
|
746
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
747
|
-
newNode.next = existingNode.next;
|
|
748
|
-
if (existingNode.next) {
|
|
749
|
-
existingNode.next.prev = newNode;
|
|
750
|
-
}
|
|
751
|
-
newNode.prev = existingNode;
|
|
752
|
-
existingNode.next = newNode;
|
|
753
|
-
if (existingNode === this.tail) {
|
|
754
|
-
this._tail = newNode;
|
|
755
|
-
}
|
|
756
|
-
this._length++;
|
|
757
|
-
return true;
|
|
758
|
-
}
|
|
759
|
-
return false;
|
|
760
|
-
}
|
|
761
|
-
/**
|
|
762
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
763
|
-
*/
|
|
764
|
-
*[Symbol.iterator]() {
|
|
765
|
-
let current = this.head;
|
|
766
|
-
while (current) {
|
|
767
|
-
yield current.value;
|
|
768
|
-
current = current.next;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
769
|
}
|
|
@@ -345,42 +345,30 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
345
345
|
*/
|
|
346
346
|
countOccurrences(value: E): number;
|
|
347
347
|
/**
|
|
348
|
-
*
|
|
349
|
-
|
|
348
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
349
|
+
*/
|
|
350
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
351
|
+
/**
|
|
352
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
353
|
+
* Space Complexity: O(1)
|
|
350
354
|
*/
|
|
351
355
|
/**
|
|
352
|
-
* Time Complexity: O(n)
|
|
353
|
-
* Space Complexity: O(1)
|
|
356
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
357
|
+
* Space Complexity: O(1)
|
|
354
358
|
*
|
|
355
359
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
356
360
|
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
357
361
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
358
362
|
* current node in the linked list.
|
|
359
363
|
*/
|
|
360
|
-
forEach(callback: (value: E, index: number) => void): void;
|
|
361
|
-
/**
|
|
362
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
363
|
-
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
364
|
-
*/
|
|
365
|
-
/**
|
|
366
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
367
|
-
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
368
|
-
*
|
|
369
|
-
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
370
|
-
* SinglyLinkedList with the transformed values.
|
|
371
|
-
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
372
|
-
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
373
|
-
* SinglyLinkedList).
|
|
374
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
375
|
-
*/
|
|
376
|
-
map<U>(callback: (value: E) => U): SinglyLinkedList<U>;
|
|
364
|
+
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void;
|
|
377
365
|
/**
|
|
378
|
-
* Time Complexity: O(n)
|
|
379
|
-
* Space Complexity: O(n)
|
|
366
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
367
|
+
* Space Complexity: O(n)
|
|
380
368
|
*/
|
|
381
369
|
/**
|
|
382
|
-
* Time Complexity: O(n)
|
|
383
|
-
* Space Complexity: O(n)
|
|
370
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
371
|
+
* Space Complexity: O(n)
|
|
384
372
|
*
|
|
385
373
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
386
374
|
* elements that satisfy the given callback function.
|
|
@@ -388,27 +376,39 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
388
376
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
389
377
|
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
390
378
|
*/
|
|
391
|
-
filter(callback: (value: E) => boolean): SinglyLinkedList<E>;
|
|
379
|
+
filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E>;
|
|
392
380
|
/**
|
|
393
|
-
* Time Complexity: O(n)
|
|
394
|
-
* Space Complexity: O(n)
|
|
381
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
382
|
+
* Space Complexity: O(n)
|
|
395
383
|
*/
|
|
396
384
|
/**
|
|
397
|
-
* Time Complexity: O(n)
|
|
398
|
-
* Space Complexity: O(n)
|
|
385
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
386
|
+
* Space Complexity: O(n)
|
|
387
|
+
*
|
|
388
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
389
|
+
* SinglyLinkedList with the transformed values.
|
|
390
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
391
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
392
|
+
* SinglyLinkedList).
|
|
393
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
394
|
+
*/
|
|
395
|
+
map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T>;
|
|
396
|
+
/**
|
|
397
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
398
|
+
* Space Complexity: O(n)
|
|
399
|
+
*/
|
|
400
|
+
/**
|
|
401
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
402
|
+
* Space Complexity: O(n)
|
|
399
403
|
*
|
|
400
404
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
401
405
|
* single value.
|
|
402
406
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
403
407
|
* used to perform a specific operation on each element of the linked list.
|
|
404
|
-
* @param {
|
|
408
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
405
409
|
* point for the reduction operation.
|
|
406
410
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
407
411
|
* elements in the linked list.
|
|
408
412
|
*/
|
|
409
|
-
reduce<
|
|
410
|
-
/**
|
|
411
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
412
|
-
*/
|
|
413
|
-
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
413
|
+
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
|
|
414
414
|
}
|
|
@@ -606,58 +606,42 @@ export class SinglyLinkedList {
|
|
|
606
606
|
return count;
|
|
607
607
|
}
|
|
608
608
|
/**
|
|
609
|
-
*
|
|
610
|
-
* Space Complexity: O(1) - Constant space.
|
|
611
|
-
*/
|
|
612
|
-
/**
|
|
613
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
614
|
-
* Space Complexity: O(1) - Constant space.
|
|
615
|
-
*
|
|
616
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
617
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
618
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
619
|
-
* current node in the linked list.
|
|
609
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
620
610
|
*/
|
|
621
|
-
|
|
611
|
+
*[Symbol.iterator]() {
|
|
622
612
|
let current = this.head;
|
|
623
|
-
let index = 0;
|
|
624
613
|
while (current) {
|
|
625
|
-
|
|
614
|
+
yield current.value;
|
|
626
615
|
current = current.next;
|
|
627
|
-
index++;
|
|
628
616
|
}
|
|
629
617
|
}
|
|
630
618
|
/**
|
|
631
|
-
* Time Complexity: O(n)
|
|
632
|
-
* Space Complexity: O(
|
|
619
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
620
|
+
* Space Complexity: O(1)
|
|
633
621
|
*/
|
|
634
622
|
/**
|
|
635
|
-
* Time Complexity: O(n)
|
|
636
|
-
* Space Complexity: O(
|
|
623
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
624
|
+
* Space Complexity: O(1)
|
|
637
625
|
*
|
|
638
|
-
* The `
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
* SinglyLinkedList).
|
|
643
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
626
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
627
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
628
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
629
|
+
* current node in the linked list.
|
|
644
630
|
*/
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
current = current.next;
|
|
631
|
+
forEach(callback) {
|
|
632
|
+
let index = 0;
|
|
633
|
+
for (const el of this) {
|
|
634
|
+
callback(el, index, this);
|
|
635
|
+
index++;
|
|
651
636
|
}
|
|
652
|
-
return mappedList;
|
|
653
637
|
}
|
|
654
638
|
/**
|
|
655
|
-
* Time Complexity: O(n)
|
|
656
|
-
* Space Complexity: O(n)
|
|
639
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
640
|
+
* Space Complexity: O(n)
|
|
657
641
|
*/
|
|
658
642
|
/**
|
|
659
|
-
* Time Complexity: O(n)
|
|
660
|
-
* Space Complexity: O(n)
|
|
643
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
644
|
+
* Space Complexity: O(n)
|
|
661
645
|
*
|
|
662
646
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
663
647
|
* elements that satisfy the given callback function.
|
|
@@ -667,49 +651,63 @@ export class SinglyLinkedList {
|
|
|
667
651
|
*/
|
|
668
652
|
filter(callback) {
|
|
669
653
|
const filteredList = new SinglyLinkedList();
|
|
670
|
-
let
|
|
671
|
-
|
|
672
|
-
if (callback(current
|
|
673
|
-
filteredList.push(current
|
|
654
|
+
let index = 0;
|
|
655
|
+
for (const current of this) {
|
|
656
|
+
if (callback(current, index, this)) {
|
|
657
|
+
filteredList.push(current);
|
|
674
658
|
}
|
|
675
|
-
|
|
659
|
+
index++;
|
|
676
660
|
}
|
|
677
661
|
return filteredList;
|
|
678
662
|
}
|
|
679
663
|
/**
|
|
680
|
-
* Time Complexity: O(n)
|
|
681
|
-
* Space Complexity: O(n)
|
|
664
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
665
|
+
* Space Complexity: O(n)
|
|
682
666
|
*/
|
|
683
667
|
/**
|
|
684
|
-
* Time Complexity: O(n)
|
|
685
|
-
* Space Complexity: O(n)
|
|
668
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
669
|
+
* Space Complexity: O(n)
|
|
670
|
+
*
|
|
671
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
672
|
+
* SinglyLinkedList with the transformed values.
|
|
673
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
674
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
675
|
+
* SinglyLinkedList).
|
|
676
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
677
|
+
*/
|
|
678
|
+
map(callback) {
|
|
679
|
+
const mappedList = new SinglyLinkedList();
|
|
680
|
+
let index = 0;
|
|
681
|
+
for (const current of this) {
|
|
682
|
+
mappedList.push(callback(current, index, this));
|
|
683
|
+
index++;
|
|
684
|
+
}
|
|
685
|
+
return mappedList;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
689
|
+
* Space Complexity: O(n)
|
|
690
|
+
*/
|
|
691
|
+
/**
|
|
692
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
693
|
+
* Space Complexity: O(n)
|
|
686
694
|
*
|
|
687
695
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
688
696
|
* single value.
|
|
689
697
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
690
698
|
* used to perform a specific operation on each element of the linked list.
|
|
691
|
-
* @param {
|
|
699
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
692
700
|
* point for the reduction operation.
|
|
693
701
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
694
702
|
* elements in the linked list.
|
|
695
703
|
*/
|
|
696
704
|
reduce(callback, initialValue) {
|
|
697
705
|
let accumulator = initialValue;
|
|
698
|
-
let
|
|
699
|
-
|
|
700
|
-
accumulator = callback(accumulator, current
|
|
701
|
-
|
|
706
|
+
let index = 0;
|
|
707
|
+
for (const current of this) {
|
|
708
|
+
accumulator = callback(accumulator, current, index, this);
|
|
709
|
+
index++;
|
|
702
710
|
}
|
|
703
711
|
return accumulator;
|
|
704
712
|
}
|
|
705
|
-
/**
|
|
706
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
707
|
-
*/
|
|
708
|
-
*[Symbol.iterator]() {
|
|
709
|
-
let current = this.head;
|
|
710
|
-
while (current) {
|
|
711
|
-
yield current.value;
|
|
712
|
-
current = current.next;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
713
|
}
|