graph-typed 1.47.4 → 1.47.6
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 +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/data-structures/binary-tree/bst.js +56 -40
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/data-structures/hash/hash-map.js +10 -4
- package/dist/data-structures/hash/hash-table.d.ts +9 -4
- package/dist/data-structures/hash/hash-table.js +50 -5
- package/dist/data-structures/heap/heap.d.ts +25 -22
- package/dist/data-structures/heap/heap.js +101 -41
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
- package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +50 -49
- package/dist/data-structures/queue/deque.js +81 -71
- package/dist/data-structures/queue/queue.d.ts +46 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/stack/stack.d.ts +20 -6
- package/dist/data-structures/stack/stack.js +65 -8
- package/dist/data-structures/trie/trie.d.ts +5 -0
- package/dist/data-structures/trie/trie.js +47 -0
- package/dist/interfaces/binary-tree.d.ts +3 -1
- package/dist/types/common.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +13 -7
- package/src/data-structures/hash/hash-table.ts +59 -9
- package/src/data-structures/heap/heap.ts +115 -46
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
- package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +86 -75
- package/src/data-structures/queue/queue.ts +88 -0
- package/src/data-structures/stack/stack.ts +75 -10
- package/src/data-structures/trie/trie.ts +53 -0
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -20,7 +20,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
20
20
|
/**
|
|
21
21
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
22
22
|
*/
|
|
23
|
-
constructor();
|
|
23
|
+
constructor(elements?: Iterable<E>);
|
|
24
24
|
protected _head: DoublyLinkedListNode<E> | null;
|
|
25
25
|
get head(): DoublyLinkedListNode<E> | null;
|
|
26
26
|
protected _tail: DoublyLinkedListNode<E> | null;
|
|
@@ -249,6 +249,23 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
249
249
|
* insertion fails.
|
|
250
250
|
*/
|
|
251
251
|
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
254
|
+
* Space Complexity: O(1)
|
|
255
|
+
*/
|
|
256
|
+
/**
|
|
257
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
258
|
+
* Space Complexity: O(1)
|
|
259
|
+
*
|
|
260
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
261
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
262
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
263
|
+
* itself.
|
|
264
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
265
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
266
|
+
* existing value or node is not found in the doubly linked list.
|
|
267
|
+
*/
|
|
268
|
+
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
252
269
|
/**
|
|
253
270
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
254
271
|
* Space Complexity: O(1)
|
|
@@ -279,18 +296,6 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
279
296
|
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
|
|
280
297
|
*/
|
|
281
298
|
delete(valOrNode: E | DoublyLinkedListNode<E> | null): boolean;
|
|
282
|
-
/**
|
|
283
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
284
|
-
* Space Complexity: O(n)
|
|
285
|
-
*/
|
|
286
|
-
/**
|
|
287
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
288
|
-
* Space Complexity: O(n)
|
|
289
|
-
*
|
|
290
|
-
* The `toArray` function converts a linked list into an array.
|
|
291
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
292
|
-
*/
|
|
293
|
-
toArray(): E[];
|
|
294
299
|
/**
|
|
295
300
|
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
296
301
|
* @returns A boolean value is being returned.
|
|
@@ -346,6 +351,17 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
346
351
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
347
352
|
*/
|
|
348
353
|
findBackward(callback: (value: E) => boolean): E | null;
|
|
354
|
+
/**
|
|
355
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
356
|
+
* Space Complexity: O(1)
|
|
357
|
+
*/
|
|
358
|
+
/**
|
|
359
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
360
|
+
* Space Complexity: O(1)
|
|
361
|
+
*
|
|
362
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
363
|
+
*/
|
|
364
|
+
reverse(): void;
|
|
349
365
|
/**
|
|
350
366
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
351
367
|
* Space Complexity: O(n)
|
|
@@ -354,21 +370,26 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
354
370
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
355
371
|
* Space Complexity: O(n)
|
|
356
372
|
*
|
|
357
|
-
* The `
|
|
358
|
-
* @returns The `
|
|
373
|
+
* The `toArray` function converts a linked list into an array.
|
|
374
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
359
375
|
*/
|
|
360
|
-
|
|
376
|
+
toArray(): E[];
|
|
361
377
|
/**
|
|
362
378
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
363
|
-
* Space Complexity: O(
|
|
379
|
+
* Space Complexity: O(n)
|
|
364
380
|
*/
|
|
365
381
|
/**
|
|
366
382
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
367
|
-
* Space Complexity: O(
|
|
383
|
+
* Space Complexity: O(n)
|
|
368
384
|
*
|
|
369
|
-
* The `
|
|
385
|
+
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
386
|
+
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
370
387
|
*/
|
|
371
|
-
|
|
388
|
+
toReversedArray(): E[];
|
|
389
|
+
/**
|
|
390
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
391
|
+
*/
|
|
392
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
372
393
|
/**
|
|
373
394
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
374
395
|
* Space Complexity: O(1)
|
|
@@ -382,7 +403,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
382
403
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
383
404
|
* current node in the linked list.
|
|
384
405
|
*/
|
|
385
|
-
forEach(callback: (value: E, index: number) => void): void;
|
|
406
|
+
forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void;
|
|
386
407
|
/**
|
|
387
408
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
388
409
|
* Space Complexity: O(n)
|
|
@@ -391,14 +412,13 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
391
412
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
392
413
|
* Space Complexity: O(n)
|
|
393
414
|
*
|
|
394
|
-
* The `
|
|
395
|
-
*
|
|
396
|
-
* @param callback - The callback parameter is a function that takes a value of type E
|
|
397
|
-
*
|
|
398
|
-
* DoublyLinkedList
|
|
399
|
-
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
415
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
416
|
+
* elements that satisfy the given callback function.
|
|
417
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
418
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
419
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
400
420
|
*/
|
|
401
|
-
|
|
421
|
+
filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E>;
|
|
402
422
|
/**
|
|
403
423
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
404
424
|
* Space Complexity: O(n)
|
|
@@ -407,13 +427,14 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
407
427
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
408
428
|
* Space Complexity: O(n)
|
|
409
429
|
*
|
|
410
|
-
* The `
|
|
411
|
-
*
|
|
412
|
-
* @param callback - The
|
|
413
|
-
*
|
|
414
|
-
*
|
|
430
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
431
|
+
* DoublyLinkedList with the transformed values.
|
|
432
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
433
|
+
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
434
|
+
* DoublyLinkedList).
|
|
435
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
|
|
415
436
|
*/
|
|
416
|
-
|
|
437
|
+
map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T>;
|
|
417
438
|
/**
|
|
418
439
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
419
440
|
* Space Complexity: O(n)
|
|
@@ -426,31 +447,11 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
426
447
|
* single value.
|
|
427
448
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
428
449
|
* used to perform a specific operation on each element of the linked list.
|
|
429
|
-
* @param {
|
|
450
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
430
451
|
* point for the reduction operation.
|
|
431
452
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
432
453
|
* elements in the linked list.
|
|
433
454
|
*/
|
|
434
|
-
reduce<
|
|
435
|
-
|
|
436
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
437
|
-
* Space Complexity: O(1)
|
|
438
|
-
*/
|
|
439
|
-
/**
|
|
440
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
441
|
-
* Space Complexity: O(1)
|
|
442
|
-
*
|
|
443
|
-
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
444
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
445
|
-
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
446
|
-
* itself.
|
|
447
|
-
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
448
|
-
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
449
|
-
* existing value or node is not found in the doubly linked list.
|
|
450
|
-
*/
|
|
451
|
-
insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
452
|
-
/**
|
|
453
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
454
|
-
*/
|
|
455
|
-
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
455
|
+
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T;
|
|
456
|
+
print(): void;
|
|
456
457
|
}
|
|
@@ -25,10 +25,15 @@ class DoublyLinkedList {
|
|
|
25
25
|
/**
|
|
26
26
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
27
27
|
*/
|
|
28
|
-
constructor() {
|
|
28
|
+
constructor(elements) {
|
|
29
29
|
this._head = null;
|
|
30
30
|
this._tail = null;
|
|
31
31
|
this._length = 0;
|
|
32
|
+
if (elements) {
|
|
33
|
+
for (const el of elements) {
|
|
34
|
+
this.push(el);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
32
37
|
}
|
|
33
38
|
get head() {
|
|
34
39
|
return this._head;
|
|
@@ -403,6 +408,46 @@ class DoublyLinkedList {
|
|
|
403
408
|
}
|
|
404
409
|
return false;
|
|
405
410
|
}
|
|
411
|
+
/**
|
|
412
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
413
|
+
* Space Complexity: O(1)
|
|
414
|
+
*/
|
|
415
|
+
/**
|
|
416
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
417
|
+
* Space Complexity: O(1)
|
|
418
|
+
*
|
|
419
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
420
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
421
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
422
|
+
* itself.
|
|
423
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
424
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
425
|
+
* existing value or node is not found in the doubly linked list.
|
|
426
|
+
*/
|
|
427
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
428
|
+
let existingNode;
|
|
429
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
430
|
+
existingNode = existingValueOrNode;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
434
|
+
}
|
|
435
|
+
if (existingNode) {
|
|
436
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
437
|
+
newNode.next = existingNode.next;
|
|
438
|
+
if (existingNode.next) {
|
|
439
|
+
existingNode.next.prev = newNode;
|
|
440
|
+
}
|
|
441
|
+
newNode.prev = existingNode;
|
|
442
|
+
existingNode.next = newNode;
|
|
443
|
+
if (existingNode === this.tail) {
|
|
444
|
+
this._tail = newNode;
|
|
445
|
+
}
|
|
446
|
+
this._length++;
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
406
451
|
/**
|
|
407
452
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
408
453
|
* Space Complexity: O(1)
|
|
@@ -472,26 +517,6 @@ class DoublyLinkedList {
|
|
|
472
517
|
}
|
|
473
518
|
return false;
|
|
474
519
|
}
|
|
475
|
-
/**
|
|
476
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
477
|
-
* Space Complexity: O(n)
|
|
478
|
-
*/
|
|
479
|
-
/**
|
|
480
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
481
|
-
* Space Complexity: O(n)
|
|
482
|
-
*
|
|
483
|
-
* The `toArray` function converts a linked list into an array.
|
|
484
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
485
|
-
*/
|
|
486
|
-
toArray() {
|
|
487
|
-
const array = [];
|
|
488
|
-
let current = this.head;
|
|
489
|
-
while (current) {
|
|
490
|
-
array.push(current.value);
|
|
491
|
-
current = current.next;
|
|
492
|
-
}
|
|
493
|
-
return array;
|
|
494
|
-
}
|
|
495
520
|
/**
|
|
496
521
|
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
497
522
|
* @returns A boolean value is being returned.
|
|
@@ -582,6 +607,25 @@ class DoublyLinkedList {
|
|
|
582
607
|
}
|
|
583
608
|
return null;
|
|
584
609
|
}
|
|
610
|
+
/**
|
|
611
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
612
|
+
* Space Complexity: O(1)
|
|
613
|
+
*/
|
|
614
|
+
/**
|
|
615
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
616
|
+
* Space Complexity: O(1)
|
|
617
|
+
*
|
|
618
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
619
|
+
*/
|
|
620
|
+
reverse() {
|
|
621
|
+
let current = this.head;
|
|
622
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
623
|
+
while (current) {
|
|
624
|
+
const next = current.next;
|
|
625
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
626
|
+
current = next;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
585
629
|
/**
|
|
586
630
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
587
631
|
* Space Complexity: O(n)
|
|
@@ -590,35 +634,46 @@ class DoublyLinkedList {
|
|
|
590
634
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
591
635
|
* Space Complexity: O(n)
|
|
592
636
|
*
|
|
593
|
-
* The `
|
|
594
|
-
* @returns The `
|
|
637
|
+
* The `toArray` function converts a linked list into an array.
|
|
638
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
595
639
|
*/
|
|
596
|
-
|
|
640
|
+
toArray() {
|
|
597
641
|
const array = [];
|
|
598
|
-
let current = this.
|
|
642
|
+
let current = this.head;
|
|
599
643
|
while (current) {
|
|
600
644
|
array.push(current.value);
|
|
601
|
-
current = current.
|
|
645
|
+
current = current.next;
|
|
602
646
|
}
|
|
603
647
|
return array;
|
|
604
648
|
}
|
|
605
649
|
/**
|
|
606
650
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
607
|
-
* Space Complexity: O(
|
|
651
|
+
* Space Complexity: O(n)
|
|
608
652
|
*/
|
|
609
653
|
/**
|
|
610
654
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
611
|
-
* Space Complexity: O(
|
|
655
|
+
* Space Complexity: O(n)
|
|
612
656
|
*
|
|
613
|
-
* The `
|
|
657
|
+
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
658
|
+
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
614
659
|
*/
|
|
615
|
-
|
|
660
|
+
toReversedArray() {
|
|
661
|
+
const array = [];
|
|
662
|
+
let current = this.tail;
|
|
663
|
+
while (current) {
|
|
664
|
+
array.push(current.value);
|
|
665
|
+
current = current.prev;
|
|
666
|
+
}
|
|
667
|
+
return array;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
671
|
+
*/
|
|
672
|
+
*[Symbol.iterator]() {
|
|
616
673
|
let current = this.head;
|
|
617
|
-
[this._head, this._tail] = [this.tail, this.head];
|
|
618
674
|
while (current) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
current = next;
|
|
675
|
+
yield current.value;
|
|
676
|
+
current = current.next;
|
|
622
677
|
}
|
|
623
678
|
}
|
|
624
679
|
/**
|
|
@@ -635,11 +690,9 @@ class DoublyLinkedList {
|
|
|
635
690
|
* current node in the linked list.
|
|
636
691
|
*/
|
|
637
692
|
forEach(callback) {
|
|
638
|
-
let current = this.head;
|
|
639
693
|
let index = 0;
|
|
640
|
-
|
|
641
|
-
callback(
|
|
642
|
-
current = current.next;
|
|
694
|
+
for (const el of this) {
|
|
695
|
+
callback(el, index, this);
|
|
643
696
|
index++;
|
|
644
697
|
}
|
|
645
698
|
}
|
|
@@ -651,21 +704,22 @@ class DoublyLinkedList {
|
|
|
651
704
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
652
705
|
* Space Complexity: O(n)
|
|
653
706
|
*
|
|
654
|
-
* The `
|
|
655
|
-
*
|
|
656
|
-
* @param callback - The callback parameter is a function that takes a value of type E
|
|
657
|
-
*
|
|
658
|
-
* DoublyLinkedList
|
|
659
|
-
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
707
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
708
|
+
* elements that satisfy the given callback function.
|
|
709
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
710
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
711
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
660
712
|
*/
|
|
661
|
-
|
|
662
|
-
const
|
|
663
|
-
let
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
713
|
+
filter(callback) {
|
|
714
|
+
const filteredList = new DoublyLinkedList();
|
|
715
|
+
let index = 0;
|
|
716
|
+
for (const current of this) {
|
|
717
|
+
if (callback(current, index, this)) {
|
|
718
|
+
filteredList.push(current);
|
|
719
|
+
}
|
|
720
|
+
index++;
|
|
667
721
|
}
|
|
668
|
-
return
|
|
722
|
+
return filteredList;
|
|
669
723
|
}
|
|
670
724
|
/**
|
|
671
725
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -675,22 +729,21 @@ class DoublyLinkedList {
|
|
|
675
729
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
676
730
|
* Space Complexity: O(n)
|
|
677
731
|
*
|
|
678
|
-
* The `
|
|
679
|
-
*
|
|
680
|
-
* @param callback - The
|
|
681
|
-
*
|
|
682
|
-
*
|
|
732
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
733
|
+
* DoublyLinkedList with the transformed values.
|
|
734
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
735
|
+
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
736
|
+
* DoublyLinkedList).
|
|
737
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
|
|
683
738
|
*/
|
|
684
|
-
|
|
685
|
-
const
|
|
686
|
-
let
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
}
|
|
691
|
-
current = current.next;
|
|
739
|
+
map(callback) {
|
|
740
|
+
const mappedList = new DoublyLinkedList();
|
|
741
|
+
let index = 0;
|
|
742
|
+
for (const current of this) {
|
|
743
|
+
mappedList.push(callback(current, index, this));
|
|
744
|
+
index++;
|
|
692
745
|
}
|
|
693
|
-
return
|
|
746
|
+
return mappedList;
|
|
694
747
|
}
|
|
695
748
|
/**
|
|
696
749
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -704,69 +757,22 @@ class DoublyLinkedList {
|
|
|
704
757
|
* single value.
|
|
705
758
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
706
759
|
* used to perform a specific operation on each element of the linked list.
|
|
707
|
-
* @param {
|
|
760
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
708
761
|
* point for the reduction operation.
|
|
709
762
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
710
763
|
* elements in the linked list.
|
|
711
764
|
*/
|
|
712
765
|
reduce(callback, initialValue) {
|
|
713
766
|
let accumulator = initialValue;
|
|
714
|
-
let
|
|
715
|
-
|
|
716
|
-
accumulator = callback(accumulator, current
|
|
717
|
-
|
|
767
|
+
let index = 0;
|
|
768
|
+
for (const current of this) {
|
|
769
|
+
accumulator = callback(accumulator, current, index, this);
|
|
770
|
+
index++;
|
|
718
771
|
}
|
|
719
772
|
return accumulator;
|
|
720
773
|
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
* Space Complexity: O(1)
|
|
724
|
-
*/
|
|
725
|
-
/**
|
|
726
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
727
|
-
* Space Complexity: O(1)
|
|
728
|
-
*
|
|
729
|
-
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
730
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
731
|
-
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
732
|
-
* itself.
|
|
733
|
-
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
734
|
-
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
735
|
-
* existing value or node is not found in the doubly linked list.
|
|
736
|
-
*/
|
|
737
|
-
insertAfter(existingValueOrNode, newValue) {
|
|
738
|
-
let existingNode;
|
|
739
|
-
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
740
|
-
existingNode = existingValueOrNode;
|
|
741
|
-
}
|
|
742
|
-
else {
|
|
743
|
-
existingNode = this.getNode(existingValueOrNode);
|
|
744
|
-
}
|
|
745
|
-
if (existingNode) {
|
|
746
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
747
|
-
newNode.next = existingNode.next;
|
|
748
|
-
if (existingNode.next) {
|
|
749
|
-
existingNode.next.prev = newNode;
|
|
750
|
-
}
|
|
751
|
-
newNode.prev = existingNode;
|
|
752
|
-
existingNode.next = newNode;
|
|
753
|
-
if (existingNode === this.tail) {
|
|
754
|
-
this._tail = newNode;
|
|
755
|
-
}
|
|
756
|
-
this._length++;
|
|
757
|
-
return true;
|
|
758
|
-
}
|
|
759
|
-
return false;
|
|
760
|
-
}
|
|
761
|
-
/**
|
|
762
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
763
|
-
*/
|
|
764
|
-
*[Symbol.iterator]() {
|
|
765
|
-
let current = this.head;
|
|
766
|
-
while (current) {
|
|
767
|
-
yield current.value;
|
|
768
|
-
current = current.next;
|
|
769
|
-
}
|
|
774
|
+
print() {
|
|
775
|
+
console.log([...this]);
|
|
770
776
|
}
|
|
771
777
|
}
|
|
772
778
|
exports.DoublyLinkedList = DoublyLinkedList;
|
|
@@ -19,7 +19,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
19
19
|
/**
|
|
20
20
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
21
21
|
*/
|
|
22
|
-
constructor();
|
|
22
|
+
constructor(elements?: Iterable<E>);
|
|
23
23
|
protected _head: SinglyLinkedListNode<E> | null;
|
|
24
24
|
get head(): SinglyLinkedListNode<E> | null;
|
|
25
25
|
protected _tail: SinglyLinkedListNode<E> | null;
|
|
@@ -345,42 +345,30 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
345
345
|
*/
|
|
346
346
|
countOccurrences(value: E): number;
|
|
347
347
|
/**
|
|
348
|
-
*
|
|
349
|
-
|
|
348
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
349
|
+
*/
|
|
350
|
+
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
351
|
+
/**
|
|
352
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
353
|
+
* Space Complexity: O(1)
|
|
350
354
|
*/
|
|
351
355
|
/**
|
|
352
|
-
* Time Complexity: O(n)
|
|
353
|
-
* Space Complexity: O(1)
|
|
356
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
357
|
+
* Space Complexity: O(1)
|
|
354
358
|
*
|
|
355
359
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
356
360
|
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
357
361
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
358
362
|
* current node in the linked list.
|
|
359
363
|
*/
|
|
360
|
-
forEach(callback: (value: E, index: number) => void): void;
|
|
361
|
-
/**
|
|
362
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
363
|
-
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
364
|
-
*/
|
|
365
|
-
/**
|
|
366
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as they need to traverse the entire list to apply the callback or reduce the list.
|
|
367
|
-
* Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
|
|
368
|
-
*
|
|
369
|
-
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
370
|
-
* SinglyLinkedList with the transformed values.
|
|
371
|
-
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
372
|
-
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
373
|
-
* SinglyLinkedList).
|
|
374
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
375
|
-
*/
|
|
376
|
-
map<U>(callback: (value: E) => U): SinglyLinkedList<U>;
|
|
364
|
+
forEach(callback: (value: E, index: number, list: SinglyLinkedList<E>) => void): void;
|
|
377
365
|
/**
|
|
378
|
-
* Time Complexity: O(n)
|
|
379
|
-
* Space Complexity: O(n)
|
|
366
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
367
|
+
* Space Complexity: O(n)
|
|
380
368
|
*/
|
|
381
369
|
/**
|
|
382
|
-
* Time Complexity: O(n)
|
|
383
|
-
* Space Complexity: O(n)
|
|
370
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
371
|
+
* Space Complexity: O(n)
|
|
384
372
|
*
|
|
385
373
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
386
374
|
* elements that satisfy the given callback function.
|
|
@@ -388,27 +376,40 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
388
376
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
389
377
|
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
390
378
|
*/
|
|
391
|
-
filter(callback: (value: E) => boolean): SinglyLinkedList<E>;
|
|
379
|
+
filter(callback: (value: E, index: number, list: SinglyLinkedList<E>) => boolean): SinglyLinkedList<E>;
|
|
392
380
|
/**
|
|
393
|
-
* Time Complexity: O(n)
|
|
394
|
-
* Space Complexity: O(n)
|
|
381
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
382
|
+
* Space Complexity: O(n)
|
|
395
383
|
*/
|
|
396
384
|
/**
|
|
397
|
-
* Time Complexity: O(n)
|
|
398
|
-
* Space Complexity: O(n)
|
|
385
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
386
|
+
* Space Complexity: O(n)
|
|
387
|
+
*
|
|
388
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
389
|
+
* SinglyLinkedList with the transformed values.
|
|
390
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
391
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
392
|
+
* SinglyLinkedList).
|
|
393
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
394
|
+
*/
|
|
395
|
+
map<T>(callback: (value: E, index: number, list: SinglyLinkedList<E>) => T): SinglyLinkedList<T>;
|
|
396
|
+
/**
|
|
397
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
398
|
+
* Space Complexity: O(n)
|
|
399
|
+
*/
|
|
400
|
+
/**
|
|
401
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
402
|
+
* Space Complexity: O(n)
|
|
399
403
|
*
|
|
400
404
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
401
405
|
* single value.
|
|
402
406
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
403
407
|
* used to perform a specific operation on each element of the linked list.
|
|
404
|
-
* @param {
|
|
408
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
405
409
|
* point for the reduction operation.
|
|
406
410
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
407
411
|
* elements in the linked list.
|
|
408
412
|
*/
|
|
409
|
-
reduce<
|
|
410
|
-
|
|
411
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
412
|
-
*/
|
|
413
|
-
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
413
|
+
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
|
|
414
|
+
print(): void;
|
|
414
415
|
}
|