data-structure-typed 1.39.0 → 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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- 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 +75 -7
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/cjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/cjs/data-structures/queue/deque.js +22 -22
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/cjs/types/helpers.d.ts +1 -4
- package/dist/cjs/types/helpers.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/map-graph.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/mjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/mjs/data-structures/queue/deque.js +22 -22
- package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/mjs/types/helpers.d.ts +1 -4
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +76 -90
- package/src/data-structures/binary-tree/bst.ts +9 -16
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +0 -2
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/helpers.ts +1 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +141 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +289 -47
- package/test/unit/data-structures/binary-tree/bst.test.ts +391 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +73 -7
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +241 -186
- package/test/unit/data-structures/graph/directed-graph.test.ts +34 -4
- package/test/unit/data-structures/graph/map-graph.test.ts +82 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +82 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +69 -6
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +59 -12
- package/test/unit/data-structures/matrix/matrix2d.test.ts +207 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +166 -1
- package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
- package/test/unit/data-structures/queue/deque.test.ts +283 -20
- package/test/unit/data-structures/queue/queue.test.ts +10 -8
|
@@ -209,7 +209,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
209
209
|
* callback.
|
|
210
210
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
211
211
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
212
|
-
* value (`BinaryTreeNodeKey`) or a custom callback function (`
|
|
212
|
+
* value (`BinaryTreeNodeKey`) or a custom callback function (`OneParamCallback<N>`) that determines
|
|
213
213
|
* whether a node matches the desired property.
|
|
214
214
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
215
215
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -340,7 +340,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
340
340
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
341
341
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
342
342
|
* done recursively or iteratively. It can have two possible values:
|
|
343
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `
|
|
343
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
344
344
|
*/
|
|
345
345
|
lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
346
346
|
if (typeof targetNode === 'number')
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult, IterationType,
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType, OneParamCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
@@ -105,7 +105,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
105
105
|
* decremented by 1 and
|
|
106
106
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
107
107
|
*/
|
|
108
|
-
delete<C extends
|
|
108
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
109
109
|
/**
|
|
110
110
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
111
111
|
*/
|
|
@@ -103,7 +103,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
|
|
|
103
103
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
104
104
|
* were removed.
|
|
105
105
|
*/
|
|
106
|
-
|
|
106
|
+
removeManyVertices(vertices: V[] | VertexKey[]): boolean;
|
|
107
107
|
/**
|
|
108
108
|
* The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
|
|
109
109
|
* @param {VertexKey | V} v1 - The parameter v1 can be either a VertexKey or a V. A VertexKey represents the unique
|
|
@@ -136,7 +136,7 @@ class AbstractGraph {
|
|
|
136
136
|
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
|
|
137
137
|
* were removed.
|
|
138
138
|
*/
|
|
139
|
-
|
|
139
|
+
removeManyVertices(vertices) {
|
|
140
140
|
const removed = [];
|
|
141
141
|
for (const v of vertices) {
|
|
142
142
|
removed.push(this.deleteVertex(v));
|
|
@@ -62,7 +62,7 @@ export declare class MapGraph<V extends MapVertex<V['val']> = MapVertex, E exten
|
|
|
62
62
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
63
63
|
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
|
|
64
64
|
*/
|
|
65
|
-
createVertex(key: VertexKey,
|
|
65
|
+
createVertex(key: VertexKey, lat?: number, long?: number, val?: V['val']): V;
|
|
66
66
|
/**
|
|
67
67
|
* The function creates a new instance of a MapEdge with the given source, destination, weight, and value.
|
|
68
68
|
* @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
@@ -92,7 +92,7 @@ class MapGraph extends directed_graph_1.DirectedGraph {
|
|
|
92
92
|
* @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
|
|
93
93
|
* @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
|
|
94
94
|
*/
|
|
95
|
-
createVertex(key,
|
|
95
|
+
createVertex(key, lat = this.origin[0], long = this.origin[1], val) {
|
|
96
96
|
return new MapVertex(key, lat, long, val);
|
|
97
97
|
}
|
|
98
98
|
/**
|
|
@@ -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
|
}
|
|
@@ -130,11 +130,11 @@ class DoublyLinkedList {
|
|
|
130
130
|
return removedNode.val;
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
133
|
-
* The `
|
|
133
|
+
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
134
134
|
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
135
135
|
* list is empty, it returns null.
|
|
136
136
|
*/
|
|
137
|
-
|
|
137
|
+
popLast() {
|
|
138
138
|
return this.pop();
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
@@ -158,11 +158,11 @@ class DoublyLinkedList {
|
|
|
158
158
|
return removedNode.val;
|
|
159
159
|
}
|
|
160
160
|
/**
|
|
161
|
-
* The `
|
|
161
|
+
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
162
162
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
163
163
|
* list.
|
|
164
164
|
*/
|
|
165
|
-
|
|
165
|
+
popFirst() {
|
|
166
166
|
return this.shift();
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
@@ -192,17 +192,17 @@ class DoublyLinkedList {
|
|
|
192
192
|
this.unshift(val);
|
|
193
193
|
}
|
|
194
194
|
/**
|
|
195
|
-
* The `
|
|
196
|
-
* @returns The method `
|
|
195
|
+
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
196
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
197
197
|
*/
|
|
198
|
-
|
|
198
|
+
getFirst() {
|
|
199
199
|
return this.head?.val;
|
|
200
200
|
}
|
|
201
201
|
/**
|
|
202
|
-
* The `
|
|
203
|
-
* @returns The method `
|
|
202
|
+
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
203
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
204
204
|
*/
|
|
205
|
-
|
|
205
|
+
getLast() {
|
|
206
206
|
return this.tail?.val;
|
|
207
207
|
}
|
|
208
208
|
/**
|
|
@@ -245,7 +245,7 @@ class DoublyLinkedList {
|
|
|
245
245
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
246
246
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
247
247
|
*/
|
|
248
|
-
|
|
248
|
+
getNode(val) {
|
|
249
249
|
let current = this.head;
|
|
250
250
|
while (current) {
|
|
251
251
|
if (current.val === val) {
|
|
@@ -285,6 +285,40 @@ class DoublyLinkedList {
|
|
|
285
285
|
this._length++;
|
|
286
286
|
return true;
|
|
287
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
290
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
291
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
292
|
+
* itself.
|
|
293
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
294
|
+
* list.
|
|
295
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
296
|
+
* insertion fails.
|
|
297
|
+
*/
|
|
298
|
+
insertBefore(existingValueOrNode, newValue) {
|
|
299
|
+
let existingNode;
|
|
300
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
301
|
+
existingNode = existingValueOrNode;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
305
|
+
}
|
|
306
|
+
if (existingNode) {
|
|
307
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
308
|
+
newNode.prev = existingNode.prev;
|
|
309
|
+
if (existingNode.prev) {
|
|
310
|
+
existingNode.prev.next = newNode;
|
|
311
|
+
}
|
|
312
|
+
newNode.next = existingNode;
|
|
313
|
+
existingNode.prev = newNode;
|
|
314
|
+
if (existingNode === this.head) {
|
|
315
|
+
this.head = newNode;
|
|
316
|
+
}
|
|
317
|
+
this._length++;
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
288
322
|
/**
|
|
289
323
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
290
324
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -320,7 +354,7 @@ class DoublyLinkedList {
|
|
|
320
354
|
node = valOrNode;
|
|
321
355
|
}
|
|
322
356
|
else {
|
|
323
|
-
node = this.
|
|
357
|
+
node = this.getNode(valOrNode);
|
|
324
358
|
}
|
|
325
359
|
if (node) {
|
|
326
360
|
if (node === this.head) {
|
|
@@ -405,14 +439,14 @@ class DoublyLinkedList {
|
|
|
405
439
|
return -1;
|
|
406
440
|
}
|
|
407
441
|
/**
|
|
408
|
-
* The `
|
|
442
|
+
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
409
443
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
410
444
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
411
445
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
412
|
-
* @returns The method `
|
|
446
|
+
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
413
447
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
414
448
|
*/
|
|
415
|
-
|
|
449
|
+
findBackward(callback) {
|
|
416
450
|
let current = this.tail;
|
|
417
451
|
while (current) {
|
|
418
452
|
if (callback(current.val)) {
|
|
@@ -423,10 +457,10 @@ class DoublyLinkedList {
|
|
|
423
457
|
return null;
|
|
424
458
|
}
|
|
425
459
|
/**
|
|
426
|
-
* The `
|
|
427
|
-
* @returns The `
|
|
460
|
+
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
461
|
+
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
428
462
|
*/
|
|
429
|
-
|
|
463
|
+
toArrayBackward() {
|
|
430
464
|
const array = [];
|
|
431
465
|
let current = this.tail;
|
|
432
466
|
while (current) {
|
|
@@ -531,7 +565,7 @@ class DoublyLinkedList {
|
|
|
531
565
|
existingNode = existingValueOrNode;
|
|
532
566
|
}
|
|
533
567
|
else {
|
|
534
|
-
existingNode = this.
|
|
568
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
535
569
|
}
|
|
536
570
|
if (existingNode) {
|
|
537
571
|
const newNode = new DoublyLinkedListNode(newValue);
|
|
@@ -550,38 +584,14 @@ class DoublyLinkedList {
|
|
|
550
584
|
return false;
|
|
551
585
|
}
|
|
552
586
|
/**
|
|
553
|
-
* The
|
|
554
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
555
|
-
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
556
|
-
* itself.
|
|
557
|
-
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
558
|
-
* list.
|
|
559
|
-
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
560
|
-
* insertion fails.
|
|
587
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
561
588
|
*/
|
|
562
|
-
|
|
563
|
-
let
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
else {
|
|
568
|
-
existingNode = this.findNode(existingValueOrNode);
|
|
569
|
-
}
|
|
570
|
-
if (existingNode) {
|
|
571
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
572
|
-
newNode.prev = existingNode.prev;
|
|
573
|
-
if (existingNode.prev) {
|
|
574
|
-
existingNode.prev.next = newNode;
|
|
575
|
-
}
|
|
576
|
-
newNode.next = existingNode;
|
|
577
|
-
existingNode.prev = newNode;
|
|
578
|
-
if (existingNode === this.head) {
|
|
579
|
-
this.head = newNode;
|
|
580
|
-
}
|
|
581
|
-
this._length++;
|
|
582
|
-
return true;
|
|
589
|
+
*[Symbol.iterator]() {
|
|
590
|
+
let current = this.head;
|
|
591
|
+
while (current) {
|
|
592
|
+
yield current.val;
|
|
593
|
+
current = current.next;
|
|
583
594
|
}
|
|
584
|
-
return false;
|
|
585
595
|
}
|
|
586
596
|
}
|
|
587
597
|
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
|
}
|