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,23 +9,62 @@ import type { ElementCallback } from '../../types';
|
|
|
9
9
|
import { IterableElementBase } from '../base';
|
|
10
10
|
|
|
11
11
|
export class SinglyLinkedListNode<E = any> {
|
|
12
|
-
value: E;
|
|
13
|
-
next: SinglyLinkedListNode<E> | undefined;
|
|
14
|
-
|
|
15
12
|
/**
|
|
16
13
|
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
|
|
17
14
|
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
18
15
|
* will be stored in the node of a linked list.
|
|
19
16
|
*/
|
|
20
17
|
constructor(value: E) {
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
18
|
+
this._value = value;
|
|
19
|
+
this._next = undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
protected _value: E;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The function returns the value of a protected variable.
|
|
26
|
+
* @returns The value of the variable `_value` is being returned.
|
|
27
|
+
*/
|
|
28
|
+
get value(): E {
|
|
29
|
+
return this._value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The above function sets the value of a variable.
|
|
34
|
+
* @param {E} value - The parameter "value" is of type E, which means it can be any type.
|
|
35
|
+
*/
|
|
36
|
+
set value(value: E) {
|
|
37
|
+
this._value = value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected _next: SinglyLinkedListNode<E> | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The `next` function returns the next node in a singly linked list.
|
|
44
|
+
* @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>`
|
|
45
|
+
* object or `undefined`.
|
|
46
|
+
*/
|
|
47
|
+
get next(): SinglyLinkedListNode<E> | undefined {
|
|
48
|
+
return this._next;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The "next" property of a SinglyLinkedListNode is set to the provided value.
|
|
53
|
+
* @param {SinglyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
54
|
+
* `SinglyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
55
|
+
* `SinglyLinkedListNode` object or `undefined` as its value.
|
|
56
|
+
*/
|
|
57
|
+
set next(value: SinglyLinkedListNode<E> | undefined) {
|
|
58
|
+
this._next = value;
|
|
23
59
|
}
|
|
24
60
|
}
|
|
25
61
|
|
|
26
62
|
export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
27
63
|
/**
|
|
28
|
-
* The constructor initializes
|
|
64
|
+
* The constructor initializes a new instance of a class with an optional iterable of elements.
|
|
65
|
+
* @param elements - The `elements` parameter is an optional iterable object that contains the
|
|
66
|
+
* initial elements to be added to the instance of the class. If no `elements` are provided, an empty
|
|
67
|
+
* array will be used as the default value.
|
|
29
68
|
*/
|
|
30
69
|
constructor(elements: Iterable<E> = []) {
|
|
31
70
|
super();
|
|
@@ -36,30 +75,44 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
36
75
|
|
|
37
76
|
protected _head: SinglyLinkedListNode<E> | undefined;
|
|
38
77
|
|
|
78
|
+
/**
|
|
79
|
+
* The `head` function returns the first node of a singly linked list.
|
|
80
|
+
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
81
|
+
*/
|
|
39
82
|
get head(): SinglyLinkedListNode<E> | undefined {
|
|
40
83
|
return this._head;
|
|
41
84
|
}
|
|
42
85
|
|
|
43
86
|
protected _tail: SinglyLinkedListNode<E> | undefined;
|
|
44
87
|
|
|
88
|
+
/**
|
|
89
|
+
* The `tail` function returns the last node of a singly linked list.
|
|
90
|
+
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
91
|
+
*/
|
|
45
92
|
get tail(): SinglyLinkedListNode<E> | undefined {
|
|
46
93
|
return this._tail;
|
|
47
94
|
}
|
|
48
95
|
|
|
49
96
|
protected _size: number = 0;
|
|
50
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The function returns the size of an object.
|
|
100
|
+
* @returns The size of the object, which is a number.
|
|
101
|
+
*/
|
|
51
102
|
get size(): number {
|
|
52
103
|
return this._size;
|
|
53
104
|
}
|
|
54
105
|
|
|
55
106
|
/**
|
|
56
|
-
* Time Complexity: O(n)
|
|
57
|
-
* Space Complexity: O(n)
|
|
107
|
+
* Time Complexity: O(n)
|
|
108
|
+
* Space Complexity: O(n)
|
|
109
|
+
* Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
|
|
110
|
+
* Linear space, as it creates a new node for each element in the array.
|
|
58
111
|
*/
|
|
59
112
|
|
|
60
113
|
/**
|
|
61
|
-
* Time Complexity: O(n)
|
|
62
|
-
* Space Complexity: O(n)
|
|
114
|
+
* Time Complexity: O(n)
|
|
115
|
+
* Space Complexity: O(n)
|
|
63
116
|
*
|
|
64
117
|
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
65
118
|
* array.
|
|
@@ -75,13 +128,15 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
75
128
|
}
|
|
76
129
|
|
|
77
130
|
/**
|
|
78
|
-
* Time Complexity: O(1)
|
|
79
|
-
* Space Complexity: O(1)
|
|
131
|
+
* Time Complexity: O(1)
|
|
132
|
+
* Space Complexity: O(1)
|
|
133
|
+
* Constant time, as it involves basic pointer adjustments.
|
|
134
|
+
* Constant space, as it only creates a new node.
|
|
80
135
|
*/
|
|
81
136
|
|
|
82
137
|
/**
|
|
83
|
-
* Time Complexity: O(1)
|
|
84
|
-
* Space Complexity: O(1)
|
|
138
|
+
* Time Complexity: O(1)
|
|
139
|
+
* Space Complexity: O(1)
|
|
85
140
|
*
|
|
86
141
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
87
142
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
@@ -101,13 +156,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
101
156
|
}
|
|
102
157
|
|
|
103
158
|
/**
|
|
104
|
-
* Time Complexity: O(1)
|
|
105
|
-
* Space Complexity: O(1)
|
|
159
|
+
* Time Complexity: O(1)
|
|
160
|
+
* Space Complexity: O(1)
|
|
106
161
|
*/
|
|
107
162
|
|
|
108
163
|
/**
|
|
109
|
-
* Time Complexity: O(1)
|
|
110
|
-
* Space Complexity: O(1)
|
|
164
|
+
* Time Complexity: O(1)
|
|
165
|
+
* Space Complexity: O(1)
|
|
111
166
|
*
|
|
112
167
|
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
113
168
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
@@ -118,13 +173,14 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
118
173
|
}
|
|
119
174
|
|
|
120
175
|
/**
|
|
121
|
-
* Time Complexity: O(n)
|
|
122
|
-
* Space Complexity: O(1)
|
|
176
|
+
* Time Complexity: O(n)
|
|
177
|
+
* Space Complexity: O(1)
|
|
178
|
+
* Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
123
179
|
*/
|
|
124
180
|
|
|
125
181
|
/**
|
|
126
|
-
* Time Complexity: O(n)
|
|
127
|
-
* Space Complexity: O(1)
|
|
182
|
+
* Time Complexity: O(n)
|
|
183
|
+
* Space Complexity: O(1)
|
|
128
184
|
*
|
|
129
185
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
130
186
|
* pointers accordingly.
|
|
@@ -153,13 +209,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
153
209
|
}
|
|
154
210
|
|
|
155
211
|
/**
|
|
156
|
-
* Time Complexity: O(n)
|
|
157
|
-
* Space Complexity: O(1)
|
|
212
|
+
* Time Complexity: O(n)
|
|
213
|
+
* Space Complexity: O(1)
|
|
158
214
|
*/
|
|
159
215
|
|
|
160
216
|
/**
|
|
161
|
-
* Time Complexity: O(n)
|
|
162
|
-
* Space Complexity: O(1)
|
|
217
|
+
* Time Complexity: O(n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
163
219
|
*
|
|
164
220
|
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
165
221
|
* pointers accordingly.
|
|
@@ -171,13 +227,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
171
227
|
}
|
|
172
228
|
|
|
173
229
|
/**
|
|
174
|
-
* Time Complexity: O(1)
|
|
175
|
-
* Space Complexity: O(1)
|
|
230
|
+
* Time Complexity: O(1)
|
|
231
|
+
* Space Complexity: O(1)
|
|
176
232
|
*/
|
|
177
233
|
|
|
178
234
|
/**
|
|
179
|
-
* Time Complexity: O(1)
|
|
180
|
-
* Space Complexity: O(1)
|
|
235
|
+
* Time Complexity: O(1)
|
|
236
|
+
* Space Complexity: O(1)
|
|
181
237
|
*
|
|
182
238
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
183
239
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -191,13 +247,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
191
247
|
}
|
|
192
248
|
|
|
193
249
|
/**
|
|
194
|
-
* Time Complexity: O(1)
|
|
195
|
-
* Space Complexity: O(1)
|
|
250
|
+
* Time Complexity: O(1)
|
|
251
|
+
* Space Complexity: O(1)
|
|
196
252
|
*/
|
|
197
253
|
|
|
198
254
|
/**
|
|
199
|
-
* Time Complexity: O(1)
|
|
200
|
-
* Space Complexity: O(1)
|
|
255
|
+
* Time Complexity: O(1)
|
|
256
|
+
* Space Complexity: O(1)
|
|
201
257
|
*
|
|
202
258
|
* The `pollFirst()` function removes and returns the value of the first node in a linked list.
|
|
203
259
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -207,13 +263,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
207
263
|
}
|
|
208
264
|
|
|
209
265
|
/**
|
|
210
|
-
* Time Complexity: O(1)
|
|
211
|
-
* Space Complexity: O(1)
|
|
266
|
+
* Time Complexity: O(1)
|
|
267
|
+
* Space Complexity: O(1)
|
|
212
268
|
*/
|
|
213
269
|
|
|
214
270
|
/**
|
|
215
|
-
* Time Complexity: O(1)
|
|
216
|
-
* Space Complexity: O(1)
|
|
271
|
+
* Time Complexity: O(1)
|
|
272
|
+
* Space Complexity: O(1)
|
|
217
273
|
*
|
|
218
274
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
219
275
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
@@ -233,13 +289,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
233
289
|
}
|
|
234
290
|
|
|
235
291
|
/**
|
|
236
|
-
* Time Complexity: O(1)
|
|
237
|
-
* Space Complexity: O(1)
|
|
292
|
+
* Time Complexity: O(1)
|
|
293
|
+
* Space Complexity: O(1)
|
|
238
294
|
*/
|
|
239
295
|
|
|
240
296
|
/**
|
|
241
|
-
* Time Complexity: O(1)
|
|
242
|
-
* Space Complexity: O(1)
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
243
299
|
*
|
|
244
300
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
245
301
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
@@ -250,21 +306,22 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
250
306
|
}
|
|
251
307
|
|
|
252
308
|
/**
|
|
253
|
-
* Time Complexity: O(n)
|
|
254
|
-
* Space Complexity: O(1)
|
|
309
|
+
* Time Complexity: O(n)
|
|
310
|
+
* Space Complexity: O(1)
|
|
311
|
+
* Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
255
312
|
*/
|
|
256
313
|
|
|
257
314
|
/**
|
|
258
|
-
* Time Complexity: O(n)
|
|
259
|
-
* Space Complexity: O(1)
|
|
315
|
+
* Time Complexity: O(n)
|
|
316
|
+
* Space Complexity: O(1)
|
|
260
317
|
*
|
|
261
|
-
* The function `
|
|
318
|
+
* The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range.
|
|
262
319
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
263
320
|
* retrieve from the list.
|
|
264
|
-
* @returns The method `
|
|
321
|
+
* @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or
|
|
265
322
|
* `undefined` if the index is out of bounds.
|
|
266
323
|
*/
|
|
267
|
-
|
|
324
|
+
at(index: number): E | undefined {
|
|
268
325
|
if (index < 0 || index >= this.size) return undefined;
|
|
269
326
|
let current = this.head;
|
|
270
327
|
for (let i = 0; i < index; i++) {
|
|
@@ -274,13 +331,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
274
331
|
}
|
|
275
332
|
|
|
276
333
|
/**
|
|
277
|
-
* Time Complexity: O(n)
|
|
278
|
-
* Space Complexity: O(1)
|
|
334
|
+
* Time Complexity: O(n)
|
|
335
|
+
* Space Complexity: O(1)
|
|
279
336
|
*/
|
|
280
337
|
|
|
281
338
|
/**
|
|
282
|
-
* Time Complexity: O(n)
|
|
283
|
-
* Space Complexity: O(1)
|
|
339
|
+
* Time Complexity: O(n)
|
|
340
|
+
* Space Complexity: O(1)
|
|
284
341
|
*
|
|
285
342
|
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
286
343
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
@@ -297,13 +354,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
297
354
|
}
|
|
298
355
|
|
|
299
356
|
/**
|
|
300
|
-
* Time Complexity: O(n)
|
|
301
|
-
* Space Complexity: O(1)
|
|
357
|
+
* Time Complexity: O(n)
|
|
358
|
+
* Space Complexity: O(1)
|
|
302
359
|
*/
|
|
303
360
|
|
|
304
361
|
/**
|
|
305
|
-
* Time Complexity: O(n)
|
|
306
|
-
* Space Complexity: O(1)
|
|
362
|
+
* Time Complexity: O(n)
|
|
363
|
+
* Space Complexity: O(1)
|
|
307
364
|
*
|
|
308
365
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
309
366
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -330,13 +387,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
330
387
|
}
|
|
331
388
|
|
|
332
389
|
/**
|
|
333
|
-
* Time Complexity: O(n)
|
|
334
|
-
* Space Complexity: O(1)
|
|
390
|
+
* Time Complexity: O(n)
|
|
391
|
+
* Space Complexity: O(1)
|
|
335
392
|
*/
|
|
336
393
|
|
|
337
394
|
/**
|
|
338
|
-
* Time Complexity: O(n)
|
|
339
|
-
* Space Complexity: O(1)
|
|
395
|
+
* Time Complexity: O(n)
|
|
396
|
+
* Space Complexity: O(1)
|
|
340
397
|
*
|
|
341
398
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
342
399
|
* @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
|
|
@@ -379,13 +436,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
379
436
|
}
|
|
380
437
|
|
|
381
438
|
/**
|
|
382
|
-
* Time Complexity: O(n)
|
|
383
|
-
* Space Complexity: O(1)
|
|
439
|
+
* Time Complexity: O(n)
|
|
440
|
+
* Space Complexity: O(1)
|
|
384
441
|
*/
|
|
385
442
|
|
|
386
443
|
/**
|
|
387
|
-
* Time Complexity: O(n)
|
|
388
|
-
* Space Complexity: O(1)
|
|
444
|
+
* Time Complexity: O(n)
|
|
445
|
+
* Space Complexity: O(1)
|
|
389
446
|
*
|
|
390
447
|
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
391
448
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -433,13 +490,15 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
433
490
|
}
|
|
434
491
|
|
|
435
492
|
/**
|
|
436
|
-
* Time Complexity: O(n)
|
|
437
|
-
* Space Complexity: O(n)
|
|
493
|
+
* Time Complexity: O(n)
|
|
494
|
+
* Space Complexity: O(n)
|
|
495
|
+
* Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
|
|
496
|
+
* Linear space, as it creates an array with the same length as the list.
|
|
438
497
|
*/
|
|
439
498
|
|
|
440
499
|
/**
|
|
441
|
-
* Time Complexity: O(n)
|
|
442
|
-
* Space Complexity: O(n)
|
|
500
|
+
* Time Complexity: O(n)
|
|
501
|
+
* Space Complexity: O(n)
|
|
443
502
|
*
|
|
444
503
|
* The `toArray` function converts a linked list into an array.
|
|
445
504
|
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
@@ -455,13 +514,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
455
514
|
}
|
|
456
515
|
|
|
457
516
|
/**
|
|
458
|
-
* Time Complexity: O(n)
|
|
459
|
-
* Space Complexity: O(1)
|
|
517
|
+
* Time Complexity: O(n)
|
|
518
|
+
* Space Complexity: O(1)
|
|
460
519
|
*/
|
|
461
520
|
|
|
462
521
|
/**
|
|
463
|
-
* Time Complexity: O(n)
|
|
464
|
-
* Space Complexity: O(1)
|
|
522
|
+
* Time Complexity: O(n)
|
|
523
|
+
* Space Complexity: O(1)
|
|
465
524
|
*
|
|
466
525
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
467
526
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
@@ -485,39 +544,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
485
544
|
}
|
|
486
545
|
|
|
487
546
|
/**
|
|
488
|
-
* Time Complexity: O(n)
|
|
489
|
-
* Space Complexity: O(1)
|
|
490
|
-
*/
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
494
|
-
* Space Complexity: O(1) - Constant space.
|
|
495
|
-
*
|
|
496
|
-
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
497
|
-
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
498
|
-
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
499
|
-
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
500
|
-
* the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
501
|
-
*/
|
|
502
|
-
find(callback: (value: E) => boolean): E | undefined {
|
|
503
|
-
let current = this.head;
|
|
504
|
-
while (current) {
|
|
505
|
-
if (callback(current.value)) {
|
|
506
|
-
return current.value;
|
|
507
|
-
}
|
|
508
|
-
current = current.next;
|
|
509
|
-
}
|
|
510
|
-
return undefined;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
/**
|
|
514
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
515
|
-
* Space Complexity: O(1) - Constant space.
|
|
547
|
+
* Time Complexity: O(n)
|
|
548
|
+
* Space Complexity: O(1)
|
|
516
549
|
*/
|
|
517
550
|
|
|
518
551
|
/**
|
|
519
|
-
* Time Complexity: O(n)
|
|
520
|
-
* Space Complexity: O(1)
|
|
552
|
+
* Time Complexity: O(n)
|
|
553
|
+
* Space Complexity: O(1)
|
|
521
554
|
*
|
|
522
555
|
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
523
556
|
* @param {E} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
@@ -540,13 +573,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
540
573
|
}
|
|
541
574
|
|
|
542
575
|
/**
|
|
543
|
-
* Time Complexity: O(n)
|
|
544
|
-
* Space Complexity: O(1)
|
|
576
|
+
* Time Complexity: O(n)
|
|
577
|
+
* Space Complexity: O(1)
|
|
545
578
|
*/
|
|
546
579
|
|
|
547
580
|
/**
|
|
548
|
-
* Time Complexity: O(n)
|
|
549
|
-
* Space Complexity: O(1)
|
|
581
|
+
* Time Complexity: O(n)
|
|
582
|
+
* Space Complexity: O(1)
|
|
550
583
|
*
|
|
551
584
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
552
585
|
* undefined.
|
|
@@ -568,13 +601,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
568
601
|
}
|
|
569
602
|
|
|
570
603
|
/**
|
|
571
|
-
* Time Complexity: O(n)
|
|
572
|
-
* Space Complexity: O(1)
|
|
604
|
+
* Time Complexity: O(n)
|
|
605
|
+
* Space Complexity: O(1)
|
|
573
606
|
*/
|
|
574
607
|
|
|
575
608
|
/**
|
|
576
|
-
* Time Complexity: O(n)
|
|
577
|
-
* Space Complexity: O(1)
|
|
609
|
+
* Time Complexity: O(n)
|
|
610
|
+
* Space Complexity: O(1)
|
|
578
611
|
*
|
|
579
612
|
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
580
613
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
@@ -613,13 +646,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
613
646
|
}
|
|
614
647
|
|
|
615
648
|
/**
|
|
616
|
-
* Time Complexity: O(n)
|
|
617
|
-
* Space Complexity: O(1)
|
|
649
|
+
* Time Complexity: O(n)
|
|
650
|
+
* Space Complexity: O(1)
|
|
618
651
|
*/
|
|
619
652
|
|
|
620
653
|
/**
|
|
621
|
-
* Time Complexity: O(n)
|
|
622
|
-
* Space Complexity: O(1)
|
|
654
|
+
* Time Complexity: O(n)
|
|
655
|
+
* Space Complexity: O(1)
|
|
623
656
|
*
|
|
624
657
|
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
625
658
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
@@ -652,13 +685,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
652
685
|
}
|
|
653
686
|
|
|
654
687
|
/**
|
|
655
|
-
* Time Complexity: O(n)
|
|
656
|
-
* Space Complexity: O(1)
|
|
688
|
+
* Time Complexity: O(n)
|
|
689
|
+
* Space Complexity: O(1)
|
|
657
690
|
*/
|
|
658
691
|
|
|
659
692
|
/**
|
|
660
|
-
* Time Complexity: O(n)
|
|
661
|
-
* Space Complexity: O(1)
|
|
693
|
+
* Time Complexity: O(n)
|
|
694
|
+
* Space Complexity: O(1)
|
|
662
695
|
*
|
|
663
696
|
* The function counts the number of occurrences of a given value in a linked list.
|
|
664
697
|
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
@@ -678,6 +711,24 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
678
711
|
return count;
|
|
679
712
|
}
|
|
680
713
|
|
|
714
|
+
/**
|
|
715
|
+
* Time Complexity: O(n)
|
|
716
|
+
* Space Complexity: O(n)
|
|
717
|
+
*/
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Time Complexity: O(n)
|
|
721
|
+
* Space Complexity: O(n)
|
|
722
|
+
*
|
|
723
|
+
* The `clone` function returns a new instance of the `SinglyLinkedList` class with the same values
|
|
724
|
+
* as the original list.
|
|
725
|
+
* @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
|
|
726
|
+
* is a clone of the original list.
|
|
727
|
+
*/
|
|
728
|
+
clone(): SinglyLinkedList<E> {
|
|
729
|
+
return new SinglyLinkedList<E>(this.values());
|
|
730
|
+
}
|
|
731
|
+
|
|
681
732
|
/**
|
|
682
733
|
* Time Complexity: O(n)
|
|
683
734
|
* Space Complexity: O(n)
|
|
@@ -713,7 +764,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
713
764
|
}
|
|
714
765
|
|
|
715
766
|
/**
|
|
716
|
-
* Time Complexity: O(n)
|
|
767
|
+
* Time Complexity: O(n)
|
|
717
768
|
* Space Complexity: O(n)
|
|
718
769
|
*/
|
|
719
770
|
/**
|
|
@@ -741,6 +792,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
741
792
|
return mappedList;
|
|
742
793
|
}
|
|
743
794
|
|
|
795
|
+
/**
|
|
796
|
+
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
|
|
797
|
+
*/
|
|
744
798
|
protected* _getIterator(): IterableIterator<E> {
|
|
745
799
|
let current = this.head;
|
|
746
800
|
|