undirected-graph-typed 1.47.4 → 1.47.5
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/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 +6 -1
- package/dist/data-structures/heap/heap.js +51 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +117 -119
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -36
- package/dist/data-structures/linked-list/singly-linked-list.js +58 -60
- package/dist/data-structures/queue/deque.d.ts +49 -49
- package/dist/data-structures/queue/deque.js +79 -72
- package/dist/data-structures/queue/queue.d.ts +45 -0
- package/dist/data-structures/queue/queue.js +77 -0
- package/dist/data-structures/stack/stack.d.ts +18 -5
- package/dist/data-structures/stack/stack.js +56 -7
- package/dist/data-structures/trie/trie.d.ts +5 -0
- package/dist/data-structures/trie/trie.js +47 -0
- package/package.json +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 +60 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
- package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
- package/src/data-structures/queue/deque.ts +84 -77
- package/src/data-structures/queue/queue.ts +84 -0
- package/src/data-structures/stack/stack.ts +64 -8
- package/src/data-structures/trie/trie.ts +53 -0
|
@@ -403,6 +403,46 @@ class DoublyLinkedList {
|
|
|
403
403
|
}
|
|
404
404
|
return false;
|
|
405
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
408
|
+
* Space Complexity: O(1)
|
|
409
|
+
*/
|
|
410
|
+
/**
|
|
411
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
412
|
+
* Space Complexity: O(1)
|
|
413
|
+
*
|
|
414
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
415
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
416
|
+
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
417
|
+
* itself.
|
|
418
|
+
* @param {E} newValue - The value that you want to insert into the doubly linked list.
|
|
419
|
+
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
420
|
+
* existing value or node is not found in the doubly linked list.
|
|
421
|
+
*/
|
|
422
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
423
|
+
let existingNode;
|
|
424
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
425
|
+
existingNode = existingValueOrNode;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
429
|
+
}
|
|
430
|
+
if (existingNode) {
|
|
431
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
432
|
+
newNode.next = existingNode.next;
|
|
433
|
+
if (existingNode.next) {
|
|
434
|
+
existingNode.next.prev = newNode;
|
|
435
|
+
}
|
|
436
|
+
newNode.prev = existingNode;
|
|
437
|
+
existingNode.next = newNode;
|
|
438
|
+
if (existingNode === this.tail) {
|
|
439
|
+
this._tail = newNode;
|
|
440
|
+
}
|
|
441
|
+
this._length++;
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
406
446
|
/**
|
|
407
447
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
408
448
|
* Space Complexity: O(1)
|
|
@@ -472,26 +512,6 @@ class DoublyLinkedList {
|
|
|
472
512
|
}
|
|
473
513
|
return false;
|
|
474
514
|
}
|
|
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
515
|
/**
|
|
496
516
|
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
497
517
|
* @returns A boolean value is being returned.
|
|
@@ -582,6 +602,25 @@ class DoublyLinkedList {
|
|
|
582
602
|
}
|
|
583
603
|
return null;
|
|
584
604
|
}
|
|
605
|
+
/**
|
|
606
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
607
|
+
* Space Complexity: O(1)
|
|
608
|
+
*/
|
|
609
|
+
/**
|
|
610
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
611
|
+
* Space Complexity: O(1)
|
|
612
|
+
*
|
|
613
|
+
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
614
|
+
*/
|
|
615
|
+
reverse() {
|
|
616
|
+
let current = this.head;
|
|
617
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
618
|
+
while (current) {
|
|
619
|
+
const next = current.next;
|
|
620
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
621
|
+
current = next;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
585
624
|
/**
|
|
586
625
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
587
626
|
* Space Complexity: O(n)
|
|
@@ -590,35 +629,46 @@ class DoublyLinkedList {
|
|
|
590
629
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
591
630
|
* Space Complexity: O(n)
|
|
592
631
|
*
|
|
593
|
-
* The `
|
|
594
|
-
* @returns The `
|
|
632
|
+
* The `toArray` function converts a linked list into an array.
|
|
633
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
595
634
|
*/
|
|
596
|
-
|
|
635
|
+
toArray() {
|
|
597
636
|
const array = [];
|
|
598
|
-
let current = this.
|
|
637
|
+
let current = this.head;
|
|
599
638
|
while (current) {
|
|
600
639
|
array.push(current.value);
|
|
601
|
-
current = current.
|
|
640
|
+
current = current.next;
|
|
602
641
|
}
|
|
603
642
|
return array;
|
|
604
643
|
}
|
|
605
644
|
/**
|
|
606
645
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
607
|
-
* Space Complexity: O(
|
|
646
|
+
* Space Complexity: O(n)
|
|
608
647
|
*/
|
|
609
648
|
/**
|
|
610
649
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
611
|
-
* Space Complexity: O(
|
|
650
|
+
* Space Complexity: O(n)
|
|
612
651
|
*
|
|
613
|
-
* The `
|
|
652
|
+
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
653
|
+
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
614
654
|
*/
|
|
615
|
-
|
|
655
|
+
toReversedArray() {
|
|
656
|
+
const array = [];
|
|
657
|
+
let current = this.tail;
|
|
658
|
+
while (current) {
|
|
659
|
+
array.push(current.value);
|
|
660
|
+
current = current.prev;
|
|
661
|
+
}
|
|
662
|
+
return array;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
666
|
+
*/
|
|
667
|
+
*[Symbol.iterator]() {
|
|
616
668
|
let current = this.head;
|
|
617
|
-
[this._head, this._tail] = [this.tail, this.head];
|
|
618
669
|
while (current) {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
current = next;
|
|
670
|
+
yield current.value;
|
|
671
|
+
current = current.next;
|
|
622
672
|
}
|
|
623
673
|
}
|
|
624
674
|
/**
|
|
@@ -635,11 +685,9 @@ class DoublyLinkedList {
|
|
|
635
685
|
* current node in the linked list.
|
|
636
686
|
*/
|
|
637
687
|
forEach(callback) {
|
|
638
|
-
let current = this.head;
|
|
639
688
|
let index = 0;
|
|
640
|
-
|
|
641
|
-
callback(
|
|
642
|
-
current = current.next;
|
|
689
|
+
for (const el of this) {
|
|
690
|
+
callback(el, index, this);
|
|
643
691
|
index++;
|
|
644
692
|
}
|
|
645
693
|
}
|
|
@@ -651,21 +699,22 @@ class DoublyLinkedList {
|
|
|
651
699
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
652
700
|
* Space Complexity: O(n)
|
|
653
701
|
*
|
|
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.
|
|
702
|
+
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
703
|
+
* elements that satisfy the given callback function.
|
|
704
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
705
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
706
|
+
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
660
707
|
*/
|
|
661
|
-
|
|
662
|
-
const
|
|
663
|
-
let
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
708
|
+
filter(callback) {
|
|
709
|
+
const filteredList = new DoublyLinkedList();
|
|
710
|
+
let index = 0;
|
|
711
|
+
for (const current of this) {
|
|
712
|
+
if (callback(current, index, this)) {
|
|
713
|
+
filteredList.push(current);
|
|
714
|
+
}
|
|
715
|
+
index++;
|
|
667
716
|
}
|
|
668
|
-
return
|
|
717
|
+
return filteredList;
|
|
669
718
|
}
|
|
670
719
|
/**
|
|
671
720
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -675,22 +724,21 @@ class DoublyLinkedList {
|
|
|
675
724
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
676
725
|
* Space Complexity: O(n)
|
|
677
726
|
*
|
|
678
|
-
* The `
|
|
679
|
-
*
|
|
680
|
-
* @param callback - The
|
|
681
|
-
*
|
|
682
|
-
*
|
|
727
|
+
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
728
|
+
* DoublyLinkedList with the transformed values.
|
|
729
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
730
|
+
* the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
731
|
+
* DoublyLinkedList).
|
|
732
|
+
* @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
|
|
683
733
|
*/
|
|
684
|
-
|
|
685
|
-
const
|
|
686
|
-
let
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
}
|
|
691
|
-
current = current.next;
|
|
734
|
+
map(callback) {
|
|
735
|
+
const mappedList = new DoublyLinkedList();
|
|
736
|
+
let index = 0;
|
|
737
|
+
for (const current of this) {
|
|
738
|
+
mappedList.push(callback(current, index, this));
|
|
739
|
+
index++;
|
|
692
740
|
}
|
|
693
|
-
return
|
|
741
|
+
return mappedList;
|
|
694
742
|
}
|
|
695
743
|
/**
|
|
696
744
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -704,69 +752,19 @@ class DoublyLinkedList {
|
|
|
704
752
|
* single value.
|
|
705
753
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
706
754
|
* used to perform a specific operation on each element of the linked list.
|
|
707
|
-
* @param {
|
|
755
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
708
756
|
* point for the reduction operation.
|
|
709
757
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
710
758
|
* elements in the linked list.
|
|
711
759
|
*/
|
|
712
760
|
reduce(callback, initialValue) {
|
|
713
761
|
let accumulator = initialValue;
|
|
714
|
-
let
|
|
715
|
-
|
|
716
|
-
accumulator = callback(accumulator, current
|
|
717
|
-
|
|
762
|
+
let index = 0;
|
|
763
|
+
for (const current of this) {
|
|
764
|
+
accumulator = callback(accumulator, current, index, this);
|
|
765
|
+
index++;
|
|
718
766
|
}
|
|
719
767
|
return accumulator;
|
|
720
768
|
}
|
|
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(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
|
-
}
|
|
770
|
-
}
|
|
771
769
|
}
|
|
772
770
|
exports.DoublyLinkedList = DoublyLinkedList;
|
|
@@ -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,39 @@ 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
414
|
}
|
|
@@ -605,58 +605,42 @@ class SinglyLinkedList {
|
|
|
605
605
|
return count;
|
|
606
606
|
}
|
|
607
607
|
/**
|
|
608
|
-
*
|
|
609
|
-
* Space Complexity: O(1) - Constant space.
|
|
610
|
-
*/
|
|
611
|
-
/**
|
|
612
|
-
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
613
|
-
* Space Complexity: O(1) - Constant space.
|
|
614
|
-
*
|
|
615
|
-
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
616
|
-
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
617
|
-
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
618
|
-
* current node in the linked list.
|
|
608
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
619
609
|
*/
|
|
620
|
-
|
|
610
|
+
*[Symbol.iterator]() {
|
|
621
611
|
let current = this.head;
|
|
622
|
-
let index = 0;
|
|
623
612
|
while (current) {
|
|
624
|
-
|
|
613
|
+
yield current.value;
|
|
625
614
|
current = current.next;
|
|
626
|
-
index++;
|
|
627
615
|
}
|
|
628
616
|
}
|
|
629
617
|
/**
|
|
630
|
-
* Time Complexity: O(n)
|
|
631
|
-
* Space Complexity: O(
|
|
618
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
619
|
+
* Space Complexity: O(1)
|
|
632
620
|
*/
|
|
633
621
|
/**
|
|
634
|
-
* Time Complexity: O(n)
|
|
635
|
-
* Space Complexity: O(
|
|
622
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
623
|
+
* Space Complexity: O(1)
|
|
636
624
|
*
|
|
637
|
-
* The `
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
* SinglyLinkedList).
|
|
642
|
-
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
625
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
626
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
627
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
628
|
+
* current node in the linked list.
|
|
643
629
|
*/
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
current = current.next;
|
|
630
|
+
forEach(callback) {
|
|
631
|
+
let index = 0;
|
|
632
|
+
for (const el of this) {
|
|
633
|
+
callback(el, index, this);
|
|
634
|
+
index++;
|
|
650
635
|
}
|
|
651
|
-
return mappedList;
|
|
652
636
|
}
|
|
653
637
|
/**
|
|
654
|
-
* Time Complexity: O(n)
|
|
655
|
-
* Space Complexity: O(n)
|
|
638
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
639
|
+
* Space Complexity: O(n)
|
|
656
640
|
*/
|
|
657
641
|
/**
|
|
658
|
-
* Time Complexity: O(n)
|
|
659
|
-
* Space Complexity: O(n)
|
|
642
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
643
|
+
* Space Complexity: O(n)
|
|
660
644
|
*
|
|
661
645
|
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
662
646
|
* elements that satisfy the given callback function.
|
|
@@ -666,50 +650,64 @@ class SinglyLinkedList {
|
|
|
666
650
|
*/
|
|
667
651
|
filter(callback) {
|
|
668
652
|
const filteredList = new SinglyLinkedList();
|
|
669
|
-
let
|
|
670
|
-
|
|
671
|
-
if (callback(current
|
|
672
|
-
filteredList.push(current
|
|
653
|
+
let index = 0;
|
|
654
|
+
for (const current of this) {
|
|
655
|
+
if (callback(current, index, this)) {
|
|
656
|
+
filteredList.push(current);
|
|
673
657
|
}
|
|
674
|
-
|
|
658
|
+
index++;
|
|
675
659
|
}
|
|
676
660
|
return filteredList;
|
|
677
661
|
}
|
|
678
662
|
/**
|
|
679
|
-
* Time Complexity: O(n)
|
|
680
|
-
* Space Complexity: O(n)
|
|
663
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
664
|
+
* Space Complexity: O(n)
|
|
681
665
|
*/
|
|
682
666
|
/**
|
|
683
|
-
* Time Complexity: O(n)
|
|
684
|
-
* Space Complexity: O(n)
|
|
667
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
668
|
+
* Space Complexity: O(n)
|
|
669
|
+
*
|
|
670
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
671
|
+
* SinglyLinkedList with the transformed values.
|
|
672
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
673
|
+
* the original SinglyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
|
|
674
|
+
* SinglyLinkedList).
|
|
675
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<T>` that contains the mapped values.
|
|
676
|
+
*/
|
|
677
|
+
map(callback) {
|
|
678
|
+
const mappedList = new SinglyLinkedList();
|
|
679
|
+
let index = 0;
|
|
680
|
+
for (const current of this) {
|
|
681
|
+
mappedList.push(callback(current, index, this));
|
|
682
|
+
index++;
|
|
683
|
+
}
|
|
684
|
+
return mappedList;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
688
|
+
* Space Complexity: O(n)
|
|
689
|
+
*/
|
|
690
|
+
/**
|
|
691
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
692
|
+
* Space Complexity: O(n)
|
|
685
693
|
*
|
|
686
694
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
687
695
|
* single value.
|
|
688
696
|
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
689
697
|
* used to perform a specific operation on each element of the linked list.
|
|
690
|
-
* @param {
|
|
698
|
+
* @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
691
699
|
* point for the reduction operation.
|
|
692
700
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
693
701
|
* elements in the linked list.
|
|
694
702
|
*/
|
|
695
703
|
reduce(callback, initialValue) {
|
|
696
704
|
let accumulator = initialValue;
|
|
697
|
-
let
|
|
698
|
-
|
|
699
|
-
accumulator = callback(accumulator, current
|
|
700
|
-
|
|
705
|
+
let index = 0;
|
|
706
|
+
for (const current of this) {
|
|
707
|
+
accumulator = callback(accumulator, current, index, this);
|
|
708
|
+
index++;
|
|
701
709
|
}
|
|
702
710
|
return accumulator;
|
|
703
711
|
}
|
|
704
|
-
/**
|
|
705
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
706
|
-
*/
|
|
707
|
-
*[Symbol.iterator]() {
|
|
708
|
-
let current = this.head;
|
|
709
|
-
while (current) {
|
|
710
|
-
yield current.value;
|
|
711
|
-
current = current.next;
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
712
|
}
|
|
715
713
|
exports.SinglyLinkedList = SinglyLinkedList;
|