graph-typed 1.49.1 → 1.49.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 +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/data-structures/graph/abstract-graph.js +43 -12
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +2 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/data-structures/hash/hash-map.js +16 -15
- package/dist/data-structures/heap/heap.d.ts +6 -35
- package/dist/data-structures/heap/heap.js +10 -42
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/data-structures/linked-list/doubly-linked-list.js +143 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/data-structures/queue/deque.d.ts +70 -75
- package/dist/data-structures/queue/deque.js +100 -110
- package/dist/data-structures/queue/queue.d.ts +37 -38
- package/dist/data-structures/queue/queue.js +46 -49
- package/dist/data-structures/stack/stack.d.ts +2 -3
- package/dist/data-structures/stack/stack.js +2 -5
- package/dist/data-structures/trie/trie.d.ts +1 -2
- package/dist/data-structures/trie/trie.js +2 -5
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
|
@@ -23,13 +23,13 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
23
23
|
*/
|
|
24
24
|
class DoublyLinkedList extends base_1.IterableElementBase {
|
|
25
25
|
/**
|
|
26
|
-
* The constructor initializes the linked list with an empty head, tail, and
|
|
26
|
+
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
27
27
|
*/
|
|
28
28
|
constructor(elements) {
|
|
29
29
|
super();
|
|
30
30
|
this._head = undefined;
|
|
31
31
|
this._tail = undefined;
|
|
32
|
-
this.
|
|
32
|
+
this._size = 0;
|
|
33
33
|
if (elements) {
|
|
34
34
|
for (const el of elements) {
|
|
35
35
|
this.push(el);
|
|
@@ -42,18 +42,45 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
42
42
|
get tail() {
|
|
43
43
|
return this._tail;
|
|
44
44
|
}
|
|
45
|
-
get length() {
|
|
46
|
-
return this._length;
|
|
47
|
-
}
|
|
48
45
|
get size() {
|
|
49
|
-
return this.
|
|
46
|
+
return this._size;
|
|
50
47
|
}
|
|
51
48
|
/**
|
|
52
|
-
* Time Complexity: O(n), where n is the
|
|
49
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
53
50
|
* Space Complexity: O(n)
|
|
54
51
|
*/
|
|
55
52
|
/**
|
|
56
|
-
* Time Complexity: O(n), where n is the
|
|
53
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
54
|
+
* Space Complexity: O(1)
|
|
55
|
+
*
|
|
56
|
+
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
57
|
+
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
58
|
+
*/
|
|
59
|
+
get first() {
|
|
60
|
+
var _a;
|
|
61
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Time Complexity: O(1)
|
|
65
|
+
* Space Complexity: O(1)
|
|
66
|
+
*/
|
|
67
|
+
/**
|
|
68
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
69
|
+
* Space Complexity: O(1)
|
|
70
|
+
*
|
|
71
|
+
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
72
|
+
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
73
|
+
*/
|
|
74
|
+
get last() {
|
|
75
|
+
var _a;
|
|
76
|
+
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.value;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Time Complexity: O(1)
|
|
80
|
+
* Space Complexity: O(1)
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
57
84
|
* Space Complexity: O(n)
|
|
58
85
|
*
|
|
59
86
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
@@ -90,21 +117,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
90
117
|
this.tail.next = newNode;
|
|
91
118
|
this._tail = newNode;
|
|
92
119
|
}
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Time Complexity: O(1)
|
|
97
|
-
* Space Complexity: O(1)
|
|
98
|
-
*/
|
|
99
|
-
/**
|
|
100
|
-
* Time Complexity: O(1)
|
|
101
|
-
* Space Complexity: O(1)
|
|
102
|
-
*
|
|
103
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
104
|
-
* @param {E} value - The value to be added to the linked list.
|
|
105
|
-
*/
|
|
106
|
-
addLast(value) {
|
|
107
|
-
this.push(value);
|
|
120
|
+
this._size++;
|
|
121
|
+
return true;
|
|
108
122
|
}
|
|
109
123
|
/**
|
|
110
124
|
* Time Complexity: O(1)
|
|
@@ -130,26 +144,11 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
130
144
|
this._tail = removedNode.prev;
|
|
131
145
|
this.tail.next = undefined;
|
|
132
146
|
}
|
|
133
|
-
this.
|
|
147
|
+
this._size--;
|
|
134
148
|
return removedNode.value;
|
|
135
149
|
}
|
|
136
150
|
/**
|
|
137
|
-
* Time Complexity: O(
|
|
138
|
-
* Space Complexity: O(1)
|
|
139
|
-
*/
|
|
140
|
-
/**
|
|
141
|
-
* Time Complexity: O(1)
|
|
142
|
-
* Space Complexity: O(1)
|
|
143
|
-
*
|
|
144
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
145
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
146
|
-
* list is empty, it returns undefined.
|
|
147
|
-
*/
|
|
148
|
-
pollLast() {
|
|
149
|
-
return this.pop();
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Time Complexity: O(1)
|
|
151
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
153
152
|
* Space Complexity: O(1)
|
|
154
153
|
*/
|
|
155
154
|
/**
|
|
@@ -172,26 +171,11 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
172
171
|
this._head = removedNode.next;
|
|
173
172
|
this.head.prev = undefined;
|
|
174
173
|
}
|
|
175
|
-
this.
|
|
174
|
+
this._size--;
|
|
176
175
|
return removedNode.value;
|
|
177
176
|
}
|
|
178
177
|
/**
|
|
179
|
-
* Time Complexity: O(
|
|
180
|
-
* Space Complexity: O(1)
|
|
181
|
-
*/
|
|
182
|
-
/**
|
|
183
|
-
* Time Complexity: O(1)
|
|
184
|
-
* Space Complexity: O(1)
|
|
185
|
-
*
|
|
186
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
187
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
188
|
-
* list.
|
|
189
|
-
*/
|
|
190
|
-
pollFirst() {
|
|
191
|
-
return this.shift();
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Time Complexity: O(1)
|
|
178
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
195
179
|
* Space Complexity: O(1)
|
|
196
180
|
*/
|
|
197
181
|
/**
|
|
@@ -213,52 +197,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
213
197
|
this.head.prev = newNode;
|
|
214
198
|
this._head = newNode;
|
|
215
199
|
}
|
|
216
|
-
this.
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Time Complexity: O(1)
|
|
220
|
-
* Space Complexity: O(1)
|
|
221
|
-
*/
|
|
222
|
-
/**
|
|
223
|
-
* Time Complexity: O(1)
|
|
224
|
-
* Space Complexity: O(1)
|
|
225
|
-
*
|
|
226
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
227
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
228
|
-
* doubly linked list.
|
|
229
|
-
*/
|
|
230
|
-
addFirst(value) {
|
|
231
|
-
this.unshift(value);
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
235
|
-
* Space Complexity: O(1)
|
|
236
|
-
*/
|
|
237
|
-
/**
|
|
238
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
239
|
-
* Space Complexity: O(1)
|
|
240
|
-
*
|
|
241
|
-
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
242
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
243
|
-
*/
|
|
244
|
-
getFirst() {
|
|
245
|
-
var _a;
|
|
246
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
250
|
-
* Space Complexity: O(1)
|
|
251
|
-
*/
|
|
252
|
-
/**
|
|
253
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
254
|
-
* Space Complexity: O(1)
|
|
255
|
-
*
|
|
256
|
-
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
257
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
258
|
-
*/
|
|
259
|
-
getLast() {
|
|
260
|
-
var _a;
|
|
261
|
-
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.value;
|
|
200
|
+
this._size++;
|
|
201
|
+
return true;
|
|
262
202
|
}
|
|
263
203
|
/**
|
|
264
204
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -275,7 +215,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
275
215
|
* or the linked list is empty, it will return undefined.
|
|
276
216
|
*/
|
|
277
217
|
getAt(index) {
|
|
278
|
-
if (index < 0 || index >= this.
|
|
218
|
+
if (index < 0 || index >= this.size)
|
|
279
219
|
return undefined;
|
|
280
220
|
let current = this.head;
|
|
281
221
|
for (let i = 0; i < index; i++) {
|
|
@@ -299,7 +239,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
299
239
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
300
240
|
*/
|
|
301
241
|
getNodeAt(index) {
|
|
302
|
-
if (index < 0 || index >= this.
|
|
242
|
+
if (index < 0 || index >= this.size)
|
|
303
243
|
return undefined;
|
|
304
244
|
let current = this.head;
|
|
305
245
|
for (let i = 0; i < index; i++) {
|
|
@@ -347,14 +287,14 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
347
287
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
348
288
|
* if the index is out of bounds.
|
|
349
289
|
*/
|
|
350
|
-
|
|
351
|
-
if (index < 0 || index > this.
|
|
290
|
+
addAt(index, value) {
|
|
291
|
+
if (index < 0 || index > this.size)
|
|
352
292
|
return false;
|
|
353
293
|
if (index === 0) {
|
|
354
294
|
this.unshift(value);
|
|
355
295
|
return true;
|
|
356
296
|
}
|
|
357
|
-
if (index === this.
|
|
297
|
+
if (index === this.size) {
|
|
358
298
|
this.push(value);
|
|
359
299
|
return true;
|
|
360
300
|
}
|
|
@@ -365,7 +305,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
365
305
|
newNode.next = nextNode;
|
|
366
306
|
prevNode.next = newNode;
|
|
367
307
|
nextNode.prev = newNode;
|
|
368
|
-
this.
|
|
308
|
+
this._size++;
|
|
369
309
|
return true;
|
|
370
310
|
}
|
|
371
311
|
/**
|
|
@@ -376,7 +316,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
376
316
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
377
317
|
* Space Complexity: O(1)
|
|
378
318
|
*
|
|
379
|
-
* The `
|
|
319
|
+
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
380
320
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
381
321
|
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
382
322
|
* itself.
|
|
@@ -385,7 +325,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
385
325
|
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
386
326
|
* insertion fails.
|
|
387
327
|
*/
|
|
388
|
-
|
|
328
|
+
addBefore(existingValueOrNode, newValue) {
|
|
389
329
|
let existingNode;
|
|
390
330
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
391
331
|
existingNode = existingValueOrNode;
|
|
@@ -404,7 +344,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
404
344
|
if (existingNode === this.head) {
|
|
405
345
|
this._head = newNode;
|
|
406
346
|
}
|
|
407
|
-
this.
|
|
347
|
+
this._size++;
|
|
408
348
|
return true;
|
|
409
349
|
}
|
|
410
350
|
return false;
|
|
@@ -417,7 +357,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
417
357
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
418
358
|
* Space Complexity: O(1)
|
|
419
359
|
*
|
|
420
|
-
* The `
|
|
360
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
421
361
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
422
362
|
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
423
363
|
* itself.
|
|
@@ -425,7 +365,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
425
365
|
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
426
366
|
* existing value or node is not found in the doubly linked list.
|
|
427
367
|
*/
|
|
428
|
-
|
|
368
|
+
addAfter(existingValueOrNode, newValue) {
|
|
429
369
|
let existingNode;
|
|
430
370
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
431
371
|
existingNode = existingValueOrNode;
|
|
@@ -444,15 +384,11 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
444
384
|
if (existingNode === this.tail) {
|
|
445
385
|
this._tail = newNode;
|
|
446
386
|
}
|
|
447
|
-
this.
|
|
387
|
+
this._size++;
|
|
448
388
|
return true;
|
|
449
389
|
}
|
|
450
390
|
return false;
|
|
451
391
|
}
|
|
452
|
-
/**
|
|
453
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
454
|
-
* Space Complexity: O(1)
|
|
455
|
-
*/
|
|
456
392
|
/**
|
|
457
393
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
458
394
|
* Space Complexity: O(1)
|
|
@@ -464,24 +400,24 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
464
400
|
* bounds.
|
|
465
401
|
*/
|
|
466
402
|
deleteAt(index) {
|
|
467
|
-
if (index < 0 || index >= this.
|
|
468
|
-
return
|
|
469
|
-
if (index === 0)
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
403
|
+
if (index < 0 || index >= this.size)
|
|
404
|
+
return false;
|
|
405
|
+
if (index === 0) {
|
|
406
|
+
this.shift();
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
if (index === this.size - 1) {
|
|
410
|
+
this.pop();
|
|
411
|
+
return true;
|
|
412
|
+
}
|
|
473
413
|
const removedNode = this.getNodeAt(index);
|
|
474
414
|
const prevNode = removedNode.prev;
|
|
475
415
|
const nextNode = removedNode.next;
|
|
476
416
|
prevNode.next = nextNode;
|
|
477
417
|
nextNode.prev = prevNode;
|
|
478
|
-
this.
|
|
479
|
-
return
|
|
418
|
+
this._size--;
|
|
419
|
+
return true;
|
|
480
420
|
}
|
|
481
|
-
/**
|
|
482
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
483
|
-
* Space Complexity: O(1)
|
|
484
|
-
*/
|
|
485
421
|
/**
|
|
486
422
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
487
423
|
* Space Complexity: O(1)
|
|
@@ -512,26 +448,34 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
512
448
|
const nextNode = node.next;
|
|
513
449
|
prevNode.next = nextNode;
|
|
514
450
|
nextNode.prev = prevNode;
|
|
515
|
-
this.
|
|
451
|
+
this._size--;
|
|
516
452
|
}
|
|
517
453
|
return true;
|
|
518
454
|
}
|
|
519
455
|
return false;
|
|
520
456
|
}
|
|
521
457
|
/**
|
|
522
|
-
*
|
|
458
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
459
|
+
* Space Complexity: O(1)
|
|
460
|
+
*/
|
|
461
|
+
/**
|
|
462
|
+
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
523
463
|
* @returns A boolean value is being returned.
|
|
524
464
|
*/
|
|
525
465
|
isEmpty() {
|
|
526
|
-
return this.
|
|
466
|
+
return this.size === 0;
|
|
527
467
|
}
|
|
528
468
|
/**
|
|
529
|
-
*
|
|
469
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
470
|
+
* Space Complexity: O(1)
|
|
471
|
+
*/
|
|
472
|
+
/**
|
|
473
|
+
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
530
474
|
*/
|
|
531
475
|
clear() {
|
|
532
476
|
this._head = undefined;
|
|
533
477
|
this._tail = undefined;
|
|
534
|
-
this.
|
|
478
|
+
this._size = 0;
|
|
535
479
|
}
|
|
536
480
|
/**
|
|
537
481
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -585,7 +529,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
585
529
|
}
|
|
586
530
|
/**
|
|
587
531
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
588
|
-
* Space Complexity: O(
|
|
532
|
+
* Space Complexity: O(n)
|
|
589
533
|
*/
|
|
590
534
|
/**
|
|
591
535
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -610,7 +554,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
610
554
|
}
|
|
611
555
|
/**
|
|
612
556
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
613
|
-
* Space Complexity: O(
|
|
557
|
+
* Space Complexity: O(n)
|
|
614
558
|
*/
|
|
615
559
|
/**
|
|
616
560
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -626,9 +570,10 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
626
570
|
[current.prev, current.next] = [current.next, current.prev];
|
|
627
571
|
current = next;
|
|
628
572
|
}
|
|
573
|
+
return this;
|
|
629
574
|
}
|
|
630
575
|
/**
|
|
631
|
-
* Time Complexity: O(n)
|
|
576
|
+
* Time Complexity: O(n)
|
|
632
577
|
* Space Complexity: O(n)
|
|
633
578
|
*/
|
|
634
579
|
/**
|
|
@@ -668,8 +613,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
668
613
|
return array;
|
|
669
614
|
}
|
|
670
615
|
/**
|
|
671
|
-
* Time Complexity: O(
|
|
672
|
-
* Space Complexity: O(
|
|
616
|
+
* Time Complexity: O(1)
|
|
617
|
+
* Space Complexity: O(1)
|
|
673
618
|
*/
|
|
674
619
|
/**
|
|
675
620
|
* Time Complexity: O(n)
|
|
@@ -700,8 +645,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
700
645
|
return filteredList;
|
|
701
646
|
}
|
|
702
647
|
/**
|
|
703
|
-
* Time Complexity: O(
|
|
704
|
-
* Space Complexity: O(
|
|
648
|
+
* Time Complexity: O(1)
|
|
649
|
+
* Space Complexity: O(1)
|
|
705
650
|
*/
|
|
706
651
|
/**
|
|
707
652
|
* Time Complexity: O(n)
|
|
@@ -729,12 +674,64 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
729
674
|
}
|
|
730
675
|
return mappedList;
|
|
731
676
|
}
|
|
677
|
+
/**
|
|
678
|
+
* Time Complexity: O(1)
|
|
679
|
+
* Space Complexity: O(1)
|
|
680
|
+
*/
|
|
681
|
+
/**
|
|
682
|
+
* Time Complexity: O(1)
|
|
683
|
+
* Space Complexity: O(1)
|
|
684
|
+
*
|
|
685
|
+
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
686
|
+
* @param {E} value - The value to be added to the linked list.
|
|
687
|
+
*/
|
|
688
|
+
addLast(value) {
|
|
689
|
+
return this.push(value);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Time Complexity: O(1)
|
|
693
|
+
* Space Complexity: O(1)
|
|
694
|
+
*/
|
|
695
|
+
/**
|
|
696
|
+
* Time Complexity: O(1)
|
|
697
|
+
* Space Complexity: O(1)
|
|
698
|
+
*
|
|
699
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
700
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
701
|
+
* list is empty, it returns undefined.
|
|
702
|
+
*/
|
|
703
|
+
pollLast() {
|
|
704
|
+
return this.pop();
|
|
705
|
+
}
|
|
732
706
|
/**
|
|
733
707
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
734
|
-
* Space Complexity: O(
|
|
708
|
+
* Space Complexity: O(1)
|
|
735
709
|
*/
|
|
736
|
-
|
|
737
|
-
|
|
710
|
+
/**
|
|
711
|
+
* Time Complexity: O(1)
|
|
712
|
+
* Space Complexity: O(1)
|
|
713
|
+
*
|
|
714
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
715
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
716
|
+
* list.
|
|
717
|
+
*/
|
|
718
|
+
pollFirst() {
|
|
719
|
+
return this.shift();
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
723
|
+
* Space Complexity: O(1)
|
|
724
|
+
*/
|
|
725
|
+
/**
|
|
726
|
+
* Time Complexity: O(1)
|
|
727
|
+
* Space Complexity: O(1)
|
|
728
|
+
*
|
|
729
|
+
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
730
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
731
|
+
* doubly linked list.
|
|
732
|
+
*/
|
|
733
|
+
addFirst(value) {
|
|
734
|
+
this.unshift(value);
|
|
738
735
|
}
|
|
739
736
|
/**
|
|
740
737
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
@@ -26,8 +26,8 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
26
26
|
get head(): SinglyLinkedListNode<E> | undefined;
|
|
27
27
|
protected _tail: SinglyLinkedListNode<E> | undefined;
|
|
28
28
|
get tail(): SinglyLinkedListNode<E> | undefined;
|
|
29
|
-
protected
|
|
30
|
-
get
|
|
29
|
+
protected _size: number;
|
|
30
|
+
get size(): number;
|
|
31
31
|
/**
|
|
32
32
|
* Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
|
|
33
33
|
* Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
|
|
@@ -54,7 +54,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
54
54
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
55
55
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
56
56
|
*/
|
|
57
|
-
push(value: E):
|
|
57
|
+
push(value: E): boolean;
|
|
58
58
|
/**
|
|
59
59
|
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
60
60
|
* Space Complexity: O(1) - Constant space, as it only creates a new node.
|
|
@@ -67,7 +67,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
67
67
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
68
68
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
69
69
|
*/
|
|
70
|
-
addLast(value: E):
|
|
70
|
+
addLast(value: E): boolean;
|
|
71
71
|
/**
|
|
72
72
|
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
73
73
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -132,7 +132,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
132
132
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
133
133
|
* linked list.
|
|
134
134
|
*/
|
|
135
|
-
unshift(value: E):
|
|
135
|
+
unshift(value: E): boolean;
|
|
136
136
|
/**
|
|
137
137
|
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
138
138
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -145,7 +145,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
145
145
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
146
146
|
* linked list.
|
|
147
147
|
*/
|
|
148
|
-
addFirst(value: E):
|
|
148
|
+
addFirst(value: E): boolean;
|
|
149
149
|
/**
|
|
150
150
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
151
151
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -190,7 +190,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
190
190
|
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
191
191
|
* bounds.
|
|
192
192
|
*/
|
|
193
|
-
deleteAt(index: number):
|
|
193
|
+
deleteAt(index: number): boolean;
|
|
194
194
|
/**
|
|
195
195
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
196
196
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -205,7 +205,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
205
205
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
206
206
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
207
207
|
*/
|
|
208
|
-
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined
|
|
208
|
+
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined): boolean;
|
|
209
209
|
/**
|
|
210
210
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
211
211
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -214,7 +214,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
214
214
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
215
215
|
* Space Complexity: O(1) - Constant space.
|
|
216
216
|
*
|
|
217
|
-
* The `
|
|
217
|
+
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
218
218
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
219
219
|
* linked list. It is of type number.
|
|
220
220
|
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
@@ -222,7 +222,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
222
222
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
223
223
|
* if the index is out of bounds.
|
|
224
224
|
*/
|
|
225
|
-
|
|
225
|
+
addAt(index: number, value: E): boolean;
|
|
226
226
|
/**
|
|
227
227
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
228
228
|
* whether it is empty or not.
|
|
@@ -256,7 +256,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
256
256
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
257
257
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
258
258
|
*/
|
|
259
|
-
reverse():
|
|
259
|
+
reverse(): this;
|
|
260
260
|
/**
|
|
261
261
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
262
262
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -309,14 +309,14 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
309
309
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
310
310
|
* Space Complexity: O(1) - Constant space.
|
|
311
311
|
*
|
|
312
|
-
* The `
|
|
312
|
+
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
313
313
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
314
314
|
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
315
315
|
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
316
|
-
* @returns The method `
|
|
316
|
+
* @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
317
317
|
* inserted before the existing value, and `false` otherwise.
|
|
318
318
|
*/
|
|
319
|
-
|
|
319
|
+
addBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
320
320
|
/**
|
|
321
321
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
322
322
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -325,14 +325,14 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
325
325
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
326
326
|
* Space Complexity: O(1) - Constant space.
|
|
327
327
|
*
|
|
328
|
-
* The `
|
|
328
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
329
329
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
330
330
|
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
331
331
|
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
332
332
|
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
333
333
|
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
334
334
|
*/
|
|
335
|
-
|
|
335
|
+
addAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
336
336
|
/**
|
|
337
337
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
338
338
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -387,10 +387,5 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
387
387
|
* of applying the provided `callback` function to each element in the original list.
|
|
388
388
|
*/
|
|
389
389
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T>;
|
|
390
|
-
/**
|
|
391
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
392
|
-
* Space Complexity: O(n)
|
|
393
|
-
*/
|
|
394
|
-
print(): void;
|
|
395
390
|
protected _getIterator(): IterableIterator<E>;
|
|
396
391
|
}
|