queue-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.
Files changed (28) hide show
  1. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  2. package/dist/data-structures/hash/hash-map.js +10 -4
  3. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  4. package/dist/data-structures/hash/hash-table.js +50 -5
  5. package/dist/data-structures/heap/heap.d.ts +6 -1
  6. package/dist/data-structures/heap/heap.js +51 -9
  7. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +56 -56
  8. package/dist/data-structures/linked-list/doubly-linked-list.js +117 -119
  9. package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -36
  10. package/dist/data-structures/linked-list/singly-linked-list.js +58 -60
  11. package/dist/data-structures/queue/deque.d.ts +49 -49
  12. package/dist/data-structures/queue/deque.js +79 -72
  13. package/dist/data-structures/queue/queue.d.ts +45 -0
  14. package/dist/data-structures/queue/queue.js +77 -0
  15. package/dist/data-structures/stack/stack.d.ts +18 -5
  16. package/dist/data-structures/stack/stack.js +56 -7
  17. package/dist/data-structures/trie/trie.d.ts +5 -0
  18. package/dist/data-structures/trie/trie.js +47 -0
  19. package/package.json +1 -1
  20. package/src/data-structures/hash/hash-map.ts +13 -7
  21. package/src/data-structures/hash/hash-table.ts +59 -9
  22. package/src/data-structures/heap/heap.ts +60 -9
  23. package/src/data-structures/linked-list/doubly-linked-list.ts +129 -129
  24. package/src/data-structures/linked-list/singly-linked-list.ts +65 -65
  25. package/src/data-structures/queue/deque.ts +84 -77
  26. package/src/data-structures/queue/queue.ts +84 -0
  27. package/src/data-structures/stack/stack.ts +64 -8
  28. package/src/data-structures/trie/trie.ts +53 -0
@@ -313,12 +313,14 @@ export class HashMap<K = any, V = any> {
313
313
  * @returns a new HashMap object that contains the key-value pairs from the original HashMap that
314
314
  * satisfy the given predicate function.
315
315
  */
316
- filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V> {
316
+ filter(predicate: (element: [K, V], index: number, map: HashMap<K, V>) => boolean): HashMap<K, V> {
317
317
  const filteredMap = new HashMap<K, V>();
318
+ let index = 0;
318
319
  for (const [key, value] of this) {
319
- if (predicate([key, value], this)) {
320
+ if (predicate([key, value], index, this)) {
320
321
  filteredMap.set(key, value);
321
322
  }
323
+ index++;
322
324
  }
323
325
  return filteredMap;
324
326
  }
@@ -330,11 +332,13 @@ export class HashMap<K = any, V = any> {
330
332
  * `map`.
331
333
  * @returns a new HashMap object with the values mapped according to the provided callback function.
332
334
  */
333
- map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV> {
335
+ map<NV>(callback: (element: [K, V], index: number, map: HashMap<K, V>) => NV): HashMap<K, NV> {
334
336
  const mappedMap = new HashMap<K, NV>();
337
+ let index = 0;
335
338
  for (const [key, value] of this) {
336
- const newValue = callback([key, value], this);
339
+ const newValue = callback([key, value], index, this);
337
340
  mappedMap.set(key, newValue);
341
+ index++;
338
342
  }
339
343
  return mappedMap;
340
344
  }
@@ -351,10 +355,12 @@ export class HashMap<K = any, V = any> {
351
355
  * @returns The `reduce` function is returning the final value of the accumulator after iterating
352
356
  * over all the elements in the HashMap and applying the callback function to each element.
353
357
  */
354
- reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A {
358
+ reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: HashMap<K, V>) => A, initialValue: A): A {
355
359
  let accumulator = initialValue;
356
- for (const element of this) {
357
- accumulator = callback(accumulator, element, this);
360
+ let index = 0;
361
+ for (const entry of this) {
362
+ accumulator = callback(accumulator, entry, index, this);
363
+ index++;
358
364
  }
359
365
  return accumulator;
360
366
  }
@@ -9,18 +9,18 @@
9
9
  export class HashTableNode<K, V> {
10
10
  key: K;
11
11
  value: V;
12
- next: HashTableNode<K, V> | null;
12
+ next: HashTableNode<K, V> | undefined;
13
13
 
14
14
  constructor(key: K, value: V) {
15
15
  this.key = key;
16
16
  this.value = value;
17
- this.next = null;
17
+ this.next = undefined;
18
18
  }
19
19
  }
20
20
 
21
21
  import { HashFunction } from '../../types';
22
22
 
23
- export class HashTable<K, V> {
23
+ export class HashTable<K = any, V = any> {
24
24
  protected static readonly DEFAULT_CAPACITY = 16;
25
25
  protected static readonly LOAD_FACTOR = 0.75;
26
26
 
@@ -28,7 +28,7 @@ export class HashTable<K, V> {
28
28
  this._hashFn = hashFn || this._defaultHashFn;
29
29
  this._capacity = Math.max(capacity, HashTable.DEFAULT_CAPACITY);
30
30
  this._size = 0;
31
- this._buckets = new Array<HashTableNode<K, V> | null>(this._capacity).fill(null);
31
+ this._buckets = new Array<HashTableNode<K, V> | undefined>(this._capacity).fill(undefined);
32
32
  }
33
33
 
34
34
  protected _capacity: number;
@@ -43,9 +43,9 @@ export class HashTable<K, V> {
43
43
  return this._size;
44
44
  }
45
45
 
46
- protected _buckets: Array<HashTableNode<K, V> | null>;
46
+ protected _buckets: Array<HashTableNode<K, V> | undefined>;
47
47
 
48
- get buckets(): Array<HashTableNode<K, V> | null> {
48
+ get buckets(): Array<HashTableNode<K, V> | undefined> {
49
49
  return this._buckets;
50
50
  }
51
51
 
@@ -125,7 +125,7 @@ export class HashTable<K, V> {
125
125
  delete(key: K): void {
126
126
  const index = this._hash(key);
127
127
  let currentNode = this._buckets[index];
128
- let prevNode: HashTableNode<K, V> | null = null;
128
+ let prevNode: HashTableNode<K, V> | undefined = undefined;
129
129
 
130
130
  while (currentNode) {
131
131
  if (currentNode.key === key) {
@@ -135,7 +135,7 @@ export class HashTable<K, V> {
135
135
  this._buckets[index] = currentNode.next;
136
136
  }
137
137
  this._size--;
138
- currentNode.next = null; // Release memory
138
+ currentNode.next = undefined; // Release memory
139
139
  return;
140
140
  }
141
141
  prevNode = currentNode;
@@ -143,6 +143,56 @@ export class HashTable<K, V> {
143
143
  }
144
144
  }
145
145
 
146
+ * [Symbol.iterator](): Generator<[K, V], void, undefined> {
147
+ for (const bucket of this._buckets) {
148
+ let currentNode = bucket;
149
+ while (currentNode) {
150
+ yield [currentNode.key, currentNode.value];
151
+ currentNode = currentNode.next;
152
+ }
153
+ }
154
+ }
155
+
156
+ forEach(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => void): void {
157
+ let index = 0;
158
+ for (const entry of this) {
159
+ callback(entry, index, this);
160
+ index++;
161
+ }
162
+ }
163
+
164
+ filter(predicate: (entry: [K, V], index: number, table: HashTable<K, V>) => boolean): HashTable<K, V> {
165
+ const newTable = new HashTable<K, V>();
166
+ let index = 0;
167
+ for (const [key, value] of this) {
168
+ if (predicate([key, value], index, this)) {
169
+ newTable.set(key, value);
170
+ }
171
+ index++;
172
+ }
173
+ return newTable;
174
+ }
175
+
176
+ map<T>(callback: (entry: [K, V], index: number, table: HashTable<K, V>) => T): HashTable<K, T> {
177
+ const newTable = new HashTable<K, T>();
178
+ let index = 0;
179
+ for (const [key, value] of this) {
180
+ newTable.set(key, callback([key, value], index, this));
181
+ index++;
182
+ }
183
+ return newTable;
184
+ }
185
+
186
+ reduce<T>(callback: (accumulator: T, entry: [K, V], index: number, table: HashTable<K, V>) => T, initialValue: T): T {
187
+ let accumulator = initialValue;
188
+ let index = 0;
189
+ for (const entry of this) {
190
+ accumulator = callback(accumulator, entry, index, this);
191
+ index++;
192
+ }
193
+ return accumulator;
194
+ }
195
+
146
196
  /**
147
197
  * The function `_defaultHashFn` calculates the hash value of a given key and returns the remainder when divided by the
148
198
  * capacity of the data structure.
@@ -241,7 +291,7 @@ export class HashTable<K, V> {
241
291
  */
242
292
  protected _expand(): void {
243
293
  const newCapacity = this._capacity * 2;
244
- const newBuckets = new Array<HashTableNode<K, V> | null>(newCapacity).fill(null);
294
+ const newBuckets = new Array<HashTableNode<K, V> | undefined>(newCapacity).fill(undefined);
245
295
 
246
296
  for (const bucket of this._buckets) {
247
297
  let currentNode = bucket;
@@ -226,29 +226,30 @@ export class Heap<E = any> {
226
226
  * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
227
227
  * @returns An array containing elements traversed in the specified order.
228
228
  */
229
- dfs(order: DFSOrderPattern): E[] {
229
+ dfs(order: DFSOrderPattern = 'pre'): E[] {
230
230
  const result: E[] = [];
231
231
 
232
232
  // Auxiliary recursive function, traverses the binary heap according to the traversal order
233
- const dfsHelper = (index: number) => {
233
+ const _dfs = (index: number) => {
234
+ const left = 2 * index + 1, right = left + 1;
234
235
  if (index < this.size) {
235
236
  if (order === 'in') {
236
- dfsHelper(2 * index + 1);
237
+ _dfs(left);
237
238
  result.push(this.elements[index]);
238
- dfsHelper(2 * index + 2);
239
+ _dfs(right);
239
240
  } else if (order === 'pre') {
240
241
  result.push(this.elements[index]);
241
- dfsHelper(2 * index + 1);
242
- dfsHelper(2 * index + 2);
242
+ _dfs(left);
243
+ _dfs(right);
243
244
  } else if (order === 'post') {
244
- dfsHelper(2 * index + 1);
245
- dfsHelper(2 * index + 2);
245
+ _dfs(left);
246
+ _dfs(right);
246
247
  result.push(this.elements[index]);
247
248
  }
248
249
  }
249
250
  };
250
251
 
251
- dfsHelper(0); // Traverse starting from the root node
252
+ _dfs(0); // Traverse starting from the root node
252
253
 
253
254
  return result;
254
255
  }
@@ -324,6 +325,56 @@ export class Heap<E = any> {
324
325
  for (let i = Math.floor(this.size / 2); i >= 0; i--) this._sinkDown(i, this.elements.length >> 1);
325
326
  }
326
327
 
328
+ * [Symbol.iterator]() {
329
+ for (const element of this.elements) {
330
+ yield element;
331
+ }
332
+ }
333
+
334
+ forEach(callback: (element: E, index: number, heap: this) => void): void {
335
+ let index = 0;
336
+ for (const el of this) {
337
+ callback(el, index, this);
338
+ index++;
339
+ }
340
+ }
341
+
342
+ filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E> {
343
+ const filteredHeap: Heap<E> = new Heap<E>({ comparator: this.comparator });
344
+ let index = 0;
345
+ for (const el of this) {
346
+ if (predicate(el, index, this)) {
347
+ filteredHeap.push(el);
348
+ }
349
+ index++;
350
+ }
351
+ return filteredHeap;
352
+ }
353
+
354
+ map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T> {
355
+
356
+ const mappedHeap: Heap<T> = new Heap<T>({ comparator: comparator });
357
+ let index = 0;
358
+ for (const el of this) {
359
+ mappedHeap.add(callback(el, index, this));
360
+ index++;
361
+ }
362
+ return mappedHeap;
363
+ }
364
+
365
+ reduce<T>(
366
+ callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T,
367
+ initialValue: T
368
+ ): T {
369
+ let accumulator: T = initialValue;
370
+ let index = 0;
371
+ for (const el of this) {
372
+ accumulator = callback(accumulator, el, index, this);
373
+ index++;
374
+ }
375
+ return accumulator;
376
+ }
377
+
327
378
  /**
328
379
  * Time Complexity: O(log n)
329
380
  * Space Complexity: O(1)
@@ -441,6 +441,50 @@ export class DoublyLinkedList<E = any> {
441
441
  return false;
442
442
  }
443
443
 
444
+ /**
445
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
446
+ * Space Complexity: O(1)
447
+ */
448
+
449
+ /**
450
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
451
+ * Space Complexity: O(1)
452
+ *
453
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
454
+ * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
455
+ * after which the new value will be inserted. It can be either the value of the existing node or the existing node
456
+ * itself.
457
+ * @param {E} newValue - The value that you want to insert into the doubly linked list.
458
+ * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
459
+ * existing value or node is not found in the doubly linked list.
460
+ */
461
+ insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
462
+ let existingNode;
463
+
464
+ if (existingValueOrNode instanceof DoublyLinkedListNode) {
465
+ existingNode = existingValueOrNode;
466
+ } else {
467
+ existingNode = this.getNode(existingValueOrNode);
468
+ }
469
+
470
+ if (existingNode) {
471
+ const newNode = new DoublyLinkedListNode(newValue);
472
+ newNode.next = existingNode.next;
473
+ if (existingNode.next) {
474
+ existingNode.next.prev = newNode;
475
+ }
476
+ newNode.prev = existingNode;
477
+ existingNode.next = newNode;
478
+ if (existingNode === this.tail) {
479
+ this._tail = newNode;
480
+ }
481
+ this._length++;
482
+ return true;
483
+ }
484
+
485
+ return false;
486
+ }
487
+
444
488
  /**
445
489
  * Time Complexity: O(n), where n is the number of elements in the linked list.
446
490
  * Space Complexity: O(1)
@@ -511,28 +555,6 @@ export class DoublyLinkedList<E = any> {
511
555
  return false;
512
556
  }
513
557
 
514
- /**
515
- * Time Complexity: O(n), where n is the number of elements in the linked list.
516
- * Space Complexity: O(n)
517
- */
518
-
519
- /**
520
- * Time Complexity: O(n), where n is the number of elements in the linked list.
521
- * Space Complexity: O(n)
522
- *
523
- * The `toArray` function converts a linked list into an array.
524
- * @returns The `toArray()` method is returning an array of type `E[]`.
525
- */
526
- toArray(): E[] {
527
- const array: E[] = [];
528
- let current = this.head;
529
- while (current) {
530
- array.push(current.value);
531
- current = current.next;
532
- }
533
- return array;
534
- }
535
-
536
558
  /**
537
559
  * The function checks if a variable has a length greater than zero and returns a boolean value.
538
560
  * @returns A boolean value is being returned.
@@ -631,28 +653,6 @@ export class DoublyLinkedList<E = any> {
631
653
  return null;
632
654
  }
633
655
 
634
- /**
635
- * Time Complexity: O(n), where n is the number of elements in the linked list.
636
- * Space Complexity: O(n)
637
- */
638
-
639
- /**
640
- * Time Complexity: O(n), where n is the number of elements in the linked list.
641
- * Space Complexity: O(n)
642
- *
643
- * The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
644
- * @returns The `toArrayBackward()` function returns an array of type `E[]`.
645
- */
646
- toArrayBackward(): E[] {
647
- const array: E[] = [];
648
- let current = this.tail;
649
- while (current) {
650
- array.push(current.value);
651
- current = current.prev;
652
- }
653
- return array;
654
- }
655
-
656
656
  /**
657
657
  * Time Complexity: O(n), where n is the number of elements in the linked list.
658
658
  * Space Complexity: O(1)
@@ -676,26 +676,24 @@ export class DoublyLinkedList<E = any> {
676
676
 
677
677
  /**
678
678
  * Time Complexity: O(n), where n is the number of elements in the linked list.
679
- * Space Complexity: O(1)
679
+ * Space Complexity: O(n)
680
680
  */
681
681
 
682
682
  /**
683
683
  * Time Complexity: O(n), where n is the number of elements in the linked list.
684
- * Space Complexity: O(1)
684
+ * Space Complexity: O(n)
685
685
  *
686
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
687
- * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
688
- * represents the value of the current node in the linked list, and the index argument represents the index of the
689
- * current node in the linked list.
686
+ * The `toArray` function converts a linked list into an array.
687
+ * @returns The `toArray()` method is returning an array of type `E[]`.
690
688
  */
691
- forEach(callback: (value: E, index: number) => void): void {
689
+ toArray(): E[] {
690
+ const array: E[] = [];
692
691
  let current = this.head;
693
- let index = 0;
694
692
  while (current) {
695
- callback(current.value, index);
693
+ array.push(current.value);
696
694
  current = current.next;
697
- index++;
698
695
  }
696
+ return array;
699
697
  }
700
698
 
701
699
  /**
@@ -707,21 +705,51 @@ export class DoublyLinkedList<E = any> {
707
705
  * Time Complexity: O(n), where n is the number of elements in the linked list.
708
706
  * Space Complexity: O(n)
709
707
  *
710
- * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
711
- * DoublyLinkedList with the transformed values.
712
- * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
713
- * the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
714
- * DoublyLinkedList).
715
- * @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
708
+ * The `toReversedArray` function converts a doubly linked list into an array in reverse order.
709
+ * @returns The `toReversedArray()` function returns an array of type `E[]`.
716
710
  */
717
- map<U>(callback: (value: E) => U): DoublyLinkedList<U> {
718
- const mappedList = new DoublyLinkedList<U>();
711
+ toReversedArray(): E[] {
712
+ const array: E[] = [];
713
+ let current = this.tail;
714
+ while (current) {
715
+ array.push(current.value);
716
+ current = current.prev;
717
+ }
718
+ return array;
719
+ }
720
+
721
+ /**
722
+ * The function returns an iterator that iterates over the values of a linked list.
723
+ */
724
+ * [Symbol.iterator]() {
719
725
  let current = this.head;
726
+
720
727
  while (current) {
721
- mappedList.push(callback(current.value));
728
+ yield current.value;
722
729
  current = current.next;
723
730
  }
724
- return mappedList;
731
+ }
732
+
733
+ /**
734
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
735
+ * Space Complexity: O(1)
736
+ */
737
+
738
+ /**
739
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
740
+ * Space Complexity: O(1)
741
+ *
742
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
743
+ * @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
744
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
745
+ * current node in the linked list.
746
+ */
747
+ forEach(callback: (value: E, index: number, list: DoublyLinkedList<E>) => void): void {
748
+ let index = 0;
749
+ for (const el of this) {
750
+ callback(el, index, this);
751
+ index++;
752
+ }
725
753
  }
726
754
 
727
755
  /**
@@ -739,14 +767,14 @@ export class DoublyLinkedList<E = any> {
739
767
  * It is used to determine whether a value should be included in the filtered list or not.
740
768
  * @returns The filtered list, which is an instance of the DoublyLinkedList class.
741
769
  */
742
- filter(callback: (value: E) => boolean): DoublyLinkedList<E> {
770
+ filter(callback: (value: E, index: number, list: DoublyLinkedList<E>) => boolean): DoublyLinkedList<E> {
743
771
  const filteredList = new DoublyLinkedList<E>();
744
- let current = this.head;
745
- while (current) {
746
- if (callback(current.value)) {
747
- filteredList.push(current.value);
772
+ let index = 0;
773
+ for (const current of this) {
774
+ if (callback(current, index, this)) {
775
+ filteredList.push(current);
748
776
  }
749
- current = current.next;
777
+ index++;
750
778
  }
751
779
  return filteredList;
752
780
  }
@@ -760,78 +788,50 @@ export class DoublyLinkedList<E = any> {
760
788
  * Time Complexity: O(n), where n is the number of elements in the linked list.
761
789
  * Space Complexity: O(n)
762
790
  *
763
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
764
- * single value.
765
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
766
- * used to perform a specific operation on each element of the linked list.
767
- * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
768
- * point for the reduction operation.
769
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
770
- * elements in the linked list.
791
+ * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
792
+ * DoublyLinkedList with the transformed values.
793
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
794
+ * the original DoublyLinkedList) and returns a value of type T (the type of values that will be stored in the mapped
795
+ * DoublyLinkedList).
796
+ * @returns The `map` function is returning a new instance of `DoublyLinkedList<T>` that contains the mapped values.
771
797
  */
772
- reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
773
- let accumulator = initialValue;
774
- let current = this.head;
775
- while (current) {
776
- accumulator = callback(accumulator, current.value);
777
- current = current.next;
798
+ map<T>(callback: (value: E, index: number, list: DoublyLinkedList<E>) => T): DoublyLinkedList<T> {
799
+ const mappedList = new DoublyLinkedList<T>();
800
+ let index = 0;
801
+ for (const current of this) {
802
+ mappedList.push(callback(current, index, this));
803
+ index++;
778
804
  }
779
- return accumulator;
805
+
806
+ return mappedList;
780
807
  }
781
808
 
782
809
  /**
783
810
  * Time Complexity: O(n), where n is the number of elements in the linked list.
784
- * Space Complexity: O(1)
811
+ * Space Complexity: O(n)
785
812
  */
786
813
 
787
814
  /**
788
815
  * Time Complexity: O(n), where n is the number of elements in the linked list.
789
- * Space Complexity: O(1)
816
+ * Space Complexity: O(n)
790
817
  *
791
- * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
792
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
793
- * after which the new value will be inserted. It can be either the value of the existing node or the existing node
794
- * itself.
795
- * @param {E} newValue - The value that you want to insert into the doubly linked list.
796
- * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
797
- * existing value or node is not found in the doubly linked list.
818
+ * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
819
+ * single value.
820
+ * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
821
+ * used to perform a specific operation on each element of the linked list.
822
+ * @param {T} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
823
+ * point for the reduction operation.
824
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
825
+ * elements in the linked list.
798
826
  */
799
- insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
800
- let existingNode;
801
-
802
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
803
- existingNode = existingValueOrNode;
804
- } else {
805
- existingNode = this.getNode(existingValueOrNode);
806
- }
807
-
808
- if (existingNode) {
809
- const newNode = new DoublyLinkedListNode(newValue);
810
- newNode.next = existingNode.next;
811
- if (existingNode.next) {
812
- existingNode.next.prev = newNode;
813
- }
814
- newNode.prev = existingNode;
815
- existingNode.next = newNode;
816
- if (existingNode === this.tail) {
817
- this._tail = newNode;
818
- }
819
- this._length++;
820
- return true;
827
+ reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T {
828
+ let accumulator = initialValue;
829
+ let index = 0;
830
+ for (const current of this) {
831
+ accumulator = callback(accumulator, current, index, this);
832
+ index++;
821
833
  }
822
834
 
823
- return false;
824
- }
825
-
826
- /**
827
- * The function returns an iterator that iterates over the values of a linked list.
828
- */
829
- * [Symbol.iterator]() {
830
- let current = this.head;
831
-
832
- while (current) {
833
- yield current.value;
834
- current = current.next;
835
- }
835
+ return accumulator;
836
836
  }
837
837
  }