avl-tree-typed 1.47.3 → 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.
Files changed (29) hide show
  1. package/README.md +410 -352
  2. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  3. package/dist/data-structures/hash/hash-map.js +10 -4
  4. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  5. package/dist/data-structures/hash/hash-table.js +50 -5
  6. package/dist/data-structures/heap/heap.d.ts +6 -1
  7. package/dist/data-structures/heap/heap.js +51 -9
  8. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
  9. package/dist/data-structures/linked-list/doubly-linked-list.js +117 -119
  10. package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -36
  11. package/dist/data-structures/linked-list/singly-linked-list.js +58 -60
  12. package/dist/data-structures/queue/deque.d.ts +49 -49
  13. package/dist/data-structures/queue/deque.js +79 -72
  14. package/dist/data-structures/queue/queue.d.ts +45 -0
  15. package/dist/data-structures/queue/queue.js +77 -0
  16. package/dist/data-structures/stack/stack.d.ts +18 -5
  17. package/dist/data-structures/stack/stack.js +56 -7
  18. package/dist/data-structures/trie/trie.d.ts +5 -0
  19. package/dist/data-structures/trie/trie.js +47 -0
  20. package/package.json +2 -2
  21. package/src/data-structures/hash/hash-map.ts +13 -7
  22. package/src/data-structures/hash/hash-table.ts +59 -9
  23. package/src/data-structures/heap/heap.ts +60 -9
  24. package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
  25. package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
  26. package/src/data-structures/queue/deque.ts +84 -77
  27. package/src/data-structures/queue/queue.ts +84 -0
  28. package/src/data-structures/stack/stack.ts +64 -8
  29. 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 `toArrayBackward` function converts a doubly linked list into an array in reverse order.
594
- * @returns The `toArrayBackward()` function returns an array of type `E[]`.
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
- toArrayBackward() {
635
+ toArray() {
597
636
  const array = [];
598
- let current = this.tail;
637
+ let current = this.head;
599
638
  while (current) {
600
639
  array.push(current.value);
601
- current = current.prev;
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(1)
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(1)
650
+ * Space Complexity: O(n)
612
651
  *
613
- * The `reverse` function reverses the order of the elements in a doubly linked list.
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
- reverse() {
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
- const next = current.next;
620
- [current.prev, current.next] = [current.next, current.prev];
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
- while (current) {
641
- callback(current.value, index);
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 `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
655
- * DoublyLinkedList with the transformed values.
656
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
657
- * the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
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
- map(callback) {
662
- const mappedList = new DoublyLinkedList();
663
- let current = this.head;
664
- while (current) {
665
- mappedList.push(callback(current.value));
666
- current = current.next;
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 mappedList;
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 `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
679
- * elements that satisfy the given callback function.
680
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
681
- * It is used to determine whether a value should be included in the filtered list or not.
682
- * @returns The filtered list, which is an instance of the DoublyLinkedList class.
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
- filter(callback) {
685
- const filteredList = new DoublyLinkedList();
686
- let current = this.head;
687
- while (current) {
688
- if (callback(current.value)) {
689
- filteredList.push(current.value);
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 filteredList;
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 {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
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 current = this.head;
715
- while (current) {
716
- accumulator = callback(accumulator, current.value);
717
- current = current.next;
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
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
349
- * Space Complexity: O(1) - Constant space.
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) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
353
- * Space Complexity: O(1) - Constant space.
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) - 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.
379
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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) - 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.
383
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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) - 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.
394
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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) - 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.
398
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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 {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
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<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U;
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
- * Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
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
- forEach(callback) {
610
+ *[Symbol.iterator]() {
621
611
  let current = this.head;
622
- let index = 0;
623
612
  while (current) {
624
- callback(current.value, index);
613
+ yield current.value;
625
614
  current = current.next;
626
- index++;
627
615
  }
628
616
  }
629
617
  /**
630
- * 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.
631
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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) - 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.
635
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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 `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
638
- * SinglyLinkedList with the transformed values.
639
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
640
- * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
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
- map(callback) {
645
- const mappedList = new SinglyLinkedList();
646
- let current = this.head;
647
- while (current) {
648
- mappedList.push(callback(current.value));
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) - 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.
655
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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) - 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.
659
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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 current = this.head;
670
- while (current) {
671
- if (callback(current.value)) {
672
- filteredList.push(current.value);
653
+ let index = 0;
654
+ for (const current of this) {
655
+ if (callback(current, index, this)) {
656
+ filteredList.push(current);
673
657
  }
674
- current = current.next;
658
+ index++;
675
659
  }
676
660
  return filteredList;
677
661
  }
678
662
  /**
679
- * 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.
680
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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) - 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.
684
- * Space Complexity: O(n) - Linear space, as they create new nodes or arrays.
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 {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
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 current = this.head;
698
- while (current) {
699
- accumulator = callback(accumulator, current.value);
700
- current = current.next;
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;