min-heap-typed 1.50.1 → 1.50.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -9,19 +9,75 @@ import type { ElementCallback } from '../../types';
|
|
|
9
9
|
import { IterableElementBase } from '../base';
|
|
10
10
|
|
|
11
11
|
export class DoublyLinkedListNode<E = any> {
|
|
12
|
-
value: E;
|
|
13
|
-
next: DoublyLinkedListNode<E> | undefined;
|
|
14
|
-
prev: DoublyLinkedListNode<E> | undefined;
|
|
15
|
-
|
|
16
12
|
/**
|
|
17
13
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
18
14
|
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
19
15
|
* is defined as a generic type "E".
|
|
20
16
|
*/
|
|
21
17
|
constructor(value: E) {
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
18
|
+
this._value = value;
|
|
19
|
+
this._next = undefined;
|
|
20
|
+
this._prev = undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected _value: E;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The function returns the value of a protected variable.
|
|
27
|
+
* @returns The value of the variable `_value` is being returned.
|
|
28
|
+
*/
|
|
29
|
+
get value(): E {
|
|
30
|
+
return this._value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The above function sets the value of a variable.
|
|
35
|
+
* @param {E} value - The parameter "value" is of type E, which means it can be any type.
|
|
36
|
+
*/
|
|
37
|
+
set value(value: E) {
|
|
38
|
+
this._value = value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected _next: DoublyLinkedListNode<E> | undefined;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The "next" function returns the next node in a doubly linked list.
|
|
45
|
+
* @returns The `next` property is being returned. It can be either a `DoublyLinkedListNode<E>`
|
|
46
|
+
* object or `undefined`.
|
|
47
|
+
*/
|
|
48
|
+
get next(): DoublyLinkedListNode<E> | undefined {
|
|
49
|
+
return this._next;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The "next" property of a DoublyLinkedListNode is set to the provided value.
|
|
54
|
+
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
55
|
+
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
56
|
+
* `DoublyLinkedListNode` object or `undefined` as its value.
|
|
57
|
+
*/
|
|
58
|
+
set next(value: DoublyLinkedListNode<E> | undefined) {
|
|
59
|
+
this._next = value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected _prev: DoublyLinkedListNode<E> | undefined;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The `prev` function returns the previous node in a doubly linked list.
|
|
66
|
+
* @returns The `prev` property of the `DoublyLinkedListNode` class is being returned. It can either
|
|
67
|
+
* be a `DoublyLinkedListNode` object or `undefined`.
|
|
68
|
+
*/
|
|
69
|
+
get prev(): DoublyLinkedListNode<E> | undefined {
|
|
70
|
+
return this._prev;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The function sets the previous node of a doubly linked list node.
|
|
75
|
+
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
76
|
+
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
77
|
+
* `DoublyLinkedListNode` object or `undefined` as its value.
|
|
78
|
+
*/
|
|
79
|
+
set prev(value: DoublyLinkedListNode<E> | undefined) {
|
|
80
|
+
this._prev = value;
|
|
25
81
|
}
|
|
26
82
|
}
|
|
27
83
|
|
|
@@ -33,7 +89,10 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
33
89
|
*/
|
|
34
90
|
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
35
91
|
/**
|
|
36
|
-
* The constructor initializes
|
|
92
|
+
* The constructor initializes a linked list with optional elements.
|
|
93
|
+
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
|
94
|
+
* initial elements to be added to the data structure. It defaults to an empty array if no elements
|
|
95
|
+
* are provided.
|
|
37
96
|
*/
|
|
38
97
|
constructor(elements: Iterable<E> = []) {
|
|
39
98
|
super();
|
|
@@ -49,29 +108,43 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
49
108
|
|
|
50
109
|
protected _head: DoublyLinkedListNode<E> | undefined;
|
|
51
110
|
|
|
111
|
+
/**
|
|
112
|
+
* The `head` function returns the first node of a doubly linked list.
|
|
113
|
+
* @returns The method `getHead()` returns either a `DoublyLinkedListNode<E>` object or `undefined`.
|
|
114
|
+
*/
|
|
52
115
|
get head(): DoublyLinkedListNode<E> | undefined {
|
|
53
116
|
return this._head;
|
|
54
117
|
}
|
|
55
118
|
|
|
56
119
|
protected _tail: DoublyLinkedListNode<E> | undefined;
|
|
57
120
|
|
|
121
|
+
/**
|
|
122
|
+
* The `tail` function returns the last node of a doubly linked list.
|
|
123
|
+
* @returns The `get tail()` method is returning either a `DoublyLinkedListNode<E>` object or
|
|
124
|
+
* `undefined`.
|
|
125
|
+
*/
|
|
58
126
|
get tail(): DoublyLinkedListNode<E> | undefined {
|
|
59
127
|
return this._tail;
|
|
60
128
|
}
|
|
61
129
|
|
|
62
130
|
protected _size: number;
|
|
63
131
|
|
|
132
|
+
/**
|
|
133
|
+
* The function returns the size of an object.
|
|
134
|
+
* @returns The size of the object, which is a number.
|
|
135
|
+
*/
|
|
64
136
|
get size(): number {
|
|
65
137
|
return this._size;
|
|
66
138
|
}
|
|
67
139
|
|
|
68
140
|
/**
|
|
69
|
-
* Time Complexity: O(
|
|
70
|
-
* Space Complexity: O(
|
|
141
|
+
* Time Complexity: O(1)
|
|
142
|
+
* Space Complexity: O(1)
|
|
143
|
+
* where n is the number of elements in the linked list.
|
|
71
144
|
*/
|
|
72
145
|
|
|
73
146
|
/**
|
|
74
|
-
* Time Complexity: O(
|
|
147
|
+
* Time Complexity: O(1)
|
|
75
148
|
* Space Complexity: O(1)
|
|
76
149
|
*
|
|
77
150
|
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
@@ -87,7 +160,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
87
160
|
*/
|
|
88
161
|
|
|
89
162
|
/**
|
|
90
|
-
* Time Complexity: O(
|
|
163
|
+
* Time Complexity: O(1)
|
|
91
164
|
* Space Complexity: O(1)
|
|
92
165
|
*
|
|
93
166
|
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
@@ -98,12 +171,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
98
171
|
}
|
|
99
172
|
|
|
100
173
|
/**
|
|
101
|
-
* Time Complexity: O(
|
|
102
|
-
* Space Complexity: O(
|
|
174
|
+
* Time Complexity: O(n)
|
|
175
|
+
* Space Complexity: O(n)
|
|
103
176
|
*/
|
|
104
177
|
|
|
105
178
|
/**
|
|
106
|
-
* Time Complexity: O(n)
|
|
179
|
+
* Time Complexity: O(n)
|
|
107
180
|
* Space Complexity: O(n)
|
|
108
181
|
*
|
|
109
182
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
@@ -112,11 +185,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
112
185
|
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
113
186
|
*/
|
|
114
187
|
static fromArray<E>(data: E[]) {
|
|
115
|
-
|
|
116
|
-
for (const item of data) {
|
|
117
|
-
doublyLinkedList.push(item);
|
|
118
|
-
}
|
|
119
|
-
return doublyLinkedList;
|
|
188
|
+
return new DoublyLinkedList<E>(data);
|
|
120
189
|
}
|
|
121
190
|
|
|
122
191
|
/**
|
|
@@ -173,7 +242,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
173
242
|
}
|
|
174
243
|
|
|
175
244
|
/**
|
|
176
|
-
* Time Complexity: O(
|
|
245
|
+
* Time Complexity: O(1)
|
|
177
246
|
* Space Complexity: O(1)
|
|
178
247
|
*/
|
|
179
248
|
|
|
@@ -200,7 +269,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
200
269
|
}
|
|
201
270
|
|
|
202
271
|
/**
|
|
203
|
-
* Time Complexity: O(
|
|
272
|
+
* Time Complexity: O(1)
|
|
204
273
|
* Space Complexity: O(1)
|
|
205
274
|
*/
|
|
206
275
|
|
|
@@ -227,21 +296,21 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
227
296
|
}
|
|
228
297
|
|
|
229
298
|
/**
|
|
230
|
-
* Time Complexity: O(n)
|
|
299
|
+
* Time Complexity: O(n)
|
|
231
300
|
* Space Complexity: O(1)
|
|
232
301
|
*/
|
|
233
302
|
|
|
234
303
|
/**
|
|
235
|
-
* Time Complexity: O(n)
|
|
304
|
+
* Time Complexity: O(n)
|
|
236
305
|
* Space Complexity: O(1)
|
|
237
306
|
*
|
|
238
|
-
* The `
|
|
307
|
+
* The `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
|
|
239
308
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
240
309
|
* retrieve from the list.
|
|
241
310
|
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
242
311
|
* or the linked list is empty, it will return undefined.
|
|
243
312
|
*/
|
|
244
|
-
|
|
313
|
+
at(index: number): E | undefined {
|
|
245
314
|
if (index < 0 || index >= this.size) return undefined;
|
|
246
315
|
let current = this.head;
|
|
247
316
|
for (let i = 0; i < index; i++) {
|
|
@@ -251,12 +320,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
251
320
|
}
|
|
252
321
|
|
|
253
322
|
/**
|
|
254
|
-
* Time Complexity: O(n)
|
|
323
|
+
* Time Complexity: O(n)
|
|
255
324
|
* Space Complexity: O(1)
|
|
256
325
|
*/
|
|
257
326
|
|
|
258
327
|
/**
|
|
259
|
-
* Time Complexity: O(n)
|
|
328
|
+
* Time Complexity: O(n)
|
|
260
329
|
* Space Complexity: O(1)
|
|
261
330
|
*
|
|
262
331
|
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
|
|
@@ -276,12 +345,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
276
345
|
}
|
|
277
346
|
|
|
278
347
|
/**
|
|
279
|
-
* Time Complexity: O(n)
|
|
348
|
+
* Time Complexity: O(n)
|
|
280
349
|
* Space Complexity: O(1)
|
|
281
350
|
*/
|
|
282
351
|
|
|
283
352
|
/**
|
|
284
|
-
* Time Complexity: O(n)
|
|
353
|
+
* Time Complexity: O(n)
|
|
285
354
|
* Space Complexity: O(1)
|
|
286
355
|
*
|
|
287
356
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
@@ -304,12 +373,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
304
373
|
}
|
|
305
374
|
|
|
306
375
|
/**
|
|
307
|
-
* Time Complexity: O(n)
|
|
376
|
+
* Time Complexity: O(n)
|
|
308
377
|
* Space Complexity: O(1)
|
|
309
378
|
*/
|
|
310
379
|
|
|
311
380
|
/**
|
|
312
|
-
* Time Complexity: O(n)
|
|
381
|
+
* Time Complexity: O(n)
|
|
313
382
|
* Space Complexity: O(1)
|
|
314
383
|
*
|
|
315
384
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
@@ -343,12 +412,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
343
412
|
}
|
|
344
413
|
|
|
345
414
|
/**
|
|
346
|
-
* Time Complexity: O(
|
|
415
|
+
* Time Complexity: O(1) or O(n)
|
|
347
416
|
* Space Complexity: O(1)
|
|
417
|
+
* where n is the number of elements in the linked list.
|
|
348
418
|
*/
|
|
349
419
|
|
|
350
420
|
/**
|
|
351
|
-
* Time Complexity: O(
|
|
421
|
+
* Time Complexity: O(1) or O(n)
|
|
352
422
|
* Space Complexity: O(1)
|
|
353
423
|
*
|
|
354
424
|
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
@@ -388,12 +458,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
388
458
|
}
|
|
389
459
|
|
|
390
460
|
/**
|
|
391
|
-
* Time Complexity: O(
|
|
461
|
+
* Time Complexity: O(1) or O(n)
|
|
392
462
|
* Space Complexity: O(1)
|
|
393
463
|
*/
|
|
394
464
|
|
|
395
465
|
/**
|
|
396
|
-
* Time Complexity: O(
|
|
466
|
+
* Time Complexity: O(1) or O(n)
|
|
397
467
|
* Space Complexity: O(1)
|
|
398
468
|
*
|
|
399
469
|
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
@@ -432,7 +502,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
432
502
|
}
|
|
433
503
|
|
|
434
504
|
/**
|
|
435
|
-
* Time Complexity: O(n)
|
|
505
|
+
* Time Complexity: O(n)
|
|
436
506
|
* Space Complexity: O(1)
|
|
437
507
|
*
|
|
438
508
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
@@ -462,7 +532,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
462
532
|
}
|
|
463
533
|
|
|
464
534
|
/**
|
|
465
|
-
* Time Complexity: O(
|
|
535
|
+
* Time Complexity: O(1) or O(n)
|
|
536
|
+
* Space Complexity: O(1)
|
|
537
|
+
*/
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Time Complexity: O(1) or O(n)
|
|
466
541
|
* Space Complexity: O(1)
|
|
467
542
|
*
|
|
468
543
|
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
@@ -498,11 +573,14 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
498
573
|
}
|
|
499
574
|
|
|
500
575
|
/**
|
|
501
|
-
* Time Complexity: O(
|
|
576
|
+
* Time Complexity: O(1)
|
|
502
577
|
* Space Complexity: O(1)
|
|
503
578
|
*/
|
|
504
579
|
|
|
505
580
|
/**
|
|
581
|
+
* Time Complexity: O(1)
|
|
582
|
+
* Space Complexity: O(1)
|
|
583
|
+
*
|
|
506
584
|
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
507
585
|
* @returns A boolean value is being returned.
|
|
508
586
|
*/
|
|
@@ -511,11 +589,14 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
511
589
|
}
|
|
512
590
|
|
|
513
591
|
/**
|
|
514
|
-
* Time Complexity: O(
|
|
592
|
+
* Time Complexity: O(1)
|
|
515
593
|
* Space Complexity: O(1)
|
|
516
594
|
*/
|
|
517
595
|
|
|
518
596
|
/**
|
|
597
|
+
* Time Complexity: O(1)
|
|
598
|
+
* Space Complexity: O(1)
|
|
599
|
+
*
|
|
519
600
|
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
520
601
|
*/
|
|
521
602
|
clear(): void {
|
|
@@ -525,38 +606,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
525
606
|
}
|
|
526
607
|
|
|
527
608
|
/**
|
|
528
|
-
* Time Complexity: O(n)
|
|
529
|
-
* Space Complexity: O(1)
|
|
530
|
-
*/
|
|
531
|
-
|
|
532
|
-
/**
|
|
533
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
534
|
-
* Space Complexity: O(1)
|
|
535
|
-
*
|
|
536
|
-
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
537
|
-
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
538
|
-
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
539
|
-
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
540
|
-
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
541
|
-
*/
|
|
542
|
-
find(callback: (value: E) => boolean): E | undefined {
|
|
543
|
-
let current = this.head;
|
|
544
|
-
while (current) {
|
|
545
|
-
if (callback(current.value)) {
|
|
546
|
-
return current.value;
|
|
547
|
-
}
|
|
548
|
-
current = current.next;
|
|
549
|
-
}
|
|
550
|
-
return undefined;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
609
|
+
* Time Complexity: O(n)
|
|
555
610
|
* Space Complexity: O(1)
|
|
556
611
|
*/
|
|
557
612
|
|
|
558
613
|
/**
|
|
559
|
-
* Time Complexity: O(n)
|
|
614
|
+
* Time Complexity: O(n)
|
|
560
615
|
* Space Complexity: O(1)
|
|
561
616
|
*
|
|
562
617
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
@@ -579,12 +634,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
579
634
|
}
|
|
580
635
|
|
|
581
636
|
/**
|
|
582
|
-
* Time Complexity: O(n)
|
|
583
|
-
* Space Complexity: O(
|
|
637
|
+
* Time Complexity: O(n)
|
|
638
|
+
* Space Complexity: O(1)
|
|
584
639
|
*/
|
|
585
640
|
|
|
586
641
|
/**
|
|
587
|
-
* Time Complexity: O(n)
|
|
642
|
+
* Time Complexity: O(n)
|
|
588
643
|
* Space Complexity: O(1)
|
|
589
644
|
*
|
|
590
645
|
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
@@ -606,12 +661,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
606
661
|
}
|
|
607
662
|
|
|
608
663
|
/**
|
|
609
|
-
* Time Complexity: O(n)
|
|
610
|
-
* Space Complexity: O(
|
|
664
|
+
* Time Complexity: O(n)
|
|
665
|
+
* Space Complexity: O(1)
|
|
611
666
|
*/
|
|
612
667
|
|
|
613
668
|
/**
|
|
614
|
-
* Time Complexity: O(n)
|
|
669
|
+
* Time Complexity: O(n)
|
|
615
670
|
* Space Complexity: O(1)
|
|
616
671
|
*
|
|
617
672
|
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
@@ -633,7 +688,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
633
688
|
*/
|
|
634
689
|
|
|
635
690
|
/**
|
|
636
|
-
* Time Complexity: O(n)
|
|
691
|
+
* Time Complexity: O(n)
|
|
637
692
|
* Space Complexity: O(n)
|
|
638
693
|
*
|
|
639
694
|
* The `toArray` function converts a linked list into an array.
|
|
@@ -650,12 +705,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
650
705
|
}
|
|
651
706
|
|
|
652
707
|
/**
|
|
653
|
-
* Time Complexity: O(n)
|
|
708
|
+
* Time Complexity: O(n)
|
|
654
709
|
* Space Complexity: O(n)
|
|
655
710
|
*/
|
|
656
711
|
|
|
657
712
|
/**
|
|
658
|
-
* Time Complexity: O(n)
|
|
713
|
+
* Time Complexity: O(n)
|
|
659
714
|
* Space Complexity: O(n)
|
|
660
715
|
*
|
|
661
716
|
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
@@ -672,8 +727,26 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
672
727
|
}
|
|
673
728
|
|
|
674
729
|
/**
|
|
675
|
-
* Time Complexity: O(
|
|
676
|
-
* Space Complexity: O(
|
|
730
|
+
* Time Complexity: O(n)
|
|
731
|
+
* Space Complexity: O(n)
|
|
732
|
+
*/
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Time Complexity: O(n)
|
|
736
|
+
* Space Complexity: O(n)
|
|
737
|
+
*
|
|
738
|
+
* The `clone` function creates a new instance of the `DoublyLinkedList` class with the same values
|
|
739
|
+
* as the original list.
|
|
740
|
+
* @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
|
|
741
|
+
* is a copy of the original list.
|
|
742
|
+
*/
|
|
743
|
+
clone(): DoublyLinkedList<E> {
|
|
744
|
+
return new DoublyLinkedList(this.values());
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Time Complexity: O(n)
|
|
749
|
+
* Space Complexity: O(n)
|
|
677
750
|
*/
|
|
678
751
|
|
|
679
752
|
/**
|
|
@@ -706,8 +779,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
706
779
|
}
|
|
707
780
|
|
|
708
781
|
/**
|
|
709
|
-
* Time Complexity: O(
|
|
710
|
-
* Space Complexity: O(
|
|
782
|
+
* Time Complexity: O(n)
|
|
783
|
+
* Space Complexity: O(n)
|
|
711
784
|
*/
|
|
712
785
|
|
|
713
786
|
/**
|
|
@@ -772,7 +845,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
772
845
|
}
|
|
773
846
|
|
|
774
847
|
/**
|
|
775
|
-
* Time Complexity: O(
|
|
848
|
+
* Time Complexity: O(1)
|
|
776
849
|
* Space Complexity: O(1)
|
|
777
850
|
*/
|
|
778
851
|
|
|
@@ -789,7 +862,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
789
862
|
}
|
|
790
863
|
|
|
791
864
|
/**
|
|
792
|
-
* Time Complexity: O(
|
|
865
|
+
* Time Complexity: O(1)
|
|
793
866
|
* Space Complexity: O(1)
|
|
794
867
|
*/
|
|
795
868
|
|