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,45 +6,19 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class DoublyLinkedListNode<E = any> {
|
|
9
|
+
value: E;
|
|
10
|
+
next: DoublyLinkedListNode<E> | null;
|
|
11
|
+
prev: DoublyLinkedListNode<E> | null;
|
|
12
|
+
|
|
9
13
|
/**
|
|
10
14
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
11
|
-
* @param {E}
|
|
15
|
+
* @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
16
|
* is defined as a generic type "E".
|
|
13
17
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private _val: E;
|
|
21
|
-
|
|
22
|
-
get val(): E {
|
|
23
|
-
return this._val;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
set val(value: E) {
|
|
27
|
-
this._val = value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private _next: DoublyLinkedListNode<E> | null;
|
|
31
|
-
|
|
32
|
-
get next(): DoublyLinkedListNode<E> | null {
|
|
33
|
-
return this._next;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
set next(value: DoublyLinkedListNode<E> | null) {
|
|
37
|
-
this._next = value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private _prev: DoublyLinkedListNode<E> | null;
|
|
41
|
-
|
|
42
|
-
get prev(): DoublyLinkedListNode<E> | null {
|
|
43
|
-
return this._prev;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
set prev(value: DoublyLinkedListNode<E> | null) {
|
|
47
|
-
this._prev = value;
|
|
18
|
+
constructor(value: E) {
|
|
19
|
+
this.value = value;
|
|
20
|
+
this.next = null;
|
|
21
|
+
this.prev = null;
|
|
48
22
|
}
|
|
49
23
|
}
|
|
50
24
|
|
|
@@ -58,27 +32,19 @@ export class DoublyLinkedList<E = any> {
|
|
|
58
32
|
this._length = 0;
|
|
59
33
|
}
|
|
60
34
|
|
|
61
|
-
|
|
35
|
+
protected _head: DoublyLinkedListNode<E> | null;
|
|
62
36
|
|
|
63
37
|
get head(): DoublyLinkedListNode<E> | null {
|
|
64
38
|
return this._head;
|
|
65
39
|
}
|
|
66
40
|
|
|
67
|
-
|
|
68
|
-
this._head = value;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private _tail: DoublyLinkedListNode<E> | null;
|
|
41
|
+
protected _tail: DoublyLinkedListNode<E> | null;
|
|
72
42
|
|
|
73
43
|
get tail(): DoublyLinkedListNode<E> | null {
|
|
74
44
|
return this._tail;
|
|
75
45
|
}
|
|
76
46
|
|
|
77
|
-
|
|
78
|
-
this._tail = value;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private _length: number;
|
|
47
|
+
protected _length: number;
|
|
82
48
|
|
|
83
49
|
get length(): number {
|
|
84
50
|
return this._length;
|
|
@@ -104,51 +70,51 @@ export class DoublyLinkedList<E = any> {
|
|
|
104
70
|
|
|
105
71
|
/**
|
|
106
72
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
107
|
-
* @param {E}
|
|
73
|
+
* @param {E} value - The value to be added to the linked list.
|
|
108
74
|
*/
|
|
109
|
-
push(
|
|
110
|
-
const newNode = new DoublyLinkedListNode(
|
|
75
|
+
push(value: E): void {
|
|
76
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
111
77
|
if (!this.head) {
|
|
112
|
-
this.
|
|
113
|
-
this.
|
|
78
|
+
this._head = newNode;
|
|
79
|
+
this._tail = newNode;
|
|
114
80
|
} else {
|
|
115
81
|
newNode.prev = this.tail;
|
|
116
82
|
this.tail!.next = newNode;
|
|
117
|
-
this.
|
|
83
|
+
this._tail = newNode;
|
|
118
84
|
}
|
|
119
85
|
this._length++;
|
|
120
86
|
}
|
|
121
87
|
|
|
122
88
|
/**
|
|
123
89
|
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
124
|
-
* @param {E}
|
|
90
|
+
* @param {E} value - The value to be added to the linked list.
|
|
125
91
|
*/
|
|
126
|
-
addLast(
|
|
127
|
-
this.push(
|
|
92
|
+
addLast(value: E): void {
|
|
93
|
+
this.push(value);
|
|
128
94
|
}
|
|
129
95
|
|
|
130
96
|
/**
|
|
131
97
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
132
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
98
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
133
99
|
* list is empty, it returns null.
|
|
134
100
|
*/
|
|
135
101
|
pop(): E | undefined {
|
|
136
102
|
if (!this.tail) return undefined;
|
|
137
103
|
const removedNode = this.tail;
|
|
138
104
|
if (this.head === this.tail) {
|
|
139
|
-
this.
|
|
140
|
-
this.
|
|
105
|
+
this._head = null;
|
|
106
|
+
this._tail = null;
|
|
141
107
|
} else {
|
|
142
|
-
this.
|
|
108
|
+
this._tail = removedNode.prev;
|
|
143
109
|
this.tail!.next = null;
|
|
144
110
|
}
|
|
145
111
|
this._length--;
|
|
146
|
-
return removedNode.
|
|
112
|
+
return removedNode.value;
|
|
147
113
|
}
|
|
148
114
|
|
|
149
115
|
/**
|
|
150
116
|
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
151
|
-
* @returns The method is returning the value of the removed node (removedNode.
|
|
117
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
152
118
|
* list is empty, it returns null.
|
|
153
119
|
*/
|
|
154
120
|
popLast(): E | undefined {
|
|
@@ -164,14 +130,14 @@ export class DoublyLinkedList<E = any> {
|
|
|
164
130
|
if (!this.head) return undefined;
|
|
165
131
|
const removedNode = this.head;
|
|
166
132
|
if (this.head === this.tail) {
|
|
167
|
-
this.
|
|
168
|
-
this.
|
|
133
|
+
this._head = null;
|
|
134
|
+
this._tail = null;
|
|
169
135
|
} else {
|
|
170
|
-
this.
|
|
136
|
+
this._head = removedNode.next;
|
|
171
137
|
this.head!.prev = null;
|
|
172
138
|
}
|
|
173
139
|
this._length--;
|
|
174
|
-
return removedNode.
|
|
140
|
+
return removedNode.value;
|
|
175
141
|
}
|
|
176
142
|
|
|
177
143
|
/**
|
|
@@ -185,29 +151,29 @@ export class DoublyLinkedList<E = any> {
|
|
|
185
151
|
|
|
186
152
|
/**
|
|
187
153
|
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
188
|
-
* @param {E}
|
|
154
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
189
155
|
* doubly linked list.
|
|
190
156
|
*/
|
|
191
|
-
unshift(
|
|
192
|
-
const newNode = new DoublyLinkedListNode(
|
|
157
|
+
unshift(value: E): void {
|
|
158
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
193
159
|
if (!this.head) {
|
|
194
|
-
this.
|
|
195
|
-
this.
|
|
160
|
+
this._head = newNode;
|
|
161
|
+
this._tail = newNode;
|
|
196
162
|
} else {
|
|
197
163
|
newNode.next = this.head;
|
|
198
164
|
this.head!.prev = newNode;
|
|
199
|
-
this.
|
|
165
|
+
this._head = newNode;
|
|
200
166
|
}
|
|
201
167
|
this._length++;
|
|
202
168
|
}
|
|
203
169
|
|
|
204
170
|
/**
|
|
205
171
|
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
206
|
-
* @param {E}
|
|
172
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
207
173
|
* doubly linked list.
|
|
208
174
|
*/
|
|
209
|
-
addFirst(
|
|
210
|
-
this.unshift(
|
|
175
|
+
addFirst(value: E): void {
|
|
176
|
+
this.unshift(value);
|
|
211
177
|
}
|
|
212
178
|
|
|
213
179
|
/**
|
|
@@ -215,7 +181,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
215
181
|
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
216
182
|
*/
|
|
217
183
|
getFirst(): E | undefined {
|
|
218
|
-
return this.head?.
|
|
184
|
+
return this.head?.value;
|
|
219
185
|
}
|
|
220
186
|
|
|
221
187
|
/**
|
|
@@ -223,7 +189,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
223
189
|
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
224
190
|
*/
|
|
225
191
|
getLast(): E | undefined {
|
|
226
|
-
return this.tail?.
|
|
192
|
+
return this.tail?.value;
|
|
227
193
|
}
|
|
228
194
|
|
|
229
195
|
/**
|
|
@@ -239,7 +205,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
239
205
|
for (let i = 0; i < index; i++) {
|
|
240
206
|
current = current!.next;
|
|
241
207
|
}
|
|
242
|
-
return current!.
|
|
208
|
+
return current!.value;
|
|
243
209
|
}
|
|
244
210
|
|
|
245
211
|
/**
|
|
@@ -262,15 +228,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
262
228
|
/**
|
|
263
229
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
264
230
|
* node if found, otherwise it returns null.
|
|
265
|
-
* @param {E}
|
|
266
|
-
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `
|
|
231
|
+
* @param {E} value - The `value` parameter is the value that we want to search for in the doubly linked list.
|
|
232
|
+
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `value`
|
|
267
233
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
268
234
|
*/
|
|
269
|
-
getNode(
|
|
235
|
+
getNode(value: E | null): DoublyLinkedListNode<E> | null {
|
|
270
236
|
let current = this.head;
|
|
271
237
|
|
|
272
238
|
while (current) {
|
|
273
|
-
if (current.
|
|
239
|
+
if (current.value === value) {
|
|
274
240
|
return current;
|
|
275
241
|
}
|
|
276
242
|
current = current.next;
|
|
@@ -283,23 +249,23 @@ export class DoublyLinkedList<E = any> {
|
|
|
283
249
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
284
250
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
285
251
|
* DoublyLinkedList. It is of type number.
|
|
286
|
-
* @param {E}
|
|
252
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the Doubly Linked List at the
|
|
287
253
|
* specified index.
|
|
288
254
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
289
255
|
* if the index is out of bounds.
|
|
290
256
|
*/
|
|
291
|
-
insertAt(index: number,
|
|
257
|
+
insertAt(index: number, value: E): boolean {
|
|
292
258
|
if (index < 0 || index > this.length) return false;
|
|
293
259
|
if (index === 0) {
|
|
294
|
-
this.unshift(
|
|
260
|
+
this.unshift(value);
|
|
295
261
|
return true;
|
|
296
262
|
}
|
|
297
263
|
if (index === this.length) {
|
|
298
|
-
this.push(
|
|
264
|
+
this.push(value);
|
|
299
265
|
return true;
|
|
300
266
|
}
|
|
301
267
|
|
|
302
|
-
const newNode = new DoublyLinkedListNode(
|
|
268
|
+
const newNode = new DoublyLinkedListNode(value);
|
|
303
269
|
const prevNode = this.getNodeAt(index - 1);
|
|
304
270
|
const nextNode = prevNode!.next;
|
|
305
271
|
newNode.prev = prevNode;
|
|
@@ -338,7 +304,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
338
304
|
newNode.next = existingNode;
|
|
339
305
|
existingNode.prev = newNode;
|
|
340
306
|
if (existingNode === this.head) {
|
|
341
|
-
this.
|
|
307
|
+
this._head = newNode;
|
|
342
308
|
}
|
|
343
309
|
this._length++;
|
|
344
310
|
return true;
|
|
@@ -365,7 +331,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
365
331
|
prevNode!.next = nextNode;
|
|
366
332
|
nextNode!.prev = prevNode;
|
|
367
333
|
this._length--;
|
|
368
|
-
return removedNode!.
|
|
334
|
+
return removedNode!.value;
|
|
369
335
|
}
|
|
370
336
|
|
|
371
337
|
/**
|
|
@@ -409,7 +375,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
409
375
|
const array: E[] = [];
|
|
410
376
|
let current = this.head;
|
|
411
377
|
while (current) {
|
|
412
|
-
array.push(current.
|
|
378
|
+
array.push(current.value);
|
|
413
379
|
current = current.next;
|
|
414
380
|
}
|
|
415
381
|
return array;
|
|
@@ -439,11 +405,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
439
405
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
440
406
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
441
407
|
*/
|
|
442
|
-
find(callback: (
|
|
408
|
+
find(callback: (value: E) => boolean): E | null {
|
|
443
409
|
let current = this.head;
|
|
444
410
|
while (current) {
|
|
445
|
-
if (callback(current.
|
|
446
|
-
return current.
|
|
411
|
+
if (callback(current.value)) {
|
|
412
|
+
return current.value;
|
|
447
413
|
}
|
|
448
414
|
current = current.next;
|
|
449
415
|
}
|
|
@@ -452,16 +418,16 @@ export class DoublyLinkedList<E = any> {
|
|
|
452
418
|
|
|
453
419
|
/**
|
|
454
420
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
455
|
-
* @param {E}
|
|
421
|
+
* @param {E} value - The parameter `value` is of type `E`, which means it can be any data type. It represents the value
|
|
456
422
|
* that we are searching for in the linked list.
|
|
457
|
-
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `
|
|
423
|
+
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `value` in the linked
|
|
458
424
|
* list. If the value is not found, it returns -1.
|
|
459
425
|
*/
|
|
460
|
-
indexOf(
|
|
426
|
+
indexOf(value: E): number {
|
|
461
427
|
let index = 0;
|
|
462
428
|
let current = this.head;
|
|
463
429
|
while (current) {
|
|
464
|
-
if (current.
|
|
430
|
+
if (current.value === value) {
|
|
465
431
|
return index;
|
|
466
432
|
}
|
|
467
433
|
index++;
|
|
@@ -478,11 +444,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
478
444
|
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
479
445
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
480
446
|
*/
|
|
481
|
-
findBackward(callback: (
|
|
447
|
+
findBackward(callback: (value: E) => boolean): E | null {
|
|
482
448
|
let current = this.tail;
|
|
483
449
|
while (current) {
|
|
484
|
-
if (callback(current.
|
|
485
|
-
return current.
|
|
450
|
+
if (callback(current.value)) {
|
|
451
|
+
return current.value;
|
|
486
452
|
}
|
|
487
453
|
current = current.prev;
|
|
488
454
|
}
|
|
@@ -497,7 +463,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
497
463
|
const array: E[] = [];
|
|
498
464
|
let current = this.tail;
|
|
499
465
|
while (current) {
|
|
500
|
-
array.push(current.
|
|
466
|
+
array.push(current.value);
|
|
501
467
|
current = current.prev;
|
|
502
468
|
}
|
|
503
469
|
return array;
|
|
@@ -508,7 +474,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
508
474
|
*/
|
|
509
475
|
reverse(): void {
|
|
510
476
|
let current = this.head;
|
|
511
|
-
[this.
|
|
477
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
512
478
|
while (current) {
|
|
513
479
|
const next = current.next;
|
|
514
480
|
[current.prev, current.next] = [current.next, current.prev];
|
|
@@ -518,15 +484,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
518
484
|
|
|
519
485
|
/**
|
|
520
486
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
521
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
487
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
522
488
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
523
489
|
* current node in the linked list.
|
|
524
490
|
*/
|
|
525
|
-
forEach(callback: (
|
|
491
|
+
forEach(callback: (value: E, index: number) => void): void {
|
|
526
492
|
let current = this.head;
|
|
527
493
|
let index = 0;
|
|
528
494
|
while (current) {
|
|
529
|
-
callback(current.
|
|
495
|
+
callback(current.value, index);
|
|
530
496
|
current = current.next;
|
|
531
497
|
index++;
|
|
532
498
|
}
|
|
@@ -540,11 +506,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
540
506
|
* DoublyLinkedList).
|
|
541
507
|
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
542
508
|
*/
|
|
543
|
-
map<U>(callback: (
|
|
509
|
+
map<U>(callback: (value: E) => U): DoublyLinkedList<U> {
|
|
544
510
|
const mappedList = new DoublyLinkedList<U>();
|
|
545
511
|
let current = this.head;
|
|
546
512
|
while (current) {
|
|
547
|
-
mappedList.push(callback(current.
|
|
513
|
+
mappedList.push(callback(current.value));
|
|
548
514
|
current = current.next;
|
|
549
515
|
}
|
|
550
516
|
return mappedList;
|
|
@@ -557,12 +523,12 @@ export class DoublyLinkedList<E = any> {
|
|
|
557
523
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
558
524
|
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
559
525
|
*/
|
|
560
|
-
filter(callback: (
|
|
526
|
+
filter(callback: (value: E) => boolean): DoublyLinkedList<E> {
|
|
561
527
|
const filteredList = new DoublyLinkedList<E>();
|
|
562
528
|
let current = this.head;
|
|
563
529
|
while (current) {
|
|
564
|
-
if (callback(current.
|
|
565
|
-
filteredList.push(current.
|
|
530
|
+
if (callback(current.value)) {
|
|
531
|
+
filteredList.push(current.value);
|
|
566
532
|
}
|
|
567
533
|
current = current.next;
|
|
568
534
|
}
|
|
@@ -572,18 +538,18 @@ export class DoublyLinkedList<E = any> {
|
|
|
572
538
|
/**
|
|
573
539
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
574
540
|
* single value.
|
|
575
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
541
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
576
542
|
* used to perform a specific operation on each element of the linked list.
|
|
577
543
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
578
544
|
* point for the reduction operation.
|
|
579
545
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
580
546
|
* elements in the linked list.
|
|
581
547
|
*/
|
|
582
|
-
reduce<U>(callback: (accumulator: U,
|
|
548
|
+
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
|
|
583
549
|
let accumulator = initialValue;
|
|
584
550
|
let current = this.head;
|
|
585
551
|
while (current) {
|
|
586
|
-
accumulator = callback(accumulator, current.
|
|
552
|
+
accumulator = callback(accumulator, current.value);
|
|
587
553
|
current = current.next;
|
|
588
554
|
}
|
|
589
555
|
return accumulator;
|
|
@@ -616,7 +582,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
616
582
|
newNode.prev = existingNode;
|
|
617
583
|
existingNode.next = newNode;
|
|
618
584
|
if (existingNode === this.tail) {
|
|
619
|
-
this.
|
|
585
|
+
this._tail = newNode;
|
|
620
586
|
}
|
|
621
587
|
this._length++;
|
|
622
588
|
return true;
|
|
@@ -628,11 +594,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
628
594
|
/**
|
|
629
595
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
630
596
|
*/
|
|
631
|
-
*[Symbol.iterator]() {
|
|
597
|
+
* [Symbol.iterator]() {
|
|
632
598
|
let current = this.head;
|
|
633
599
|
|
|
634
600
|
while (current) {
|
|
635
|
-
yield current.
|
|
601
|
+
yield current.value;
|
|
636
602
|
current = current.next;
|
|
637
603
|
}
|
|
638
604
|
}
|