min-heap-typed 1.39.1 → 1.39.2
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/binary-tree.d.ts +11 -1
- package/dist/data-structures/binary-tree/binary-tree.js +38 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/data-structures/queue/deque.d.ts +20 -20
- package/dist/data-structures/queue/deque.js +22 -22
- package/dist/data-structures/queue/queue.d.ts +3 -3
- package/dist/data-structures/queue/queue.js +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +47 -5
- package/src/data-structures/binary-tree/bst.ts +1 -2
- package/src/data-structures/binary-tree/rb-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +1 -2
- package/src/data-structures/graph/abstract-graph.ts +10 -11
- package/src/data-structures/graph/directed-graph.ts +1 -2
- package/src/data-structures/graph/undirected-graph.ts +4 -5
- package/src/data-structures/hash/hash-map.ts +1 -1
- 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 +2 -2
- 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 +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +119 -14
- package/src/data-structures/matrix/matrix.ts +1 -1
- 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 +27 -26
- package/src/data-structures/queue/queue.ts +4 -4
- 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
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback } from '../../types';
|
|
9
9
|
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -384,6 +384,16 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
384
384
|
* @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
385
385
|
*/
|
|
386
386
|
morris<C extends OneParamCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
|
|
387
|
+
/**
|
|
388
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
389
|
+
* either an iterative or recursive manner.
|
|
390
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
391
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
392
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
393
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
394
|
+
* binary tree nodes in a specific order.
|
|
395
|
+
*/
|
|
396
|
+
[Symbol.iterator](node?: N | null): Generator<BinaryTreeNodeKey, void, undefined>;
|
|
387
397
|
/**
|
|
388
398
|
* Swap the data of two nodes in the binary tree.
|
|
389
399
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -1028,6 +1028,44 @@ class BinaryTree {
|
|
|
1028
1028
|
}
|
|
1029
1029
|
return ans;
|
|
1030
1030
|
}
|
|
1031
|
+
/**
|
|
1032
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1033
|
+
* either an iterative or recursive manner.
|
|
1034
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1035
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1036
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1037
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1038
|
+
* binary tree nodes in a specific order.
|
|
1039
|
+
*/
|
|
1040
|
+
*[Symbol.iterator](node = this.root) {
|
|
1041
|
+
if (!node) {
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
if (this.iterationType === types_1.IterationType.ITERATIVE) {
|
|
1045
|
+
const stack = [];
|
|
1046
|
+
let current = node;
|
|
1047
|
+
while (current || stack.length > 0) {
|
|
1048
|
+
while (current) {
|
|
1049
|
+
stack.push(current);
|
|
1050
|
+
current = current.left;
|
|
1051
|
+
}
|
|
1052
|
+
current = stack.pop();
|
|
1053
|
+
if (current)
|
|
1054
|
+
yield current.key;
|
|
1055
|
+
if (current)
|
|
1056
|
+
current = current.right;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
else {
|
|
1060
|
+
if (node.left) {
|
|
1061
|
+
yield* this[Symbol.iterator](node.left);
|
|
1062
|
+
}
|
|
1063
|
+
yield node.key;
|
|
1064
|
+
if (node.right) {
|
|
1065
|
+
yield* this[Symbol.iterator](node.right);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1031
1069
|
/**
|
|
1032
1070
|
* Swap the data of two nodes in the binary tree.
|
|
1033
1071
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -60,11 +60,11 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
60
60
|
*/
|
|
61
61
|
pop(): E | undefined;
|
|
62
62
|
/**
|
|
63
|
-
* The `
|
|
63
|
+
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
64
64
|
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
65
65
|
* list is empty, it returns null.
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
popLast(): E | undefined;
|
|
68
68
|
/**
|
|
69
69
|
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
70
70
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
@@ -72,11 +72,11 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
72
72
|
*/
|
|
73
73
|
shift(): E | undefined;
|
|
74
74
|
/**
|
|
75
|
-
* The `
|
|
75
|
+
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
76
76
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
77
77
|
* list.
|
|
78
78
|
*/
|
|
79
|
-
|
|
79
|
+
popFirst(): E | undefined;
|
|
80
80
|
/**
|
|
81
81
|
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
82
82
|
* @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
|
@@ -90,15 +90,15 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
90
90
|
*/
|
|
91
91
|
addFirst(val: E): void;
|
|
92
92
|
/**
|
|
93
|
-
* The `
|
|
94
|
-
* @returns The method `
|
|
93
|
+
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
94
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
95
95
|
*/
|
|
96
|
-
|
|
96
|
+
getFirst(): E | undefined;
|
|
97
97
|
/**
|
|
98
|
-
* The `
|
|
99
|
-
* @returns The method `
|
|
98
|
+
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
99
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
100
100
|
*/
|
|
101
|
-
|
|
101
|
+
getLast(): E | undefined;
|
|
102
102
|
/**
|
|
103
103
|
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
104
104
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -123,7 +123,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
123
123
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
124
124
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
125
125
|
*/
|
|
126
|
-
|
|
126
|
+
getNode(val: E | null): DoublyLinkedListNode<E> | null;
|
|
127
127
|
/**
|
|
128
128
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
129
129
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -134,6 +134,17 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
134
134
|
* if the index is out of bounds.
|
|
135
135
|
*/
|
|
136
136
|
insertAt(index: number, val: E): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
139
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
140
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
141
|
+
* itself.
|
|
142
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
143
|
+
* list.
|
|
144
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
145
|
+
* insertion fails.
|
|
146
|
+
*/
|
|
147
|
+
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
137
148
|
/**
|
|
138
149
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
139
150
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -142,8 +153,14 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
142
153
|
* bounds.
|
|
143
154
|
*/
|
|
144
155
|
deleteAt(index: number): E | undefined;
|
|
145
|
-
|
|
146
|
-
|
|
156
|
+
/**
|
|
157
|
+
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
158
|
+
* @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
|
|
159
|
+
* a `DoublyLinkedListNode<E>` object.
|
|
160
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
|
|
161
|
+
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
|
|
162
|
+
*/
|
|
163
|
+
delete(valOrNode: E | DoublyLinkedListNode<E> | null): boolean;
|
|
147
164
|
/**
|
|
148
165
|
* The `toArray` function converts a linked list into an array.
|
|
149
166
|
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
@@ -175,19 +192,19 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
175
192
|
*/
|
|
176
193
|
indexOf(val: E): number;
|
|
177
194
|
/**
|
|
178
|
-
* The `
|
|
195
|
+
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
179
196
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
180
197
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
181
198
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
182
|
-
* @returns The method `
|
|
199
|
+
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
183
200
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
184
201
|
*/
|
|
185
|
-
|
|
202
|
+
findBackward(callback: (val: E) => boolean): E | null;
|
|
186
203
|
/**
|
|
187
|
-
* The `
|
|
188
|
-
* @returns The `
|
|
204
|
+
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
205
|
+
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
189
206
|
*/
|
|
190
|
-
|
|
207
|
+
toArrayBackward(): E[];
|
|
191
208
|
/**
|
|
192
209
|
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
193
210
|
*/
|
|
@@ -227,17 +244,18 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
227
244
|
* elements in the linked list.
|
|
228
245
|
*/
|
|
229
246
|
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
|
|
230
|
-
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
231
|
-
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
232
247
|
/**
|
|
233
|
-
* The `
|
|
248
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
234
249
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
235
|
-
*
|
|
250
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
236
251
|
* itself.
|
|
237
|
-
* @param {E} newValue - The
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* insertion fails.
|
|
252
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
253
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
254
|
+
* existing value or node is not found in the doubly linked list.
|
|
241
255
|
*/
|
|
242
|
-
|
|
256
|
+
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
259
|
+
*/
|
|
260
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
243
261
|
}
|
|
@@ -124,11 +124,11 @@ class DoublyLinkedList {
|
|
|
124
124
|
return removedNode.val;
|
|
125
125
|
}
|
|
126
126
|
/**
|
|
127
|
-
* The `
|
|
127
|
+
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
128
128
|
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
129
129
|
* list is empty, it returns null.
|
|
130
130
|
*/
|
|
131
|
-
|
|
131
|
+
popLast() {
|
|
132
132
|
return this.pop();
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
@@ -152,11 +152,11 @@ class DoublyLinkedList {
|
|
|
152
152
|
return removedNode.val;
|
|
153
153
|
}
|
|
154
154
|
/**
|
|
155
|
-
* The `
|
|
155
|
+
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
156
156
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
157
157
|
* list.
|
|
158
158
|
*/
|
|
159
|
-
|
|
159
|
+
popFirst() {
|
|
160
160
|
return this.shift();
|
|
161
161
|
}
|
|
162
162
|
/**
|
|
@@ -186,18 +186,18 @@ class DoublyLinkedList {
|
|
|
186
186
|
this.unshift(val);
|
|
187
187
|
}
|
|
188
188
|
/**
|
|
189
|
-
* The `
|
|
190
|
-
* @returns The method `
|
|
189
|
+
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
190
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
191
191
|
*/
|
|
192
|
-
|
|
192
|
+
getFirst() {
|
|
193
193
|
var _a;
|
|
194
194
|
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
|
|
195
195
|
}
|
|
196
196
|
/**
|
|
197
|
-
* The `
|
|
198
|
-
* @returns The method `
|
|
197
|
+
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
198
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
199
199
|
*/
|
|
200
|
-
|
|
200
|
+
getLast() {
|
|
201
201
|
var _a;
|
|
202
202
|
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.val;
|
|
203
203
|
}
|
|
@@ -241,7 +241,7 @@ class DoublyLinkedList {
|
|
|
241
241
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
242
242
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
243
243
|
*/
|
|
244
|
-
|
|
244
|
+
getNode(val) {
|
|
245
245
|
let current = this.head;
|
|
246
246
|
while (current) {
|
|
247
247
|
if (current.val === val) {
|
|
@@ -281,6 +281,40 @@ class DoublyLinkedList {
|
|
|
281
281
|
this._length++;
|
|
282
282
|
return true;
|
|
283
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
286
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
287
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
288
|
+
* itself.
|
|
289
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
290
|
+
* list.
|
|
291
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
292
|
+
* insertion fails.
|
|
293
|
+
*/
|
|
294
|
+
insertBefore(existingValueOrNode, newValue) {
|
|
295
|
+
let existingNode;
|
|
296
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
297
|
+
existingNode = existingValueOrNode;
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
301
|
+
}
|
|
302
|
+
if (existingNode) {
|
|
303
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
304
|
+
newNode.prev = existingNode.prev;
|
|
305
|
+
if (existingNode.prev) {
|
|
306
|
+
existingNode.prev.next = newNode;
|
|
307
|
+
}
|
|
308
|
+
newNode.next = existingNode;
|
|
309
|
+
existingNode.prev = newNode;
|
|
310
|
+
if (existingNode === this.head) {
|
|
311
|
+
this.head = newNode;
|
|
312
|
+
}
|
|
313
|
+
this._length++;
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
284
318
|
/**
|
|
285
319
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
286
320
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -316,7 +350,7 @@ class DoublyLinkedList {
|
|
|
316
350
|
node = valOrNode;
|
|
317
351
|
}
|
|
318
352
|
else {
|
|
319
|
-
node = this.
|
|
353
|
+
node = this.getNode(valOrNode);
|
|
320
354
|
}
|
|
321
355
|
if (node) {
|
|
322
356
|
if (node === this.head) {
|
|
@@ -401,14 +435,14 @@ class DoublyLinkedList {
|
|
|
401
435
|
return -1;
|
|
402
436
|
}
|
|
403
437
|
/**
|
|
404
|
-
* The `
|
|
438
|
+
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
405
439
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
406
440
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
407
441
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
408
|
-
* @returns The method `
|
|
442
|
+
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
409
443
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
410
444
|
*/
|
|
411
|
-
|
|
445
|
+
findBackward(callback) {
|
|
412
446
|
let current = this.tail;
|
|
413
447
|
while (current) {
|
|
414
448
|
if (callback(current.val)) {
|
|
@@ -419,10 +453,10 @@ class DoublyLinkedList {
|
|
|
419
453
|
return null;
|
|
420
454
|
}
|
|
421
455
|
/**
|
|
422
|
-
* The `
|
|
423
|
-
* @returns The `
|
|
456
|
+
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
457
|
+
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
424
458
|
*/
|
|
425
|
-
|
|
459
|
+
toArrayBackward() {
|
|
426
460
|
const array = [];
|
|
427
461
|
let current = this.tail;
|
|
428
462
|
while (current) {
|
|
@@ -527,7 +561,7 @@ class DoublyLinkedList {
|
|
|
527
561
|
existingNode = existingValueOrNode;
|
|
528
562
|
}
|
|
529
563
|
else {
|
|
530
|
-
existingNode = this.
|
|
564
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
531
565
|
}
|
|
532
566
|
if (existingNode) {
|
|
533
567
|
const newNode = new DoublyLinkedListNode(newValue);
|
|
@@ -546,38 +580,14 @@ class DoublyLinkedList {
|
|
|
546
580
|
return false;
|
|
547
581
|
}
|
|
548
582
|
/**
|
|
549
|
-
* The
|
|
550
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
551
|
-
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
552
|
-
* itself.
|
|
553
|
-
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
554
|
-
* list.
|
|
555
|
-
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
556
|
-
* insertion fails.
|
|
583
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
557
584
|
*/
|
|
558
|
-
|
|
559
|
-
let
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
else {
|
|
564
|
-
existingNode = this.findNode(existingValueOrNode);
|
|
565
|
-
}
|
|
566
|
-
if (existingNode) {
|
|
567
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
568
|
-
newNode.prev = existingNode.prev;
|
|
569
|
-
if (existingNode.prev) {
|
|
570
|
-
existingNode.prev.next = newNode;
|
|
571
|
-
}
|
|
572
|
-
newNode.next = existingNode;
|
|
573
|
-
existingNode.prev = newNode;
|
|
574
|
-
if (existingNode === this.head) {
|
|
575
|
-
this.head = newNode;
|
|
576
|
-
}
|
|
577
|
-
this._length++;
|
|
578
|
-
return true;
|
|
585
|
+
*[Symbol.iterator]() {
|
|
586
|
+
let current = this.head;
|
|
587
|
+
while (current) {
|
|
588
|
+
yield current.val;
|
|
589
|
+
current = current.next;
|
|
579
590
|
}
|
|
580
|
-
return false;
|
|
581
591
|
}
|
|
582
592
|
}
|
|
583
593
|
exports.DoublyLinkedList = DoublyLinkedList;
|
|
@@ -39,13 +39,18 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
39
39
|
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
40
40
|
*/
|
|
41
41
|
static fromArray<E>(data: E[]): SinglyLinkedList<E>;
|
|
42
|
-
getLength(): number;
|
|
43
42
|
/**
|
|
44
|
-
* The `push` function adds a new node with the given
|
|
45
|
-
* @param {E}
|
|
43
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
44
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
46
45
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
47
46
|
*/
|
|
48
|
-
push(
|
|
47
|
+
push(val: E): void;
|
|
48
|
+
/**
|
|
49
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
50
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
51
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
52
|
+
*/
|
|
53
|
+
addLast(val: E): void;
|
|
49
54
|
/**
|
|
50
55
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
51
56
|
* pointers accordingly.
|
|
@@ -53,17 +58,35 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
53
58
|
* the linked list is empty, it returns `null`.
|
|
54
59
|
*/
|
|
55
60
|
pop(): E | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
63
|
+
* pointers accordingly.
|
|
64
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
65
|
+
* the linked list is empty, it returns `null`.
|
|
66
|
+
*/
|
|
67
|
+
popLast(): E | undefined;
|
|
56
68
|
/**
|
|
57
69
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
58
70
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
59
71
|
*/
|
|
60
72
|
shift(): E | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
75
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
76
|
+
*/
|
|
77
|
+
popFirst(): E | undefined;
|
|
61
78
|
/**
|
|
62
79
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
63
80
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
64
81
|
* linked list.
|
|
65
82
|
*/
|
|
66
83
|
unshift(val: E): void;
|
|
84
|
+
/**
|
|
85
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
86
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
87
|
+
* linked list.
|
|
88
|
+
*/
|
|
89
|
+
addFirst(val: E): void;
|
|
67
90
|
/**
|
|
68
91
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
69
92
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -148,7 +171,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
148
171
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
149
172
|
* the specified value is found, the function returns `null`.
|
|
150
173
|
*/
|
|
151
|
-
|
|
174
|
+
getNode(value: E): SinglyLinkedListNode<E> | null;
|
|
152
175
|
/**
|
|
153
176
|
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
154
177
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
@@ -158,13 +181,58 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
158
181
|
* inserted before the existing value, and `false` otherwise.
|
|
159
182
|
*/
|
|
160
183
|
insertBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
161
|
-
|
|
162
|
-
|
|
184
|
+
/**
|
|
185
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
186
|
+
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
187
|
+
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
188
|
+
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
189
|
+
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
190
|
+
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
191
|
+
*/
|
|
192
|
+
insertAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
163
193
|
/**
|
|
164
194
|
* The function counts the number of occurrences of a given value in a linked list.
|
|
165
195
|
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
166
196
|
* @returns The count of occurrences of the given value in the linked list.
|
|
167
197
|
*/
|
|
168
198
|
countOccurrences(value: E): number;
|
|
199
|
+
/**
|
|
200
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
201
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
202
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
203
|
+
* current node in the linked list.
|
|
204
|
+
*/
|
|
205
|
+
forEach(callback: (val: E, index: number) => void): void;
|
|
206
|
+
/**
|
|
207
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
208
|
+
* SinglyLinkedList with the transformed values.
|
|
209
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
210
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
211
|
+
* SinglyLinkedList).
|
|
212
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
213
|
+
*/
|
|
214
|
+
map<U>(callback: (val: E) => U): SinglyLinkedList<U>;
|
|
215
|
+
/**
|
|
216
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
217
|
+
* elements that satisfy the given callback function.
|
|
218
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
219
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
220
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
221
|
+
*/
|
|
222
|
+
filter(callback: (val: E) => boolean): SinglyLinkedList<E>;
|
|
223
|
+
/**
|
|
224
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
225
|
+
* single value.
|
|
226
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
227
|
+
* used to perform a specific operation on each element of the linked list.
|
|
228
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
229
|
+
* point for the reduction operation.
|
|
230
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
231
|
+
* elements in the linked list.
|
|
232
|
+
*/
|
|
233
|
+
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
|
|
234
|
+
/**
|
|
235
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
236
|
+
*/
|
|
169
237
|
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
170
238
|
}
|