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.
Files changed (72) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
  2. package/dist/data-structures/binary-tree/avl-tree.js +23 -15
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
  4. package/dist/data-structures/binary-tree/binary-tree.js +66 -82
  5. package/dist/data-structures/binary-tree/bst.d.ts +38 -37
  6. package/dist/data-structures/binary-tree/bst.js +56 -40
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
  8. package/dist/data-structures/binary-tree/rb-tree.js +26 -17
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  10. package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  13. package/dist/data-structures/hash/hash-map.js +10 -4
  14. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  15. package/dist/data-structures/hash/hash-table.js +50 -5
  16. package/dist/data-structures/heap/heap.d.ts +25 -22
  17. package/dist/data-structures/heap/heap.js +101 -41
  18. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/max-heap.js +2 -2
  20. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  21. package/dist/data-structures/heap/min-heap.js +2 -2
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  25. package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
  26. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  30. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  31. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  32. package/dist/data-structures/queue/deque.d.ts +50 -49
  33. package/dist/data-structures/queue/deque.js +81 -71
  34. package/dist/data-structures/queue/queue.d.ts +46 -0
  35. package/dist/data-structures/queue/queue.js +80 -0
  36. package/dist/data-structures/stack/stack.d.ts +20 -6
  37. package/dist/data-structures/stack/stack.js +65 -8
  38. package/dist/data-structures/trie/trie.d.ts +5 -0
  39. package/dist/data-structures/trie/trie.js +47 -0
  40. package/dist/interfaces/binary-tree.d.ts +3 -1
  41. package/dist/types/common.d.ts +2 -0
  42. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  44. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  45. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  46. package/package.json +2 -2
  47. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  48. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  49. package/src/data-structures/binary-tree/bst.ts +67 -47
  50. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  51. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  52. package/src/data-structures/graph/abstract-graph.ts +1 -1
  53. package/src/data-structures/hash/hash-map.ts +13 -7
  54. package/src/data-structures/hash/hash-table.ts +59 -9
  55. package/src/data-structures/heap/heap.ts +115 -46
  56. package/src/data-structures/heap/max-heap.ts +5 -5
  57. package/src/data-structures/heap/min-heap.ts +5 -5
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  59. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  60. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  61. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  62. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  63. package/src/data-structures/queue/deque.ts +86 -75
  64. package/src/data-structures/queue/queue.ts +88 -0
  65. package/src/data-structures/stack/stack.ts +75 -10
  66. package/src/data-structures/trie/trie.ts +53 -0
  67. package/src/interfaces/binary-tree.ts +13 -1
  68. package/src/types/common.ts +5 -1
  69. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  70. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  71. package/src/types/data-structures/heap/heap.ts +3 -1
  72. 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 `toArrayBackward` function converts a doubly linked list into an array in reverse order.
358
- * @returns The `toArrayBackward()` function returns an array of type `E[]`.
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
- toArrayBackward(): E[];
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(1)
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(1)
383
+ * Space Complexity: O(n)
368
384
  *
369
- * The `reverse` function reverses the order of the elements in a doubly linked list.
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
- reverse(): void;
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 `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
395
- * DoublyLinkedList with the transformed values.
396
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
397
- * the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
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
- map<U>(callback: (value: E) => U): DoublyLinkedList<U>;
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 `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
411
- * elements that satisfy the given callback function.
412
- * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
413
- * It is used to determine whether a value should be included in the filtered list or not.
414
- * @returns The filtered list, which is an instance of the DoublyLinkedList class.
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
- filter(callback: (value: E) => boolean): DoublyLinkedList<E>;
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 {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
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<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U;
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 `toArrayBackward` function converts a doubly linked list into an array in reverse order.
594
- * @returns The `toArrayBackward()` function returns an array of type `E[]`.
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
- toArrayBackward() {
640
+ toArray() {
597
641
  const array = [];
598
- let current = this.tail;
642
+ let current = this.head;
599
643
  while (current) {
600
644
  array.push(current.value);
601
- current = current.prev;
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(1)
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(1)
655
+ * Space Complexity: O(n)
612
656
  *
613
- * The `reverse` function reverses the order of the elements in a doubly linked list.
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
- reverse() {
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
- const next = current.next;
620
- [current.prev, current.next] = [current.next, current.prev];
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
- while (current) {
641
- callback(current.value, index);
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 `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.
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
- 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;
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 mappedList;
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 `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.
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
- 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;
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 filteredList;
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 {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
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 current = this.head;
715
- while (current) {
716
- accumulator = callback(accumulator, current.value);
717
- current = current.next;
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
- * 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
- }
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
- * 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,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) - 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
+ print(): void;
414
415
  }