data-structure-typed 1.39.1 → 1.39.3

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 (77) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +10 -10
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.js +4 -4
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +58 -107
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +65 -27
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/bst.d.ts +22 -22
  9. package/dist/cjs/data-structures/binary-tree/bst.js +12 -12
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +14 -14
  14. package/dist/cjs/data-structures/binary-tree/tree-multiset.js +7 -7
  15. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  16. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
  17. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
  18. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  19. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
  20. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
  21. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  22. package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
  23. package/dist/cjs/data-structures/queue/deque.js +22 -22
  24. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  25. package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
  26. package/dist/cjs/data-structures/queue/queue.js +3 -3
  27. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  28. package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
  29. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  30. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  31. package/dist/cjs/types/helpers.d.ts +1 -1
  32. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +10 -10
  33. package/dist/mjs/data-structures/binary-tree/avl-tree.js +4 -4
  34. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +58 -107
  35. package/dist/mjs/data-structures/binary-tree/binary-tree.js +65 -27
  36. package/dist/mjs/data-structures/binary-tree/bst.d.ts +22 -22
  37. package/dist/mjs/data-structures/binary-tree/bst.js +12 -12
  38. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  39. package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +14 -14
  40. package/dist/mjs/data-structures/binary-tree/tree-multiset.js +7 -7
  41. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
  42. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
  43. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
  44. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
  45. package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
  46. package/dist/mjs/data-structures/queue/deque.js +22 -22
  47. package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
  48. package/dist/mjs/data-structures/queue/queue.js +3 -3
  49. package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
  50. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  51. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
  52. package/dist/mjs/types/helpers.d.ts +1 -1
  53. package/dist/umd/data-structure-typed.min.js +1 -1
  54. package/dist/umd/data-structure-typed.min.js.map +1 -1
  55. package/package.json +5 -5
  56. package/src/data-structures/binary-tree/avl-tree.ts +10 -10
  57. package/src/data-structures/binary-tree/binary-tree.ts +165 -53
  58. package/src/data-structures/binary-tree/bst.ts +29 -31
  59. package/src/data-structures/binary-tree/rb-tree.ts +5 -5
  60. package/src/data-structures/binary-tree/tree-multiset.ts +14 -14
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
  62. package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
  63. package/src/data-structures/queue/deque.ts +22 -22
  64. package/src/data-structures/queue/queue.ts +3 -3
  65. package/src/interfaces/binary-tree.ts +4 -4
  66. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  67. package/src/types/data-structures/binary-tree/bst.ts +2 -2
  68. package/src/types/helpers.ts +1 -1
  69. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +33 -7
  70. package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
  71. package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
  72. package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
  73. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +65 -6
  74. package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
  75. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +57 -13
  76. package/test/unit/data-structures/queue/deque.test.ts +43 -43
  77. package/test/unit/data-structures/queue/queue.test.ts +7 -7
@@ -60,11 +60,11 @@ export declare class DoublyLinkedList<E = any> {
60
60
  */
61
61
  pop(): E | undefined;
62
62
  /**
63
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
63
+ * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
64
64
  * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
65
65
  * list is empty, it returns null.
66
66
  */
67
- pollLast(): E | undefined;
67
+ popLast(): E | undefined;
68
68
  /**
69
69
  * The `shift()` function removes and returns the value of the first node in a doubly linked list.
70
70
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
@@ -72,11 +72,11 @@ export declare class DoublyLinkedList<E = any> {
72
72
  */
73
73
  shift(): E | undefined;
74
74
  /**
75
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
75
+ * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
76
76
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
77
77
  * list.
78
78
  */
79
- pollFirst(): E | undefined;
79
+ popFirst(): E | undefined;
80
80
  /**
81
81
  * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
82
82
  * @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
@@ -90,15 +90,15 @@ export declare class DoublyLinkedList<E = any> {
90
90
  */
91
91
  addFirst(val: E): void;
92
92
  /**
93
- * The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty.
94
- * @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
93
+ * The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
94
+ * @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
95
95
  */
96
- peekFirst(): E | undefined;
96
+ getFirst(): E | undefined;
97
97
  /**
98
- * The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty.
99
- * @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
98
+ * The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
99
+ * @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
100
100
  */
101
- peekLast(): E | undefined;
101
+ getLast(): E | undefined;
102
102
  /**
103
103
  * The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
104
104
  * @param {number} index - The index parameter is a number that represents the position of the element we want to
@@ -123,7 +123,7 @@ export declare class DoublyLinkedList<E = any> {
123
123
  * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
124
124
  * is found in the linked list. If no such node is found, it returns `null`.
125
125
  */
126
- findNode(val: E | null): DoublyLinkedListNode<E> | null;
126
+ getNode(val: E | null): DoublyLinkedListNode<E> | null;
127
127
  /**
128
128
  * The `insert` function inserts a value at a specified index in a doubly linked list.
129
129
  * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
@@ -134,6 +134,17 @@ export declare class DoublyLinkedList<E = any> {
134
134
  * if the index is out of bounds.
135
135
  */
136
136
  insertAt(index: number, val: E): boolean;
137
+ /**
138
+ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
139
+ * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
140
+ * before which the new value will be inserted. It can be either the value of the existing node or the existing node
141
+ * itself.
142
+ * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
143
+ * list.
144
+ * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
145
+ * insertion fails.
146
+ */
147
+ insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
137
148
  /**
138
149
  * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
139
150
  * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
@@ -142,8 +153,14 @@ export declare class DoublyLinkedList<E = any> {
142
153
  * bounds.
143
154
  */
144
155
  deleteAt(index: number): E | undefined;
145
- delete(valOrNode: E): boolean;
146
- delete(valOrNode: DoublyLinkedListNode<E>): boolean;
156
+ /**
157
+ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
158
+ * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
159
+ * a `DoublyLinkedListNode<E>` object.
160
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
161
+ * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
162
+ */
163
+ delete(valOrNode: E | DoublyLinkedListNode<E> | null): boolean;
147
164
  /**
148
165
  * The `toArray` function converts a linked list into an array.
149
166
  * @returns The `toArray()` method is returning an array of type `E[]`.
@@ -175,19 +192,19 @@ export declare class DoublyLinkedList<E = any> {
175
192
  */
176
193
  indexOf(val: E): number;
177
194
  /**
178
- * The `findLast` function iterates through a linked list from the last node to the first node and returns the last
195
+ * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
179
196
  * value that satisfies the given callback function, or null if no value satisfies the callback.
180
197
  * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
181
198
  * function is used to determine whether a given value satisfies a certain condition.
182
- * @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
199
+ * @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
183
200
  * the callback function. If no value satisfies the condition, it returns `null`.
184
201
  */
185
- findLast(callback: (val: E) => boolean): E | null;
202
+ findBackward(callback: (val: E) => boolean): E | null;
186
203
  /**
187
- * The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
188
- * @returns The `toArrayReverse()` function returns an array of type `E[]`.
204
+ * The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
205
+ * @returns The `toArrayBackward()` function returns an array of type `E[]`.
189
206
  */
190
- toArrayReverse(): E[];
207
+ toArrayBackward(): E[];
191
208
  /**
192
209
  * The `reverse` function reverses the order of the elements in a doubly linked list.
193
210
  */
@@ -227,17 +244,18 @@ export declare class DoublyLinkedList<E = any> {
227
244
  * elements in the linked list.
228
245
  */
229
246
  reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
230
- insertAfter(existingValueOrNode: E, newValue: E): boolean;
231
- insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
232
247
  /**
233
- * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
248
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
234
249
  * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
235
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
250
+ * after which the new value will be inserted. It can be either the value of the existing node or the existing node
236
251
  * itself.
237
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
238
- * list.
239
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
240
- * insertion fails.
252
+ * @param {E} newValue - The value that you want to insert into the doubly linked list.
253
+ * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
254
+ * existing value or node is not found in the doubly linked list.
241
255
  */
242
- insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
256
+ insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
257
+ /**
258
+ * The function returns an iterator that iterates over the values of a linked list.
259
+ */
260
+ [Symbol.iterator](): Generator<E, void, unknown>;
243
261
  }
@@ -130,11 +130,11 @@ class DoublyLinkedList {
130
130
  return removedNode.val;
131
131
  }
132
132
  /**
133
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
133
+ * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
134
134
  * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
135
135
  * list is empty, it returns null.
136
136
  */
137
- pollLast() {
137
+ popLast() {
138
138
  return this.pop();
139
139
  }
140
140
  /**
@@ -158,11 +158,11 @@ class DoublyLinkedList {
158
158
  return removedNode.val;
159
159
  }
160
160
  /**
161
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
161
+ * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
162
162
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
163
163
  * list.
164
164
  */
165
- pollFirst() {
165
+ popFirst() {
166
166
  return this.shift();
167
167
  }
168
168
  /**
@@ -192,17 +192,17 @@ class DoublyLinkedList {
192
192
  this.unshift(val);
193
193
  }
194
194
  /**
195
- * The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty.
196
- * @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
195
+ * The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
196
+ * @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
197
197
  */
198
- peekFirst() {
198
+ getFirst() {
199
199
  return this.head?.val;
200
200
  }
201
201
  /**
202
- * The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty.
203
- * @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
202
+ * The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
203
+ * @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
204
204
  */
205
- peekLast() {
205
+ getLast() {
206
206
  return this.tail?.val;
207
207
  }
208
208
  /**
@@ -245,7 +245,7 @@ class DoublyLinkedList {
245
245
  * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
246
246
  * is found in the linked list. If no such node is found, it returns `null`.
247
247
  */
248
- findNode(val) {
248
+ getNode(val) {
249
249
  let current = this.head;
250
250
  while (current) {
251
251
  if (current.val === val) {
@@ -285,6 +285,40 @@ class DoublyLinkedList {
285
285
  this._length++;
286
286
  return true;
287
287
  }
288
+ /**
289
+ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
290
+ * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
291
+ * before which the new value will be inserted. It can be either the value of the existing node or the existing node
292
+ * itself.
293
+ * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
294
+ * list.
295
+ * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
296
+ * insertion fails.
297
+ */
298
+ insertBefore(existingValueOrNode, newValue) {
299
+ let existingNode;
300
+ if (existingValueOrNode instanceof DoublyLinkedListNode) {
301
+ existingNode = existingValueOrNode;
302
+ }
303
+ else {
304
+ existingNode = this.getNode(existingValueOrNode);
305
+ }
306
+ if (existingNode) {
307
+ const newNode = new DoublyLinkedListNode(newValue);
308
+ newNode.prev = existingNode.prev;
309
+ if (existingNode.prev) {
310
+ existingNode.prev.next = newNode;
311
+ }
312
+ newNode.next = existingNode;
313
+ existingNode.prev = newNode;
314
+ if (existingNode === this.head) {
315
+ this.head = newNode;
316
+ }
317
+ this._length++;
318
+ return true;
319
+ }
320
+ return false;
321
+ }
288
322
  /**
289
323
  * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
290
324
  * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
@@ -320,7 +354,7 @@ class DoublyLinkedList {
320
354
  node = valOrNode;
321
355
  }
322
356
  else {
323
- node = this.findNode(valOrNode);
357
+ node = this.getNode(valOrNode);
324
358
  }
325
359
  if (node) {
326
360
  if (node === this.head) {
@@ -405,14 +439,14 @@ class DoublyLinkedList {
405
439
  return -1;
406
440
  }
407
441
  /**
408
- * The `findLast` function iterates through a linked list from the last node to the first node and returns the last
442
+ * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
409
443
  * value that satisfies the given callback function, or null if no value satisfies the callback.
410
444
  * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
411
445
  * function is used to determine whether a given value satisfies a certain condition.
412
- * @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
446
+ * @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
413
447
  * the callback function. If no value satisfies the condition, it returns `null`.
414
448
  */
415
- findLast(callback) {
449
+ findBackward(callback) {
416
450
  let current = this.tail;
417
451
  while (current) {
418
452
  if (callback(current.val)) {
@@ -423,10 +457,10 @@ class DoublyLinkedList {
423
457
  return null;
424
458
  }
425
459
  /**
426
- * The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
427
- * @returns The `toArrayReverse()` function returns an array of type `E[]`.
460
+ * The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
461
+ * @returns The `toArrayBackward()` function returns an array of type `E[]`.
428
462
  */
429
- toArrayReverse() {
463
+ toArrayBackward() {
430
464
  const array = [];
431
465
  let current = this.tail;
432
466
  while (current) {
@@ -531,7 +565,7 @@ class DoublyLinkedList {
531
565
  existingNode = existingValueOrNode;
532
566
  }
533
567
  else {
534
- existingNode = this.findNode(existingValueOrNode);
568
+ existingNode = this.getNode(existingValueOrNode);
535
569
  }
536
570
  if (existingNode) {
537
571
  const newNode = new DoublyLinkedListNode(newValue);
@@ -550,38 +584,14 @@ class DoublyLinkedList {
550
584
  return false;
551
585
  }
552
586
  /**
553
- * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
554
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
555
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
556
- * itself.
557
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
558
- * list.
559
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
560
- * insertion fails.
587
+ * The function returns an iterator that iterates over the values of a linked list.
561
588
  */
562
- insertBefore(existingValueOrNode, newValue) {
563
- let existingNode;
564
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
565
- existingNode = existingValueOrNode;
566
- }
567
- else {
568
- existingNode = this.findNode(existingValueOrNode);
569
- }
570
- if (existingNode) {
571
- const newNode = new DoublyLinkedListNode(newValue);
572
- newNode.prev = existingNode.prev;
573
- if (existingNode.prev) {
574
- existingNode.prev.next = newNode;
575
- }
576
- newNode.next = existingNode;
577
- existingNode.prev = newNode;
578
- if (existingNode === this.head) {
579
- this.head = newNode;
580
- }
581
- this._length++;
582
- return true;
589
+ *[Symbol.iterator]() {
590
+ let current = this.head;
591
+ while (current) {
592
+ yield current.val;
593
+ current = current.next;
583
594
  }
584
- return false;
585
595
  }
586
596
  }
587
597
  exports.DoublyLinkedList = DoublyLinkedList;
@@ -39,13 +39,18 @@ export declare class SinglyLinkedList<E = any> {
39
39
  * @returns The `fromArray` function returns a `SinglyLinkedList` object.
40
40
  */
41
41
  static fromArray<E>(data: E[]): SinglyLinkedList<E>;
42
- getLength(): number;
43
42
  /**
44
- * The `push` function adds a new node with the given data to the end of a singly linked list.
45
- * @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
43
+ * The `push` function adds a new node with the given val to the end of a singly linked list.
44
+ * @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
46
45
  * any type (E) as specified in the generic type declaration of the class or function.
47
46
  */
48
- push(data: E): void;
47
+ push(val: E): void;
48
+ /**
49
+ * The `push` function adds a new node with the given val to the end of a singly linked list.
50
+ * @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
51
+ * any type (E) as specified in the generic type declaration of the class or function.
52
+ */
53
+ addLast(val: E): void;
49
54
  /**
50
55
  * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
51
56
  * pointers accordingly.
@@ -53,17 +58,35 @@ export declare class SinglyLinkedList<E = any> {
53
58
  * the linked list is empty, it returns `null`.
54
59
  */
55
60
  pop(): E | undefined;
61
+ /**
62
+ * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
63
+ * pointers accordingly.
64
+ * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
65
+ * the linked list is empty, it returns `null`.
66
+ */
67
+ popLast(): E | undefined;
56
68
  /**
57
69
  * The `shift()` function removes and returns the value of the first node in a linked list.
58
70
  * @returns The value of the node that is being removed from the beginning of the linked list.
59
71
  */
60
72
  shift(): E | undefined;
73
+ /**
74
+ * The `popFirst()` function removes and returns the value of the first node in a linked list.
75
+ * @returns The value of the node that is being removed from the beginning of the linked list.
76
+ */
77
+ popFirst(): E | undefined;
61
78
  /**
62
79
  * The unshift function adds a new node with the given value to the beginning of a singly linked list.
63
80
  * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
64
81
  * linked list.
65
82
  */
66
83
  unshift(val: E): void;
84
+ /**
85
+ * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
86
+ * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
87
+ * linked list.
88
+ */
89
+ addFirst(val: E): void;
67
90
  /**
68
91
  * The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
69
92
  * @param {number} index - The index parameter is a number that represents the position of the element we want to
@@ -148,7 +171,7 @@ export declare class SinglyLinkedList<E = any> {
148
171
  * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
149
172
  * the specified value is found, the function returns `null`.
150
173
  */
151
- findNode(value: E): SinglyLinkedListNode<E> | null;
174
+ getNode(value: E): SinglyLinkedListNode<E> | null;
152
175
  /**
153
176
  * The `insertBefore` function inserts a new value before an existing value in a singly linked list.
154
177
  * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
@@ -158,13 +181,58 @@ export declare class SinglyLinkedList<E = any> {
158
181
  * inserted before the existing value, and `false` otherwise.
159
182
  */
160
183
  insertBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
161
- insertAfter(existingValueOrNode: E, newValue: E): boolean;
162
- insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
184
+ /**
185
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
186
+ * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
187
+ * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
188
+ * @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
189
+ * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
190
+ * existing value or node, and false if the existing value or node was not found in the linked list.
191
+ */
192
+ insertAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
163
193
  /**
164
194
  * The function counts the number of occurrences of a given value in a linked list.
165
195
  * @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
166
196
  * @returns The count of occurrences of the given value in the linked list.
167
197
  */
168
198
  countOccurrences(value: E): number;
199
+ /**
200
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
201
+ * @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
202
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
203
+ * current node in the linked list.
204
+ */
205
+ forEach(callback: (val: E, index: number) => void): void;
206
+ /**
207
+ * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
208
+ * SinglyLinkedList with the transformed values.
209
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
210
+ * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
211
+ * SinglyLinkedList).
212
+ * @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
213
+ */
214
+ map<U>(callback: (val: E) => U): SinglyLinkedList<U>;
215
+ /**
216
+ * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
217
+ * elements that satisfy the given callback function.
218
+ * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
219
+ * It is used to determine whether a value should be included in the filtered list or not.
220
+ * @returns The filtered list, which is an instance of the SinglyLinkedList class.
221
+ */
222
+ filter(callback: (val: E) => boolean): SinglyLinkedList<E>;
223
+ /**
224
+ * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
225
+ * single value.
226
+ * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
227
+ * used to perform a specific operation on each element of the linked list.
228
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
229
+ * point for the reduction operation.
230
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
231
+ * elements in the linked list.
232
+ */
233
+ reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
234
+ /**
235
+ * The function returns an iterator that iterates over the values of a linked list.
236
+ */
169
237
  [Symbol.iterator](): Generator<E, void, unknown>;
170
238
  }
@@ -74,16 +74,13 @@ class SinglyLinkedList {
74
74
  }
75
75
  return singlyLinkedList;
76
76
  }
77
- getLength() {
78
- return this._length;
79
- }
80
77
  /**
81
- * The `push` function adds a new node with the given data to the end of a singly linked list.
82
- * @param {E} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
78
+ * The `push` function adds a new node with the given val to the end of a singly linked list.
79
+ * @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
83
80
  * any type (E) as specified in the generic type declaration of the class or function.
84
81
  */
85
- push(data) {
86
- const newNode = new SinglyLinkedListNode(data);
82
+ push(val) {
83
+ const newNode = new SinglyLinkedListNode(val);
87
84
  if (!this.head) {
88
85
  this.head = newNode;
89
86
  this.tail = newNode;
@@ -94,6 +91,14 @@ class SinglyLinkedList {
94
91
  }
95
92
  this._length++;
96
93
  }
94
+ /**
95
+ * The `push` function adds a new node with the given val to the end of a singly linked list.
96
+ * @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
97
+ * any type (E) as specified in the generic type declaration of the class or function.
98
+ */
99
+ addLast(val) {
100
+ this.push(val);
101
+ }
97
102
  /**
98
103
  * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
99
104
  * pointers accordingly.
@@ -120,6 +125,15 @@ class SinglyLinkedList {
120
125
  this._length--;
121
126
  return val;
122
127
  }
128
+ /**
129
+ * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
130
+ * pointers accordingly.
131
+ * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
132
+ * the linked list is empty, it returns `null`.
133
+ */
134
+ popLast() {
135
+ return this.pop();
136
+ }
123
137
  /**
124
138
  * The `shift()` function removes and returns the value of the first node in a linked list.
125
139
  * @returns The value of the node that is being removed from the beginning of the linked list.
@@ -132,6 +146,13 @@ class SinglyLinkedList {
132
146
  this._length--;
133
147
  return removedNode.val;
134
148
  }
149
+ /**
150
+ * The `popFirst()` function removes and returns the value of the first node in a linked list.
151
+ * @returns The value of the node that is being removed from the beginning of the linked list.
152
+ */
153
+ popFirst() {
154
+ return this.shift();
155
+ }
135
156
  /**
136
157
  * The unshift function adds a new node with the given value to the beginning of a singly linked list.
137
158
  * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
@@ -149,6 +170,14 @@ class SinglyLinkedList {
149
170
  }
150
171
  this._length++;
151
172
  }
173
+ /**
174
+ * The addFirst function adds a new node with the given value to the beginning of a singly linked list.
175
+ * @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
176
+ * linked list.
177
+ */
178
+ addFirst(val) {
179
+ this.unshift(val);
180
+ }
152
181
  /**
153
182
  * The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
154
183
  * @param {number} index - The index parameter is a number that represents the position of the element we want to
@@ -355,7 +384,7 @@ class SinglyLinkedList {
355
384
  * @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
356
385
  * the specified value is found, the function returns `null`.
357
386
  */
358
- findNode(value) {
387
+ getNode(value) {
359
388
  let current = this.head;
360
389
  while (current) {
361
390
  if (current.val === value) {
@@ -414,7 +443,7 @@ class SinglyLinkedList {
414
443
  existingNode = existingValueOrNode;
415
444
  }
416
445
  else {
417
- existingNode = this.findNode(existingValueOrNode);
446
+ existingNode = this.getNode(existingValueOrNode);
418
447
  }
419
448
  if (existingNode) {
420
449
  const newNode = new SinglyLinkedListNode(newValue);
@@ -444,6 +473,78 @@ class SinglyLinkedList {
444
473
  }
445
474
  return count;
446
475
  }
476
+ /**
477
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
478
+ * @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
479
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
480
+ * current node in the linked list.
481
+ */
482
+ forEach(callback) {
483
+ let current = this.head;
484
+ let index = 0;
485
+ while (current) {
486
+ callback(current.val, index);
487
+ current = current.next;
488
+ index++;
489
+ }
490
+ }
491
+ /**
492
+ * The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
493
+ * SinglyLinkedList with the transformed values.
494
+ * @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
495
+ * the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
496
+ * SinglyLinkedList).
497
+ * @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
498
+ */
499
+ map(callback) {
500
+ const mappedList = new SinglyLinkedList();
501
+ let current = this.head;
502
+ while (current) {
503
+ mappedList.push(callback(current.val));
504
+ current = current.next;
505
+ }
506
+ return mappedList;
507
+ }
508
+ /**
509
+ * The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
510
+ * elements that satisfy the given callback function.
511
+ * @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
512
+ * It is used to determine whether a value should be included in the filtered list or not.
513
+ * @returns The filtered list, which is an instance of the SinglyLinkedList class.
514
+ */
515
+ filter(callback) {
516
+ const filteredList = new SinglyLinkedList();
517
+ let current = this.head;
518
+ while (current) {
519
+ if (callback(current.val)) {
520
+ filteredList.push(current.val);
521
+ }
522
+ current = current.next;
523
+ }
524
+ return filteredList;
525
+ }
526
+ /**
527
+ * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
528
+ * single value.
529
+ * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
530
+ * used to perform a specific operation on each element of the linked list.
531
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
532
+ * point for the reduction operation.
533
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
534
+ * elements in the linked list.
535
+ */
536
+ reduce(callback, initialValue) {
537
+ let accumulator = initialValue;
538
+ let current = this.head;
539
+ while (current) {
540
+ accumulator = callback(accumulator, current.val);
541
+ current = current.next;
542
+ }
543
+ return accumulator;
544
+ }
545
+ /**
546
+ * The function returns an iterator that iterates over the values of a linked list.
547
+ */
447
548
  *[Symbol.iterator]() {
448
549
  let current = this.head;
449
550
  while (current) {