directed-graph-typed 1.48.0 → 1.49.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/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +28 -19
- package/dist/data-structures/binary-tree/avl-tree.js +22 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
- package/dist/data-structures/binary-tree/binary-tree.js +241 -215
- package/dist/data-structures/binary-tree/bst.d.ts +64 -48
- package/dist/data-structures/binary-tree/bst.js +94 -65
- package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
- package/dist/data-structures/binary-tree/rb-tree.js +42 -49
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
- package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
- package/dist/data-structures/graph/abstract-graph.js +130 -103
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +42 -55
- package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
- package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
- package/dist/data-structures/queue/deque.d.ts +35 -167
- package/dist/data-structures/queue/deque.js +43 -249
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -25
- package/src/data-structures/binary-tree/binary-tree.ts +336 -296
- package/src/data-structures/binary-tree/bst.ts +135 -89
- package/src/data-structures/binary-tree/rb-tree.ts +60 -69
- package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
- package/src/data-structures/graph/abstract-graph.ts +136 -104
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- package/src/data-structures/hash/hash-map.ts +335 -84
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +54 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
- package/src/data-structures/queue/deque.ts +43 -275
- package/src/data-structures/queue/queue.ts +71 -86
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/index.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* data-structure-typed
|
|
3
6
|
*
|
|
@@ -22,11 +25,12 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
22
25
|
}
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
export class DoublyLinkedList<E = any> {
|
|
28
|
+
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
26
29
|
/**
|
|
27
30
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
31
|
*/
|
|
29
32
|
constructor(elements?: Iterable<E>) {
|
|
33
|
+
super();
|
|
30
34
|
this._head = undefined;
|
|
31
35
|
this._tail = undefined;
|
|
32
36
|
this._length = 0;
|
|
@@ -158,11 +162,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
158
162
|
* Time Complexity: O(1)
|
|
159
163
|
* Space Complexity: O(1)
|
|
160
164
|
*
|
|
161
|
-
* The `
|
|
165
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
162
166
|
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
163
167
|
* list is empty, it returns undefined.
|
|
164
168
|
*/
|
|
165
|
-
|
|
169
|
+
pollLast(): E | undefined {
|
|
166
170
|
return this.pop();
|
|
167
171
|
}
|
|
168
172
|
|
|
@@ -202,11 +206,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
202
206
|
* Time Complexity: O(1)
|
|
203
207
|
* Space Complexity: O(1)
|
|
204
208
|
*
|
|
205
|
-
* The `
|
|
209
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
206
210
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
207
211
|
* list.
|
|
208
212
|
*/
|
|
209
|
-
|
|
213
|
+
pollFirst(): E | undefined {
|
|
210
214
|
return this.shift();
|
|
211
215
|
}
|
|
212
216
|
|
|
@@ -724,59 +728,32 @@ export class DoublyLinkedList<E = any> {
|
|
|
724
728
|
}
|
|
725
729
|
|
|
726
730
|
/**
|
|
727
|
-
*
|
|
728
|
-
*/
|
|
729
|
-
* [Symbol.iterator]() {
|
|
730
|
-
let current = this.head;
|
|
731
|
-
|
|
732
|
-
while (current) {
|
|
733
|
-
yield current.value;
|
|
734
|
-
current = current.next;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
/**
|
|
739
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
740
|
-
* Space Complexity: O(1)
|
|
741
|
-
*/
|
|
742
|
-
|
|
743
|
-
/**
|
|
744
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
745
|
-
* Space Complexity: O(1)
|
|
746
|
-
*
|
|
747
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
748
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
749
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
750
|
-
* current node in the linked list.
|
|
751
|
-
*/
|
|
752
|
-
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void {
|
|
753
|
-
let index = 0;
|
|
754
|
-
for (const el of this) {
|
|
755
|
-
callback(el, index, this);
|
|
756
|
-
index++;
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
/**
|
|
761
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
731
|
+
* Time Complexity: O(n)
|
|
762
732
|
* Space Complexity: O(n)
|
|
763
733
|
*/
|
|
764
734
|
|
|
765
735
|
/**
|
|
766
|
-
* Time Complexity: O(n)
|
|
736
|
+
* Time Complexity: O(n)
|
|
767
737
|
* Space Complexity: O(n)
|
|
768
738
|
*
|
|
769
|
-
* The `filter` function
|
|
770
|
-
*
|
|
771
|
-
*
|
|
772
|
-
*
|
|
773
|
-
*
|
|
774
|
-
|
|
775
|
-
|
|
739
|
+
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
|
|
740
|
+
* list and applying a callback function to each element, returning only the elements for which the
|
|
741
|
+
* callback function returns true.
|
|
742
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
743
|
+
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current
|
|
744
|
+
* element, and the DoublyLinkedList itself. The callback function should return a boolean value
|
|
745
|
+
* indicating whether the current element should be included
|
|
746
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
747
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
748
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
749
|
+
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
|
|
750
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
751
|
+
*/
|
|
752
|
+
filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E> {
|
|
776
753
|
const filteredList = new DoublyLinkedList<E>();
|
|
777
754
|
let index = 0;
|
|
778
755
|
for (const current of this) {
|
|
779
|
-
if (callback(current, index, this)) {
|
|
756
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
780
757
|
filteredList.push(current);
|
|
781
758
|
}
|
|
782
759
|
index++;
|
|
@@ -790,21 +767,27 @@ export class DoublyLinkedList<E = any> {
|
|
|
790
767
|
*/
|
|
791
768
|
|
|
792
769
|
/**
|
|
793
|
-
* Time Complexity: O(n)
|
|
770
|
+
* Time Complexity: O(n)
|
|
794
771
|
* Space Complexity: O(n)
|
|
795
772
|
*
|
|
796
|
-
* The `map` function
|
|
797
|
-
*
|
|
798
|
-
* @param callback - The callback parameter is a function that
|
|
799
|
-
*
|
|
800
|
-
* DoublyLinkedList
|
|
801
|
-
*
|
|
802
|
-
|
|
803
|
-
|
|
773
|
+
* The `map` function creates a new DoublyLinkedList by applying a callback function to each element
|
|
774
|
+
* in the original list.
|
|
775
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
776
|
+
* DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
|
|
777
|
+
* and the DoublyLinkedList itself. The callback function should return a value that will be added to
|
|
778
|
+
* the new DoublyLinkedList that
|
|
779
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
780
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
781
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
782
|
+
* @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
|
|
783
|
+
* of applying the provided `callback` function to each element in the original `DoublyLinkedList`
|
|
784
|
+
* object.
|
|
785
|
+
*/
|
|
786
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T> {
|
|
804
787
|
const mappedList = new DoublyLinkedList<T>();
|
|
805
788
|
let index = 0;
|
|
806
789
|
for (const current of this) {
|
|
807
|
-
mappedList.push(callback(current, index, this));
|
|
790
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
808
791
|
index++;
|
|
809
792
|
}
|
|
810
793
|
|
|
@@ -816,31 +799,19 @@ export class DoublyLinkedList<E = any> {
|
|
|
816
799
|
* Space Complexity: O(n)
|
|
817
800
|
*/
|
|
818
801
|
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
821
|
-
* Space Complexity: O(n)
|
|
822
|
-
*
|
|
823
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
824
|
-
* single value.
|
|
825
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
826
|
-
* used to perform a specific operation on each element of the linked list.
|
|
827
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
828
|
-
* point for the reduction operation.
|
|
829
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
830
|
-
* elements in the linked list.
|
|
831
|
-
*/
|
|
832
|
-
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T {
|
|
833
|
-
let accumulator = initialValue;
|
|
834
|
-
let index = 0;
|
|
835
|
-
for (const current of this) {
|
|
836
|
-
accumulator = callback(accumulator, current, index, this);
|
|
837
|
-
index++;
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
return accumulator;
|
|
841
|
-
}
|
|
842
|
-
|
|
843
802
|
print(): void {
|
|
844
803
|
console.log([...this]);
|
|
845
804
|
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
808
|
+
*/
|
|
809
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
810
|
+
let current = this.head;
|
|
811
|
+
|
|
812
|
+
while (current) {
|
|
813
|
+
yield current.value;
|
|
814
|
+
current = current.next;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
846
817
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { IterableElementBase } from "../base";
|
|
2
|
+
import { ElementCallback } from "../../types";
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* data-structure-typed
|
|
3
6
|
*
|
|
@@ -20,11 +23,12 @@ export class SinglyLinkedListNode<E = any> {
|
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
export class SinglyLinkedList<E = any> {
|
|
26
|
+
export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
24
27
|
/**
|
|
25
28
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
29
|
*/
|
|
27
30
|
constructor(elements?: Iterable<E>) {
|
|
31
|
+
super();
|
|
28
32
|
this._head = undefined;
|
|
29
33
|
this._tail = undefined;
|
|
30
34
|
this._length = 0;
|
|
@@ -160,12 +164,12 @@ export class SinglyLinkedList<E = any> {
|
|
|
160
164
|
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
161
165
|
* Space Complexity: O(1) - Constant space.
|
|
162
166
|
*
|
|
163
|
-
* The `
|
|
167
|
+
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
164
168
|
* pointers accordingly.
|
|
165
169
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
166
170
|
* the linked list is empty, it returns `undefined`.
|
|
167
171
|
*/
|
|
168
|
-
|
|
172
|
+
pollLast(): E | undefined {
|
|
169
173
|
return this.pop();
|
|
170
174
|
}
|
|
171
175
|
|
|
@@ -198,10 +202,10 @@ export class SinglyLinkedList<E = any> {
|
|
|
198
202
|
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
199
203
|
* Space Complexity: O(1) - Constant space.
|
|
200
204
|
*
|
|
201
|
-
* The `
|
|
205
|
+
* The `pollFirst()` function removes and returns the value of the first node in a linked list.
|
|
202
206
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
203
207
|
*/
|
|
204
|
-
|
|
208
|
+
pollFirst(): E | undefined {
|
|
205
209
|
return this.shift();
|
|
206
210
|
}
|
|
207
211
|
|
|
@@ -670,59 +674,32 @@ export class SinglyLinkedList<E = any> {
|
|
|
670
674
|
}
|
|
671
675
|
|
|
672
676
|
/**
|
|
673
|
-
*
|
|
674
|
-
*/
|
|
675
|
-
* [Symbol.iterator]() {
|
|
676
|
-
let current = this.head;
|
|
677
|
-
|
|
678
|
-
while (current) {
|
|
679
|
-
yield current.value;
|
|
680
|
-
current = current.next;
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
/**
|
|
685
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
686
|
-
* Space Complexity: O(1)
|
|
687
|
-
*/
|
|
688
|
-
|
|
689
|
-
/**
|
|
690
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
691
|
-
* Space Complexity: O(1)
|
|
692
|
-
*
|
|
693
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
694
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
695
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
696
|
-
* current node in the linked list.
|
|
697
|
-
*/
|
|
698
|
-
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void {
|
|
699
|
-
let index = 0;
|
|
700
|
-
for (const el of this) {
|
|
701
|
-
callback(el, index, this);
|
|
702
|
-
index++;
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
/**
|
|
707
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
677
|
+
* Time Complexity: O(n)
|
|
708
678
|
* Space Complexity: O(n)
|
|
709
679
|
*/
|
|
710
680
|
|
|
711
681
|
/**
|
|
712
|
-
* Time Complexity: O(n)
|
|
682
|
+
* Time Complexity: O(n)
|
|
713
683
|
* Space Complexity: O(n)
|
|
714
684
|
*
|
|
715
|
-
* The `filter` function
|
|
716
|
-
*
|
|
717
|
-
*
|
|
718
|
-
*
|
|
719
|
-
*
|
|
720
|
-
|
|
721
|
-
|
|
685
|
+
* The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
|
|
686
|
+
* list and applying a callback function to each element to determine if it should be included in the
|
|
687
|
+
* filtered list.
|
|
688
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
689
|
+
* list. It takes three arguments: the current element, the index of the current element, and the
|
|
690
|
+
* list itself. The callback function should return a boolean value indicating whether the current
|
|
691
|
+
* element should be included in the filtered list or not
|
|
692
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
693
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
694
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
695
|
+
* @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
|
|
696
|
+
* elements that pass the filter condition specified by the `callback` function.
|
|
697
|
+
*/
|
|
698
|
+
filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E> {
|
|
722
699
|
const filteredList = new SinglyLinkedList<E>();
|
|
723
700
|
let index = 0;
|
|
724
701
|
for (const current of this) {
|
|
725
|
-
if (callback(current, index, this)) {
|
|
702
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
726
703
|
filteredList.push(current);
|
|
727
704
|
}
|
|
728
705
|
index++;
|
|
@@ -730,27 +707,30 @@ export class SinglyLinkedList<E = any> {
|
|
|
730
707
|
return filteredList;
|
|
731
708
|
}
|
|
732
709
|
|
|
710
|
+
|
|
733
711
|
/**
|
|
734
712
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
735
713
|
* Space Complexity: O(n)
|
|
736
714
|
*/
|
|
737
|
-
|
|
738
715
|
/**
|
|
739
|
-
* Time Complexity: O(n)
|
|
716
|
+
* Time Complexity: O(n)
|
|
740
717
|
* Space Complexity: O(n)
|
|
741
718
|
*
|
|
742
|
-
* The `map` function
|
|
743
|
-
*
|
|
744
|
-
* @param callback - The callback parameter is a function that
|
|
745
|
-
* the
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
|
|
749
|
-
|
|
719
|
+
* The `map` function creates a new SinglyLinkedList by applying a callback function to each element
|
|
720
|
+
* of the original list.
|
|
721
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
722
|
+
* the linked list. It takes three arguments:
|
|
723
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
724
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
725
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
726
|
+
* @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
|
|
727
|
+
* of applying the provided `callback` function to each element in the original list.
|
|
728
|
+
*/
|
|
729
|
+
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T> {
|
|
750
730
|
const mappedList = new SinglyLinkedList<T>();
|
|
751
731
|
let index = 0;
|
|
752
732
|
for (const current of this) {
|
|
753
|
-
mappedList.push(callback(current, index, this));
|
|
733
|
+
mappedList.push(callback.call(thisArg, current, index, this));
|
|
754
734
|
index++;
|
|
755
735
|
}
|
|
756
736
|
|
|
@@ -762,31 +742,16 @@ export class SinglyLinkedList<E = any> {
|
|
|
762
742
|
* Space Complexity: O(n)
|
|
763
743
|
*/
|
|
764
744
|
|
|
765
|
-
/**
|
|
766
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
767
|
-
* Space Complexity: O(n)
|
|
768
|
-
*
|
|
769
|
-
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
770
|
-
* single value.
|
|
771
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
772
|
-
* used to perform a specific operation on each element of the linked list.
|
|
773
|
-
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
774
|
-
* point for the reduction operation.
|
|
775
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
776
|
-
* elements in the linked list.
|
|
777
|
-
*/
|
|
778
|
-
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T {
|
|
779
|
-
let accumulator = initialValue;
|
|
780
|
-
let index = 0;
|
|
781
|
-
for (const current of this) {
|
|
782
|
-
accumulator = callback(accumulator, current, index, this);
|
|
783
|
-
index++;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
return accumulator;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
745
|
print(): void {
|
|
790
746
|
console.log([...this]);
|
|
791
747
|
}
|
|
748
|
+
|
|
749
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
750
|
+
let current = this.head;
|
|
751
|
+
|
|
752
|
+
while (current) {
|
|
753
|
+
yield current.value;
|
|
754
|
+
current = current.next;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
792
757
|
}
|