directed-graph-typed 1.39.5 → 1.40.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/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -14
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -27
- package/dist/data-structures/binary-tree/binary-tree.js +25 -46
- package/dist/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/data-structures/binary-tree/bst.js +16 -16
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
- package/dist/data-structures/binary-tree/rb-tree.js +5 -11
- package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
- package/dist/data-structures/binary-tree/segment-tree.js +20 -68
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
- package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
- package/dist/data-structures/graph/abstract-graph.js +18 -57
- package/dist/data-structures/graph/directed-graph.d.ts +16 -22
- package/dist/data-structures/graph/directed-graph.js +17 -35
- package/dist/data-structures/graph/map-graph.d.ts +13 -19
- package/dist/data-structures/graph/map-graph.js +15 -33
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
- package/dist/data-structures/graph/undirected-graph.js +15 -28
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +10 -13
- package/dist/data-structures/hash/hash-table.js +8 -17
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +34 -42
- package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
- package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/interfaces/graph.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +15 -17
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +35 -60
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/rb-tree.ts +19 -27
- package/src/data-structures/binary-tree/segment-tree.ts +25 -92
- package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
- package/src/data-structures/graph/abstract-graph.ts +42 -88
- package/src/data-structures/graph/directed-graph.ts +29 -55
- package/src/data-structures/graph/map-graph.ts +20 -45
- package/src/data-structures/graph/undirected-graph.ts +24 -41
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +15 -27
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +81 -115
- package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/interfaces/graph.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -6,34 +6,26 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export declare class DoublyLinkedListNode<E = any> {
|
|
9
|
+
value: E;
|
|
10
|
+
next: DoublyLinkedListNode<E> | null;
|
|
11
|
+
prev: DoublyLinkedListNode<E> | null;
|
|
9
12
|
/**
|
|
10
13
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
11
|
-
* @param {E}
|
|
14
|
+
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
12
15
|
* is defined as a generic type "E".
|
|
13
16
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
private _val;
|
|
16
|
-
get val(): E;
|
|
17
|
-
set val(value: E);
|
|
18
|
-
private _next;
|
|
19
|
-
get next(): DoublyLinkedListNode<E> | null;
|
|
20
|
-
set next(value: DoublyLinkedListNode<E> | null);
|
|
21
|
-
private _prev;
|
|
22
|
-
get prev(): DoublyLinkedListNode<E> | null;
|
|
23
|
-
set prev(value: DoublyLinkedListNode<E> | null);
|
|
17
|
+
constructor(value: E);
|
|
24
18
|
}
|
|
25
19
|
export declare class DoublyLinkedList<E = any> {
|
|
26
20
|
/**
|
|
27
21
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
22
|
*/
|
|
29
23
|
constructor();
|
|
30
|
-
|
|
24
|
+
protected _head: DoublyLinkedListNode<E> | null;
|
|
31
25
|
get head(): DoublyLinkedListNode<E> | null;
|
|
32
|
-
|
|
33
|
-
private _tail;
|
|
26
|
+
protected _tail: DoublyLinkedListNode<E> | null;
|
|
34
27
|
get tail(): DoublyLinkedListNode<E> | null;
|
|
35
|
-
|
|
36
|
-
private _length;
|
|
28
|
+
protected _length: number;
|
|
37
29
|
get length(): number;
|
|
38
30
|
get size(): number;
|
|
39
31
|
/**
|
|
@@ -45,23 +37,23 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
45
37
|
static fromArray<E>(data: E[]): DoublyLinkedList<E>;
|
|
46
38
|
/**
|
|
47
39
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
48
|
-
* @param {E}
|
|
40
|
+
* @param {E} value - The value to be added to the linked list.
|
|
49
41
|
*/
|
|
50
|
-
push(
|
|
42
|
+
push(value: E): void;
|
|
51
43
|
/**
|
|
52
44
|
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
53
|
-
* @param {E}
|
|
45
|
+
* @param {E} value - The value to be added to the linked list.
|
|
54
46
|
*/
|
|
55
|
-
addLast(
|
|
47
|
+
addLast(value: E): void;
|
|
56
48
|
/**
|
|
57
49
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
58
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
50
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
59
51
|
* list is empty, it returns null.
|
|
60
52
|
*/
|
|
61
53
|
pop(): E | undefined;
|
|
62
54
|
/**
|
|
63
55
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
64
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
56
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
65
57
|
* list is empty, it returns null.
|
|
66
58
|
*/
|
|
67
59
|
popLast(): E | undefined;
|
|
@@ -79,16 +71,16 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
79
71
|
popFirst(): E | undefined;
|
|
80
72
|
/**
|
|
81
73
|
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
82
|
-
* @param {E}
|
|
74
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
83
75
|
* doubly linked list.
|
|
84
76
|
*/
|
|
85
|
-
unshift(
|
|
77
|
+
unshift(value: E): void;
|
|
86
78
|
/**
|
|
87
79
|
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
88
|
-
* @param {E}
|
|
80
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
89
81
|
* doubly linked list.
|
|
90
82
|
*/
|
|
91
|
-
addFirst(
|
|
83
|
+
addFirst(value: E): void;
|
|
92
84
|
/**
|
|
93
85
|
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
94
86
|
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
@@ -119,21 +111,21 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
119
111
|
/**
|
|
120
112
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
121
113
|
* node if found, otherwise it returns null.
|
|
122
|
-
* @param {E}
|
|
123
|
-
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `
|
|
114
|
+
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
|
115
|
+
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
|
124
116
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
125
117
|
*/
|
|
126
|
-
getNode(
|
|
118
|
+
getNode(value: E | null): DoublyLinkedListNode<E> | null;
|
|
127
119
|
/**
|
|
128
120
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
129
121
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
130
122
|
* DoublyLinkedList. It is of type number.
|
|
131
|
-
* @param {E}
|
|
123
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
132
124
|
* specified index.
|
|
133
125
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
134
126
|
* if the index is out of bounds.
|
|
135
127
|
*/
|
|
136
|
-
insertAt(index: number,
|
|
128
|
+
insertAt(index: number, value: E): boolean;
|
|
137
129
|
/**
|
|
138
130
|
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
139
131
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -182,15 +174,15 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
182
174
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
183
175
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
184
176
|
*/
|
|
185
|
-
find(callback: (
|
|
177
|
+
find(callback: (value: E) => boolean): E | null;
|
|
186
178
|
/**
|
|
187
179
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
188
|
-
* @param {E}
|
|
180
|
+
* @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
|
|
189
181
|
* that we are searching for in the linked list.
|
|
190
|
-
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `
|
|
182
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
|
|
191
183
|
* list. If the value is not found, it returns -1.
|
|
192
184
|
*/
|
|
193
|
-
indexOf(
|
|
185
|
+
indexOf(value: E): number;
|
|
194
186
|
/**
|
|
195
187
|
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
196
188
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
@@ -199,7 +191,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
199
191
|
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
200
192
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
201
193
|
*/
|
|
202
|
-
findBackward(callback: (
|
|
194
|
+
findBackward(callback: (value: E) => boolean): E | null;
|
|
203
195
|
/**
|
|
204
196
|
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
205
197
|
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
@@ -211,11 +203,11 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
211
203
|
reverse(): void;
|
|
212
204
|
/**
|
|
213
205
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
214
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
206
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
215
207
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
216
208
|
* current node in the linked list.
|
|
217
209
|
*/
|
|
218
|
-
forEach(callback: (
|
|
210
|
+
forEach(callback: (value: E, index: number) => void): void;
|
|
219
211
|
/**
|
|
220
212
|
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
221
213
|
* DoublyLinkedList with the transformed values.
|
|
@@ -224,7 +216,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
224
216
|
* DoublyLinkedList).
|
|
225
217
|
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
226
218
|
*/
|
|
227
|
-
map<U>(callback: (
|
|
219
|
+
map<U>(callback: (value: E) => U): DoublyLinkedList<U>;
|
|
228
220
|
/**
|
|
229
221
|
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
230
222
|
* elements that satisfy the given callback function.
|
|
@@ -232,18 +224,18 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
232
224
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
233
225
|
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
234
226
|
*/
|
|
235
|
-
filter(callback: (
|
|
227
|
+
filter(callback: (value: E) => boolean): DoublyLinkedList<E>;
|
|
236
228
|
/**
|
|
237
229
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
238
230
|
* single value.
|
|
239
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
231
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
240
232
|
* used to perform a specific operation on each element of the linked list.
|
|
241
233
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
242
234
|
* point for the reduction operation.
|
|
243
235
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
244
236
|
* elements in the linked list.
|
|
245
237
|
*/
|
|
246
|
-
reduce<U>(callback: (accumulator: U,
|
|
238
|
+
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U;
|
|
247
239
|
/**
|
|
248
240
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
249
241
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -11,31 +11,13 @@ exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
|
11
11
|
class DoublyLinkedListNode {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
14
|
-
* @param {E}
|
|
14
|
+
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
15
15
|
* is defined as a generic type "E".
|
|
16
16
|
*/
|
|
17
|
-
constructor(
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
}
|
|
22
|
-
get val() {
|
|
23
|
-
return this._val;
|
|
24
|
-
}
|
|
25
|
-
set val(value) {
|
|
26
|
-
this._val = value;
|
|
27
|
-
}
|
|
28
|
-
get next() {
|
|
29
|
-
return this._next;
|
|
30
|
-
}
|
|
31
|
-
set next(value) {
|
|
32
|
-
this._next = value;
|
|
33
|
-
}
|
|
34
|
-
get prev() {
|
|
35
|
-
return this._prev;
|
|
36
|
-
}
|
|
37
|
-
set prev(value) {
|
|
38
|
-
this._prev = value;
|
|
17
|
+
constructor(value) {
|
|
18
|
+
this.value = value;
|
|
19
|
+
this.next = null;
|
|
20
|
+
this.prev = null;
|
|
39
21
|
}
|
|
40
22
|
}
|
|
41
23
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
@@ -51,15 +33,9 @@ class DoublyLinkedList {
|
|
|
51
33
|
get head() {
|
|
52
34
|
return this._head;
|
|
53
35
|
}
|
|
54
|
-
set head(value) {
|
|
55
|
-
this._head = value;
|
|
56
|
-
}
|
|
57
36
|
get tail() {
|
|
58
37
|
return this._tail;
|
|
59
38
|
}
|
|
60
|
-
set tail(value) {
|
|
61
|
-
this._tail = value;
|
|
62
|
-
}
|
|
63
39
|
get length() {
|
|
64
40
|
return this._length;
|
|
65
41
|
}
|
|
@@ -81,31 +57,31 @@ class DoublyLinkedList {
|
|
|
81
57
|
}
|
|
82
58
|
/**
|
|
83
59
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
84
|
-
* @param {E}
|
|
60
|
+
* @param {E} value - The value to be added to the linked list.
|
|
85
61
|
*/
|
|
86
|
-
push(
|
|
87
|
-
const newNode = new DoublyLinkedListNode(
|
|
62
|
+
push(value) {
|
|
63
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
88
64
|
if (!this.head) {
|
|
89
|
-
this.
|
|
90
|
-
this.
|
|
65
|
+
this._head = newNode;
|
|
66
|
+
this._tail = newNode;
|
|
91
67
|
}
|
|
92
68
|
else {
|
|
93
69
|
newNode.prev = this.tail;
|
|
94
70
|
this.tail.next = newNode;
|
|
95
|
-
this.
|
|
71
|
+
this._tail = newNode;
|
|
96
72
|
}
|
|
97
73
|
this._length++;
|
|
98
74
|
}
|
|
99
75
|
/**
|
|
100
76
|
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
101
|
-
* @param {E}
|
|
77
|
+
* @param {E} value - The value to be added to the linked list.
|
|
102
78
|
*/
|
|
103
|
-
addLast(
|
|
104
|
-
this.push(
|
|
79
|
+
addLast(value) {
|
|
80
|
+
this.push(value);
|
|
105
81
|
}
|
|
106
82
|
/**
|
|
107
83
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
108
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
84
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
109
85
|
* list is empty, it returns null.
|
|
110
86
|
*/
|
|
111
87
|
pop() {
|
|
@@ -113,19 +89,19 @@ class DoublyLinkedList {
|
|
|
113
89
|
return undefined;
|
|
114
90
|
const removedNode = this.tail;
|
|
115
91
|
if (this.head === this.tail) {
|
|
116
|
-
this.
|
|
117
|
-
this.
|
|
92
|
+
this._head = null;
|
|
93
|
+
this._tail = null;
|
|
118
94
|
}
|
|
119
95
|
else {
|
|
120
|
-
this.
|
|
96
|
+
this._tail = removedNode.prev;
|
|
121
97
|
this.tail.next = null;
|
|
122
98
|
}
|
|
123
99
|
this._length--;
|
|
124
|
-
return removedNode.
|
|
100
|
+
return removedNode.value;
|
|
125
101
|
}
|
|
126
102
|
/**
|
|
127
103
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
128
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
104
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
129
105
|
* list is empty, it returns null.
|
|
130
106
|
*/
|
|
131
107
|
popLast() {
|
|
@@ -141,15 +117,15 @@ class DoublyLinkedList {
|
|
|
141
117
|
return undefined;
|
|
142
118
|
const removedNode = this.head;
|
|
143
119
|
if (this.head === this.tail) {
|
|
144
|
-
this.
|
|
145
|
-
this.
|
|
120
|
+
this._head = null;
|
|
121
|
+
this._tail = null;
|
|
146
122
|
}
|
|
147
123
|
else {
|
|
148
|
-
this.
|
|
124
|
+
this._head = removedNode.next;
|
|
149
125
|
this.head.prev = null;
|
|
150
126
|
}
|
|
151
127
|
this._length--;
|
|
152
|
-
return removedNode.
|
|
128
|
+
return removedNode.value;
|
|
153
129
|
}
|
|
154
130
|
/**
|
|
155
131
|
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
@@ -161,29 +137,29 @@ class DoublyLinkedList {
|
|
|
161
137
|
}
|
|
162
138
|
/**
|
|
163
139
|
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
164
|
-
* @param {E}
|
|
140
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
165
141
|
* doubly linked list.
|
|
166
142
|
*/
|
|
167
|
-
unshift(
|
|
168
|
-
const newNode = new DoublyLinkedListNode(
|
|
143
|
+
unshift(value) {
|
|
144
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
169
145
|
if (!this.head) {
|
|
170
|
-
this.
|
|
171
|
-
this.
|
|
146
|
+
this._head = newNode;
|
|
147
|
+
this._tail = newNode;
|
|
172
148
|
}
|
|
173
149
|
else {
|
|
174
150
|
newNode.next = this.head;
|
|
175
151
|
this.head.prev = newNode;
|
|
176
|
-
this.
|
|
152
|
+
this._head = newNode;
|
|
177
153
|
}
|
|
178
154
|
this._length++;
|
|
179
155
|
}
|
|
180
156
|
/**
|
|
181
157
|
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
182
|
-
* @param {E}
|
|
158
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
183
159
|
* doubly linked list.
|
|
184
160
|
*/
|
|
185
|
-
addFirst(
|
|
186
|
-
this.unshift(
|
|
161
|
+
addFirst(value) {
|
|
162
|
+
this.unshift(value);
|
|
187
163
|
}
|
|
188
164
|
/**
|
|
189
165
|
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
@@ -191,7 +167,7 @@ class DoublyLinkedList {
|
|
|
191
167
|
*/
|
|
192
168
|
getFirst() {
|
|
193
169
|
var _a;
|
|
194
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.
|
|
170
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
195
171
|
}
|
|
196
172
|
/**
|
|
197
173
|
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
@@ -199,7 +175,7 @@ class DoublyLinkedList {
|
|
|
199
175
|
*/
|
|
200
176
|
getLast() {
|
|
201
177
|
var _a;
|
|
202
|
-
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.
|
|
178
|
+
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.value;
|
|
203
179
|
}
|
|
204
180
|
/**
|
|
205
181
|
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
@@ -215,7 +191,7 @@ class DoublyLinkedList {
|
|
|
215
191
|
for (let i = 0; i < index; i++) {
|
|
216
192
|
current = current.next;
|
|
217
193
|
}
|
|
218
|
-
return current.
|
|
194
|
+
return current.value;
|
|
219
195
|
}
|
|
220
196
|
/**
|
|
221
197
|
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
|
|
@@ -237,14 +213,14 @@ class DoublyLinkedList {
|
|
|
237
213
|
/**
|
|
238
214
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
239
215
|
* node if found, otherwise it returns null.
|
|
240
|
-
* @param {E}
|
|
241
|
-
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `
|
|
216
|
+
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
|
217
|
+
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
|
242
218
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
243
219
|
*/
|
|
244
|
-
getNode(
|
|
220
|
+
getNode(value) {
|
|
245
221
|
let current = this.head;
|
|
246
222
|
while (current) {
|
|
247
|
-
if (current.
|
|
223
|
+
if (current.value === value) {
|
|
248
224
|
return current;
|
|
249
225
|
}
|
|
250
226
|
current = current.next;
|
|
@@ -255,23 +231,23 @@ class DoublyLinkedList {
|
|
|
255
231
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
256
232
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
257
233
|
* DoublyLinkedList. It is of type number.
|
|
258
|
-
* @param {E}
|
|
234
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
259
235
|
* specified index.
|
|
260
236
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
261
237
|
* if the index is out of bounds.
|
|
262
238
|
*/
|
|
263
|
-
insertAt(index,
|
|
239
|
+
insertAt(index, value) {
|
|
264
240
|
if (index < 0 || index > this.length)
|
|
265
241
|
return false;
|
|
266
242
|
if (index === 0) {
|
|
267
|
-
this.unshift(
|
|
243
|
+
this.unshift(value);
|
|
268
244
|
return true;
|
|
269
245
|
}
|
|
270
246
|
if (index === this.length) {
|
|
271
|
-
this.push(
|
|
247
|
+
this.push(value);
|
|
272
248
|
return true;
|
|
273
249
|
}
|
|
274
|
-
const newNode = new DoublyLinkedListNode(
|
|
250
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
275
251
|
const prevNode = this.getNodeAt(index - 1);
|
|
276
252
|
const nextNode = prevNode.next;
|
|
277
253
|
newNode.prev = prevNode;
|
|
@@ -308,7 +284,7 @@ class DoublyLinkedList {
|
|
|
308
284
|
newNode.next = existingNode;
|
|
309
285
|
existingNode.prev = newNode;
|
|
310
286
|
if (existingNode === this.head) {
|
|
311
|
-
this.
|
|
287
|
+
this._head = newNode;
|
|
312
288
|
}
|
|
313
289
|
this._length++;
|
|
314
290
|
return true;
|
|
@@ -335,7 +311,7 @@ class DoublyLinkedList {
|
|
|
335
311
|
prevNode.next = nextNode;
|
|
336
312
|
nextNode.prev = prevNode;
|
|
337
313
|
this._length--;
|
|
338
|
-
return removedNode.
|
|
314
|
+
return removedNode.value;
|
|
339
315
|
}
|
|
340
316
|
/**
|
|
341
317
|
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
@@ -378,7 +354,7 @@ class DoublyLinkedList {
|
|
|
378
354
|
const array = [];
|
|
379
355
|
let current = this.head;
|
|
380
356
|
while (current) {
|
|
381
|
-
array.push(current.
|
|
357
|
+
array.push(current.value);
|
|
382
358
|
current = current.next;
|
|
383
359
|
}
|
|
384
360
|
return array;
|
|
@@ -408,8 +384,8 @@ class DoublyLinkedList {
|
|
|
408
384
|
find(callback) {
|
|
409
385
|
let current = this.head;
|
|
410
386
|
while (current) {
|
|
411
|
-
if (callback(current.
|
|
412
|
-
return current.
|
|
387
|
+
if (callback(current.value)) {
|
|
388
|
+
return current.value;
|
|
413
389
|
}
|
|
414
390
|
current = current.next;
|
|
415
391
|
}
|
|
@@ -417,16 +393,16 @@ class DoublyLinkedList {
|
|
|
417
393
|
}
|
|
418
394
|
/**
|
|
419
395
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
420
|
-
* @param {E}
|
|
396
|
+
* @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
|
|
421
397
|
* that we are searching for in the linked list.
|
|
422
|
-
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `
|
|
398
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
|
|
423
399
|
* list. If the value is not found, it returns -1.
|
|
424
400
|
*/
|
|
425
|
-
indexOf(
|
|
401
|
+
indexOf(value) {
|
|
426
402
|
let index = 0;
|
|
427
403
|
let current = this.head;
|
|
428
404
|
while (current) {
|
|
429
|
-
if (current.
|
|
405
|
+
if (current.value === value) {
|
|
430
406
|
return index;
|
|
431
407
|
}
|
|
432
408
|
index++;
|
|
@@ -445,8 +421,8 @@ class DoublyLinkedList {
|
|
|
445
421
|
findBackward(callback) {
|
|
446
422
|
let current = this.tail;
|
|
447
423
|
while (current) {
|
|
448
|
-
if (callback(current.
|
|
449
|
-
return current.
|
|
424
|
+
if (callback(current.value)) {
|
|
425
|
+
return current.value;
|
|
450
426
|
}
|
|
451
427
|
current = current.prev;
|
|
452
428
|
}
|
|
@@ -460,7 +436,7 @@ class DoublyLinkedList {
|
|
|
460
436
|
const array = [];
|
|
461
437
|
let current = this.tail;
|
|
462
438
|
while (current) {
|
|
463
|
-
array.push(current.
|
|
439
|
+
array.push(current.value);
|
|
464
440
|
current = current.prev;
|
|
465
441
|
}
|
|
466
442
|
return array;
|
|
@@ -470,7 +446,7 @@ class DoublyLinkedList {
|
|
|
470
446
|
*/
|
|
471
447
|
reverse() {
|
|
472
448
|
let current = this.head;
|
|
473
|
-
[this.
|
|
449
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
474
450
|
while (current) {
|
|
475
451
|
const next = current.next;
|
|
476
452
|
[current.prev, current.next] = [current.next, current.prev];
|
|
@@ -479,7 +455,7 @@ class DoublyLinkedList {
|
|
|
479
455
|
}
|
|
480
456
|
/**
|
|
481
457
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
482
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
458
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
483
459
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
484
460
|
* current node in the linked list.
|
|
485
461
|
*/
|
|
@@ -487,7 +463,7 @@ class DoublyLinkedList {
|
|
|
487
463
|
let current = this.head;
|
|
488
464
|
let index = 0;
|
|
489
465
|
while (current) {
|
|
490
|
-
callback(current.
|
|
466
|
+
callback(current.value, index);
|
|
491
467
|
current = current.next;
|
|
492
468
|
index++;
|
|
493
469
|
}
|
|
@@ -504,7 +480,7 @@ class DoublyLinkedList {
|
|
|
504
480
|
const mappedList = new DoublyLinkedList();
|
|
505
481
|
let current = this.head;
|
|
506
482
|
while (current) {
|
|
507
|
-
mappedList.push(callback(current.
|
|
483
|
+
mappedList.push(callback(current.value));
|
|
508
484
|
current = current.next;
|
|
509
485
|
}
|
|
510
486
|
return mappedList;
|
|
@@ -520,8 +496,8 @@ class DoublyLinkedList {
|
|
|
520
496
|
const filteredList = new DoublyLinkedList();
|
|
521
497
|
let current = this.head;
|
|
522
498
|
while (current) {
|
|
523
|
-
if (callback(current.
|
|
524
|
-
filteredList.push(current.
|
|
499
|
+
if (callback(current.value)) {
|
|
500
|
+
filteredList.push(current.value);
|
|
525
501
|
}
|
|
526
502
|
current = current.next;
|
|
527
503
|
}
|
|
@@ -530,7 +506,7 @@ class DoublyLinkedList {
|
|
|
530
506
|
/**
|
|
531
507
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
532
508
|
* single value.
|
|
533
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
509
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
534
510
|
* used to perform a specific operation on each element of the linked list.
|
|
535
511
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
536
512
|
* point for the reduction operation.
|
|
@@ -541,7 +517,7 @@ class DoublyLinkedList {
|
|
|
541
517
|
let accumulator = initialValue;
|
|
542
518
|
let current = this.head;
|
|
543
519
|
while (current) {
|
|
544
|
-
accumulator = callback(accumulator, current.
|
|
520
|
+
accumulator = callback(accumulator, current.value);
|
|
545
521
|
current = current.next;
|
|
546
522
|
}
|
|
547
523
|
return accumulator;
|
|
@@ -572,7 +548,7 @@ class DoublyLinkedList {
|
|
|
572
548
|
newNode.prev = existingNode;
|
|
573
549
|
existingNode.next = newNode;
|
|
574
550
|
if (existingNode === this.tail) {
|
|
575
|
-
this.
|
|
551
|
+
this._tail = newNode;
|
|
576
552
|
}
|
|
577
553
|
this._length++;
|
|
578
554
|
return true;
|
|
@@ -585,7 +561,7 @@ class DoublyLinkedList {
|
|
|
585
561
|
*[Symbol.iterator]() {
|
|
586
562
|
let current = this.head;
|
|
587
563
|
while (current) {
|
|
588
|
-
yield current.
|
|
564
|
+
yield current.value;
|
|
589
565
|
current = current.next;
|
|
590
566
|
}
|
|
591
567
|
}
|