data-structure-typed 1.12.21 → 1.15.1

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 (48) hide show
  1. package/README.md +278 -179
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
  3. package/dist/data-structures/binary-tree/binary-tree.js +67 -0
  4. package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
  5. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  6. package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
  7. package/dist/data-structures/graph/abstract-graph.js +47 -7
  8. package/dist/data-structures/graph/directed-graph.d.ts +8 -0
  9. package/dist/data-structures/graph/directed-graph.js +14 -2
  10. package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
  11. package/dist/data-structures/graph/undirected-graph.js +23 -1
  12. package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
  13. package/dist/data-structures/hash/coordinate-map.js +16 -0
  14. package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
  15. package/dist/data-structures/hash/coordinate-set.js +16 -0
  16. package/dist/data-structures/heap/heap.d.ts +16 -0
  17. package/dist/data-structures/heap/heap.js +38 -0
  18. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
  20. package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
  21. package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
  22. package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
  23. package/dist/data-structures/priority-queue/priority-queue.js +18 -2
  24. package/dist/data-structures/queue/deque.d.ts +18 -7
  25. package/dist/data-structures/queue/deque.js +50 -3
  26. package/dist/data-structures/types/abstract-graph.d.ts +2 -2
  27. package/dist/utils/types/utils.d.ts +0 -49
  28. package/dist/utils/types/utils.js +14 -52
  29. package/dist/utils/utils.d.ts +1 -97
  30. package/dist/utils/utils.js +197 -546
  31. package/package.json +4 -3
  32. package/src/data-structures/binary-tree/aa-tree.ts +1 -1
  33. package/src/data-structures/binary-tree/binary-tree.ts +84 -14
  34. package/src/data-structures/binary-tree/segment-tree.ts +45 -13
  35. package/src/data-structures/graph/abstract-graph.ts +58 -15
  36. package/src/data-structures/graph/directed-graph.ts +14 -5
  37. package/src/data-structures/graph/undirected-graph.ts +23 -6
  38. package/src/data-structures/hash/coordinate-map.ts +13 -1
  39. package/src/data-structures/hash/coordinate-set.ts +13 -1
  40. package/src/data-structures/heap/heap.ts +31 -0
  41. package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
  42. package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
  43. package/src/data-structures/priority-queue/priority-queue.ts +15 -2
  44. package/src/data-structures/queue/deque.ts +38 -8
  45. package/src/data-structures/types/abstract-graph.ts +3 -3
  46. package/src/utils/types/utils.ts +165 -167
  47. package/src/utils/utils.ts +209 -480
  48. package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
@@ -5,356 +5,290 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- /**
9
- * The class which represents one link or node in a linked list
10
- * ```ts
11
- * const node = new SinglyLinkedListNode(1, null, null, null);
12
- * ```
13
- */
14
- export declare class SinglyLinkedListNode<NodeData = any> {
15
- /** Data stored on the node */
16
- val: NodeData;
17
- /** The previous node in the list */
18
- prev: SinglyLinkedListNode<NodeData> | null;
19
- /** The next link in the list */
20
- next: SinglyLinkedListNode<NodeData> | null;
21
- /** The list this node belongs to */
22
- list: SinglyLinkedList<NodeData> | null;
23
- constructor(
24
- /** Data stored on the node */
25
- val: NodeData,
26
- /** The previous node in the list */
27
- prev: SinglyLinkedListNode<NodeData> | null,
28
- /** The next link in the list */
29
- next: SinglyLinkedListNode<NodeData> | null,
30
- /** The list this node belongs to */
31
- list: SinglyLinkedList<NodeData> | null);
32
- /**
33
- * Alias to .val
34
- * ```ts
35
- * new LinkedList(1, 2, 3).head.value; // 1
36
- * ```
37
- */
38
- get value(): NodeData;
39
- /**
40
- * Get the index of this node
41
- * ```ts
42
- * new LinkedList(1, 2, 3).head.index; // 0
43
- * ```
44
- */
8
+ export declare class SinglyLinkedListNode<NodeVal = any> {
9
+ protected _val: NodeVal;
10
+ get val(): NodeVal;
11
+ set val(value: NodeVal);
12
+ protected _prev: SinglyLinkedListNode<NodeVal> | null;
13
+ get prev(): SinglyLinkedListNode<NodeVal> | null;
14
+ set prev(value: SinglyLinkedListNode<NodeVal> | null);
15
+ protected _next: SinglyLinkedListNode<NodeVal> | null;
16
+ get next(): SinglyLinkedListNode<NodeVal> | null;
17
+ set next(value: SinglyLinkedListNode<NodeVal> | null);
18
+ protected _list: SinglyLinkedList<NodeVal> | null;
19
+ get list(): SinglyLinkedList<NodeVal> | null;
20
+ set list(value: SinglyLinkedList<NodeVal> | null);
21
+ constructor(val: NodeVal, prev?: SinglyLinkedListNode<NodeVal> | null, next?: SinglyLinkedListNode<NodeVal> | null, list?: SinglyLinkedList<NodeVal> | null);
45
22
  get index(): number | undefined;
46
23
  /**
47
- * Insert a new node before this one
48
- * ```ts
49
- * new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3
50
- * ```
51
- * @param val Data to save in the node
24
+ * The `insertBefore` function inserts a new node with the given value before the current node in a singly linked list.
25
+ * @param {NodeVal} val - The parameter "val" is of type "NodeVal". It represents the value of the node that you want
26
+ * to insert before the current node.
27
+ * @returns The method is returning a SinglyLinkedList<NodeVal>.
52
28
  */
53
- insertBefore(val: NodeData): SinglyLinkedList<NodeData>;
29
+ insertBefore(val: NodeVal): SinglyLinkedList<NodeVal>;
54
30
  /**
55
- * Insert new val after this node
56
- * ```ts
57
- * new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3
58
- * ```
59
- * @param val Data to be saved in the node
31
+ * The function inserts a new node with the given value after the current node in a singly linked list.
32
+ * @param {NodeVal} val - The parameter `val` is the value of the node that you want to insert after the current node.
33
+ * @returns The method is returning a SinglyLinkedList<NodeVal>.
60
34
  */
61
- insertAfter(val: NodeData): SinglyLinkedList<NodeData>;
35
+ insertAfter(val: NodeVal): SinglyLinkedList<NodeVal>;
62
36
  /**
63
- * Remove this node
64
- * ```ts
65
- * new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
66
- * ```
37
+ * The `remove()` function removes a node from a singly linked list.
38
+ * @returns The remove() method is returning a SinglyLinkedListNode<NodeVal> object.
67
39
  */
68
- remove(): SinglyLinkedListNode<NodeData>;
40
+ remove(): SinglyLinkedListNode<NodeVal>;
69
41
  }
70
- /**
71
- * A doubly linked list
72
- * ```ts
73
- * const list = new LinkedList(1, 2, 3);
74
- * const listFromArray = LinkedList.from([1, 2, 3]);
75
- * ```
76
- */
77
- export declare class SinglyLinkedList<NodeData = any> {
78
- /** The head of the list, the first node */
79
- head: SinglyLinkedListNode<NodeData> | null;
80
- /** The tail of the list, the last node */
81
- tail: SinglyLinkedListNode<NodeData> | null;
82
- /** Internal size reference */
83
- private size;
84
- constructor(...args: NodeData[]);
85
- /**
86
- * The length of the list
87
- */
88
- get length(): number;
89
- /**
90
- * Convert any iterable to a new linked list
91
- * ```javascript
92
- * const array = [1, 2, 3];
93
- * const list = LinkedList.from(array);
94
- * ```
95
- * @param iterable Any iterable datatype like Array or Map
42
+ export declare class SinglyLinkedList<NodeVal = any> {
43
+ protected _head: SinglyLinkedListNode<NodeVal> | null;
44
+ get head(): SinglyLinkedListNode<NodeVal> | null;
45
+ set head(value: SinglyLinkedListNode<NodeVal> | null);
46
+ protected _tail: SinglyLinkedListNode<NodeVal> | null;
47
+ get tail(): SinglyLinkedListNode<NodeVal> | null;
48
+ set tail(value: SinglyLinkedListNode<NodeVal> | null);
49
+ protected _size: number;
50
+ get size(): number;
51
+ set size(value: number);
52
+ /**
53
+ * The constructor initializes a linked list with the given arguments as nodes.
54
+ * @param {NodeVal[]} args - args is a rest parameter that allows the constructor to accept an arbitrary number of
55
+ * arguments of type NodeVal.
56
+ */
57
+ constructor(...args: NodeVal[]);
58
+ /**
59
+ * The `from` function in TypeScript creates a new SinglyLinkedList instance from an iterable object.
60
+ * @param iterable - The `iterable` parameter is an object that can be iterated over, such as an array or a string. It
61
+ * contains a collection of elements of type `T`.
62
+ * @returns The method is returning a new instance of the SinglyLinkedList class.
96
63
  */
97
64
  static from<T>(iterable: Iterable<T>): SinglyLinkedList<T>;
98
65
  /**
99
- * Get the node val at a specified index, zero based
100
- * ```ts
101
- * new LinkedList(1, 2, 3).get(0); // 1
102
- * ```
103
- * @param index to retrieve val at
66
+ * The `get` function returns the value of a node at a given index in a data structure.
67
+ * @param {number} index - The index parameter is a number that represents the position of the node in the data
68
+ * structure.
69
+ * @returns The method is returning the value of the node at the specified index if the node exists, otherwise it
70
+ * returns undefined.
71
+ */
72
+ get(index: number): NodeVal | undefined;
73
+ /**
74
+ * The function `getNode` returns the node at a given index in a singly linked list.
75
+ * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
76
+ * retrieve from the linked list.
77
+ * @returns a SinglyLinkedListNode<NodeVal> object or undefined.
78
+ */
79
+ getNode(index: number): SinglyLinkedListNode<NodeVal> | undefined;
80
+ /**
81
+ * The function `findNodeIndex` searches for a node in a singly linked list that satisfies a given condition and
82
+ * returns its index and the node itself.
83
+ * @param callbackFn - The callbackFn parameter is a function that takes three arguments: data, index, and list. It is
84
+ * used to determine whether a node in the singly linked list matches a certain condition. The function should return a
85
+ * boolean value indicating whether the condition is met for the given node.
86
+ * @returns The function `findNodeIndex` returns an object with two properties: `node` and `index`. The `node` property
87
+ * contains the node that matches the condition specified in the `callbackFn` function, and the `index` property
88
+ * contains the index of that node in the linked list. If no node matches the condition, the function returns
89
+ * `undefined`.
90
+ */
91
+ findNodeIndex(callbackFn: (data: NodeVal, index: number, list: SinglyLinkedList<NodeVal>) => boolean): ({
92
+ node: SinglyLinkedListNode<NodeVal>;
93
+ index: number;
94
+ }) | undefined;
95
+ /**
96
+ * The findNode function searches for a node in a singly linked list based on a given callback function.
97
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
98
+ * value indicating whether the current node matches the desired criteria.
99
+ * @returns The function `findNode` returns a `SinglyLinkedListNode<NodeVal>` if a node satisfying the condition
100
+ * specified by the `callbackFn` is found in the linked list. If no such node is found, it returns `undefined`.
104
101
  */
105
- get(index: number): NodeData | undefined;
102
+ findNode(callbackFn: (data: NodeVal, index: number, list: SinglyLinkedList<NodeVal>) => boolean): SinglyLinkedListNode<NodeVal> | undefined;
106
103
  /**
107
- * Get the node at index, zero based
108
- * ```ts
109
- * new LinkedList(1, 2, 3).getNode(0);
110
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
111
- * ```
104
+ * The `find` function in TypeScript searches for a node in a singly linked list based on a given callback function and
105
+ * returns the value of the found node.
106
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
107
+ * value indicating whether the condition is met for a particular node in the linked list.
108
+ * @returns The method `find` returns the `NodeVal` value of the first node in the linked list that satisfies the
109
+ * condition specified by the `callbackFn` function. If no node satisfies the condition, it returns `undefined`.
112
110
  */
113
- getNode(index: number): SinglyLinkedListNode<NodeData> | undefined;
111
+ find(callbackFn: (data: NodeVal, index: number, list: SinglyLinkedList<NodeVal>) => boolean): NodeVal | undefined;
114
112
  /**
115
- * Return the first node and its index in the list that
116
- * satisfies the testing function
117
- * ```ts
118
- * new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
119
- * // { node: SinglyLinkedListNode, index: 0 }
120
- * ```
121
- * @param f A function to be applied to the val of each node
113
+ * The findIndex function returns the index of the first node in a singly linked list that satisfies a given condition,
114
+ * or -1 if no such node is found.
115
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
116
+ * value indicating whether the condition is met for a particular node in the singly linked list.
117
+ * @returns The method `findIndex` returns a number.
122
118
  */
123
- findNodeIndex(f: (data: NodeData, index: number, list: SinglyLinkedList<NodeData>) => boolean): ({
124
- node: SinglyLinkedListNode<NodeData>;
125
- index: number;
126
- }) | undefined;
119
+ findIndex(callbackFn: (data: NodeVal, index: number, list: SinglyLinkedList<NodeVal>) => boolean): number;
120
+ append(...args: NodeVal[]): SinglyLinkedList<NodeVal>;
121
+ /**
122
+ * The push function appends multiple NodeVal objects to a data structure and returns the new size of the data
123
+ * structure.
124
+ * @param {NodeVal[]} args - args is a rest parameter of type NodeVal[]. It allows the function to accept any number
125
+ * of arguments of type NodeVal.
126
+ * @returns The size of the data structure after the nodes are appended.
127
+ */
128
+ push(...args: NodeVal[]): number;
129
+ /**
130
+ * The `prepend` function adds new nodes to the beginning of a singly linked list.
131
+ * @param {NodeVal[]} args - An array of NodeVal objects.
132
+ * @returns The `prepend` method is returning the updated `SinglyLinkedList` object.
133
+ */
134
+ prepend(...args: NodeVal[]): SinglyLinkedList<NodeVal>;
135
+ /**
136
+ * The `insertAt` function inserts a value at a specified index in a singly linked list.
137
+ * @param {number} index - The index parameter is a number that represents the position at which the new node should be
138
+ * inserted in the linked list.
139
+ * @param {NodeVal} val - The `val` parameter represents the value of the node that you want to insert into the linked
140
+ * list.
141
+ * @returns The method `insertAt` returns the updated `SinglyLinkedList` object.
142
+ */
143
+ insertAt(index: number, val: NodeVal): SinglyLinkedList<NodeVal>;
144
+ /**
145
+ * The removeNode function removes a node from a singly linked list and updates the head, tail, and size properties
146
+ * accordingly.
147
+ * @param node - The `node` parameter is of type `SinglyLinkedListNode<NodeVal>`, which represents a node in a singly
148
+ * linked list.
149
+ * @returns the removed node.
150
+ */
151
+ removeNode(node: SinglyLinkedListNode<NodeVal>): SinglyLinkedListNode<NodeVal>;
152
+ /**
153
+ * The `removeAt` function removes a node at a specified index from a singly linked list.
154
+ * @param {number} index - The index parameter is a number that represents the position of the node to be removed in
155
+ * the singly linked list.
156
+ * @returns The method `removeAt` returns a `SinglyLinkedListNode<NodeVal>` if the node at the specified index is
157
+ * found and removed successfully. If the node is not found, it returns `undefined`.
158
+ */
159
+ removeAt(index: number): SinglyLinkedListNode<NodeVal> | undefined;
160
+ /**
161
+ * The `insertBefore` function inserts a new node with a given value before a specified reference node in a singly
162
+ * linked list.
163
+ * @param referenceNode - The referenceNode parameter is the node in the linked list before which the new node will be
164
+ * inserted.
165
+ * @param {NodeVal} val - The value of the new node that will be inserted before the reference node.
166
+ * @returns The method is returning the updated SinglyLinkedList object.
167
+ */
168
+ insertBefore(referenceNode: SinglyLinkedListNode<NodeVal>, val: NodeVal): SinglyLinkedList<NodeVal>;
169
+ /**
170
+ * The `sort` function uses the quicksort algorithm to sort the elements of a singly linked list based on a provided
171
+ * comparison function.
172
+ * @param start - The `start` parameter is the starting node of the sublist that needs to be sorted.
173
+ * @param end - The `end` parameter is a reference to the last node in the linked list. It is used as the pivot element
174
+ * for the quicksort algorithm.
175
+ * @returns The `sort` method is returning the sorted `SinglyLinkedList` object.
176
+ */
177
+ sort(compare: (a: NodeVal, b: NodeVal) => boolean): SinglyLinkedList<NodeVal>;
178
+ /**
179
+ * The `insertAfter` function inserts a new node with a given value after a specified reference node in a singly linked
180
+ * list.
181
+ * @param referenceNode - The referenceNode parameter is the node after which the new node will be inserted.
182
+ * @param {NodeVal} val - The value of the new node that will be inserted after the reference node.
183
+ * @returns The `insertAfter` method is returning the updated `SinglyLinkedList` object.
184
+ */
185
+ insertAfter(referenceNode: SinglyLinkedListNode<NodeVal>, val: NodeVal): SinglyLinkedList<NodeVal>;
127
186
  /**
128
- * Returns the first node in the list that
129
- * satisfies the provided testing function. Otherwise undefined is returned.
130
- * ```ts
131
- * new LinkedList(1, 2, 3).findNode(val => val === 1);
132
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
133
- * ```
134
- * @param f Function to test val against
135
- */
136
- findNode(f: (data: NodeData, index: number, list: SinglyLinkedList<NodeData>) => boolean): SinglyLinkedListNode<NodeData> | undefined;
137
- /**
138
- * Returns the value of the first element in the list that
139
- * satisfies the provided testing function. Otherwise undefined is returned.
140
- * ```ts
141
- * new LinkedList(1, 2, 3).find(val => val === 1); // 1
142
- * ```
143
- * @param f Function to test val against
144
- */
145
- find(f: (data: NodeData, index: number, list: SinglyLinkedList<NodeData>) => boolean): NodeData | undefined;
146
- /**
147
- * Returns the index of the first node in the list that
148
- * satisfies the provided testing function. Ohterwise -1 is returned.
149
- * ```ts
150
- * new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
151
- * ```
152
- * @param f Function to test val against
153
- */
154
- findIndex(f: (data: NodeData, index: number, list: SinglyLinkedList<NodeData>) => boolean): number;
155
- /**
156
- * Append one or any number of nodes to the end of the list.
157
- * This modifies the list in place and returns the list itself
158
- * to make this method chainable.
159
- * ```ts
160
- * new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
161
- * ```
162
- * @param args Data to be stored in the node, takes any number of arguments
163
- */
164
- append(...args: NodeData[]): SinglyLinkedList<NodeData>;
165
- /**
166
- * Synonym for append
167
- * ```ts
168
- * new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4
169
- * ```
170
- * @param args Data to be stored, takes any number of arguments
171
- */
172
- push(...args: NodeData[]): number;
173
- /**
174
- * Prepend any number of val arguments to the list. The
175
- * argument list is prepended as a block to reduce confusion:
176
- * ```javascript
177
- * new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]
178
- * ```
179
- * @param args Data to be stored in the node, accepts any number of arguments
180
- */
181
- prepend(...args: NodeData[]): SinglyLinkedList<NodeData>;
182
- /**
183
- * Insert a new node at a given index position. If index is
184
- * out of bounds, the node is appended, if index is negative
185
- * or 0, it will be prepended.
186
- * ```ts
187
- * new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
188
- * ```
189
- * @param index The index to insert the new node at
190
- * @param val Data to be stored on the new node
191
- */
192
- insertAt(index: number, val: NodeData): SinglyLinkedList<NodeData>;
193
- /**
194
- * Remove the specified node from the list and return the removed
195
- * node afterwards.
196
- * ```ts
197
- * const list = new LinkedList(1, 2, 3);
198
- * list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
199
- * ```
200
- * @param node The node to be removed
201
- */
202
- removeNode(node: SinglyLinkedListNode<NodeData>): SinglyLinkedListNode<NodeData>;
203
- /**
204
- * Remove the node at the specified index
205
- * ```ts
206
- * new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }
207
- * ```
208
- * @param index Index at which to remove
209
- */
210
- removeAt(index: number): SinglyLinkedListNode<NodeData> | undefined;
211
- /**
212
- * Insert a new node before the reference node
213
- * ```ts
214
- * const list = new LinkedList(1, 3);
215
- * list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3
216
- * ```
217
- * @param referenceNode The node reference
218
- * @param val Data to save in the node
219
- */
220
- insertBefore(referenceNode: SinglyLinkedListNode<NodeData>, val: NodeData): SinglyLinkedList<NodeData>;
221
- /**
222
- * Sorts the linked list using the provided compare function
223
- * @param compare A function used to compare the val of two nodes. It should return
224
- * a boolean. True will insert a before b, false will insert b before a.
225
- * (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1,
226
- * the sort order will be ascending.
227
- */
228
- sort(compare: (a: NodeData, b: NodeData) => boolean): SinglyLinkedList<NodeData>;
229
- /**
230
- * Insert a new node after this one
231
- * ```ts
232
- * const list = new LinkedList(2, 3);
233
- * list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3
234
- * ```
235
- * @param referenceNode The reference node
236
- * @param val Data to be saved in the node
237
- */
238
- insertAfter(referenceNode: SinglyLinkedListNode<NodeData>, val: NodeData): SinglyLinkedList<NodeData>;
239
- /**
240
- * Remove the first node from the list and return the val of the removed node
241
- * or undefined
242
- * ```ts
243
- * new LinkedList(1, 2, 3).shift(); // 1
244
- * ```
245
- */
246
- shift(): NodeData | undefined;
247
- /**
248
- * Remove the last node from the list and return the val of the removed node
249
- * or undefined if the list was empty
250
- * ```ts
251
- * new LinkedList(1, 2, 3).pop(); // 3
252
- * ```
253
- */
254
- pop(): NodeData | undefined;
255
- /**
256
- * Merge the current list with another. Both lists will be
257
- * equal after merging.
258
- * ```ts
259
- * const list = new LinkedList(1, 2);
260
- * const otherList = new LinkedList(3);
261
- * list.merge(otherList);
262
- * (list === otherList); // true
263
- * ```
264
- * @param list The list to be merged
265
- */
266
- merge(list: SinglyLinkedList<NodeData>): void;
267
- /**
268
- * Removes all nodes from a list
269
- *
270
- * ```ts
271
- * list.clear();
272
- * ```
187
+ * The `shift()` function removes and returns the first element from a linked list.
188
+ * @returns The `shift()` method is returning a value of type `NodeVal` or `undefined`.
189
+ */
190
+ shift(): NodeVal | undefined;
191
+ /**
192
+ * The `pop()` function removes and returns the last element from a linked list.
193
+ * @returns The `pop()` method is returning a value of type `NodeVal` or `undefined`.
194
+ */
195
+ pop(): NodeVal | undefined;
196
+ /**
197
+ * The merge function merges two singly linked lists by updating the next and prev pointers, as well as the head, tail,
198
+ * and size properties.
199
+ * @param list - The parameter "list" is a SinglyLinkedList object that contains nodes with data of type NodeVal.
200
+ */
201
+ merge(list: SinglyLinkedList<NodeVal>): void;
202
+ /**
203
+ * The clear() function resets the linked list by setting the head and tail to null and the size to 0.
204
+ * @returns The "this" object is being returned.
273
205
  */
274
206
  clear(): this;
275
207
  /**
276
- * The slice() method returns a shallow copy of a
277
- * portion of a list into a new list object selected
278
- * from start to end (end not included).
279
- * The original list will not be modified.
280
- * ```ts
281
- * const list = new LinkedList(1, 2, 3, 4, 5);
282
- * const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
283
- * ```
284
- * @param start Start index
285
- * @param end End index, optional
286
- */
287
- slice(start: number, end?: number): SinglyLinkedList<NodeData | {}>;
288
- /**
289
- * The reverse() function reverses the list in place and returns the list
290
- * itself.
291
- * ```ts
292
- * new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
293
- * ```
294
- */
295
- reverse(): SinglyLinkedList<NodeData>;
296
- /**
297
- * The forEach() method executes a provided function once for each list node.
298
- * ```ts
299
- * new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
300
- * ```
301
- * @param f Function to execute for each element, taking up to three arguments.
302
- * @param reverse Indicates if the list should be walked in reverse order, default is false
303
- */
304
- forEach(f: (data: any, index: number, list: SinglyLinkedList<NodeData>) => any, reverse?: boolean): void;
305
- /**
306
- * The map() method creates a new list with the results of
307
- * calling a provided function on every node in the calling list.
308
- * ```ts
309
- * new LinkedList(1, 2, 3).map(val => val + 10); // 11 <=> 12 <=> 13
310
- * ```
311
- * @param f Function that produces an node of the new list, taking up to three arguments
312
- * @param reverse Indicates if the list should be mapped in reverse order, default is false
313
- */
314
- map(f: (data: any, index: number, list: SinglyLinkedList<NodeData>) => any, reverse?: boolean): SinglyLinkedList<NodeData | {}>;
315
- /**
316
- * The filter() method creates a new list with all nodes
317
- * that pass the test implemented by the provided function.
318
- * ```ts
319
- * new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
320
- * ```
321
- * @param f Function to test each node val in the list. Return true to keep the node
322
- * @param reverse Indicates if the list should be filtered in reverse order, default is false
323
- */
324
- filter(f: (data: NodeData, index: number, list: SinglyLinkedList<NodeData>) => boolean, reverse?: boolean): SinglyLinkedList<NodeData | {}>;
325
- /**
326
- * Reduce over each node in the list
327
- * ```ts
328
- * new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
329
- * ```
330
- * @param f A reducer function
331
- * @param start An initial value
332
- * @returns The final state of the accumulator
333
- */
334
- reduce(f: (accumulator: any, currentNode: NodeData, index: number, list: SinglyLinkedList<NodeData>) => any, start?: any, reverse?: boolean): any;
335
- /**
336
- * Convert the linked list to an array
337
- * ```ts
338
- * new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
339
- * ```
340
- */
341
- toArray(): NodeData[];
342
- /**
343
- * Convert a linked list to string
344
- * ```ts
345
- * new LinkedList('one', 'two', 'three').toString(' <=> ') === 'one <=> two <=> three';
346
- * ```
347
- * @param separator Optional string to be placed in between val nodes, default is one space
208
+ * The `slice` function returns a new SinglyLinkedList containing a portion of the original list, starting from the
209
+ * specified index and ending at the optional end index.
210
+ * @param {number} start - The `start` parameter is a number that represents the index at which to start slicing the
211
+ * linked list.
212
+ * @param {number} [end] - The `end` parameter is an optional number that specifies the index at which to end the
213
+ * slicing. If no value is provided for `end`, or if the provided value is less than the `start` index, the slicing
214
+ * will continue until the end of the list.
215
+ * @returns a new SinglyLinkedList containing the sliced elements from the original list.
216
+ */
217
+ slice(start: number, end?: number): SinglyLinkedList<NodeVal | {}>;
218
+ /**
219
+ * The reverse() function reverses the order of nodes in a singly linked list.
220
+ * @returns The reverse() method is returning the reversed SinglyLinkedList.
221
+ */
222
+ reverse(): SinglyLinkedList<NodeVal>;
223
+ /**
224
+ * The `forEach` function iterates over a singly linked list and applies a callback function to each node, either in
225
+ * forward or reverse order.
226
+ * @param callbackFn - A callback function that will be called for each element in the linked list. It takes three
227
+ * parameters:
228
+ * @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
229
+ * to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
230
+ * (default), the iteration will start from the head and move towards the tail.
231
+ */
232
+ forEach(callbackFn: (data: any, index: number, list: SinglyLinkedList<NodeVal>) => any, reverse?: boolean): void;
233
+ /**
234
+ * The map function takes a callback function and applies it to each element in the linked list, returning a new linked
235
+ * list with the results.
236
+ * @param callbackFn - A callback function that will be applied to each element in the linked list. It takes three
237
+ * parameters:
238
+ * @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the mapping should be
239
+ * done in reverse order or not. If `reverse` is set to `true`, the mapping will be done in reverse order. If `reverse`
240
+ * is set to `false` or not provided, the mapping will be
241
+ * @returns The `map` function is returning a new `SinglyLinkedList` object.
242
+ */
243
+ map(callbackFn: (data: any, index: number, list: SinglyLinkedList<NodeVal>) => any, reverse?: boolean): SinglyLinkedList<NodeVal | {}>;
244
+ /**
245
+ * The `filter` function filters the elements of a singly linked list based on a given callback function.
246
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It should return a
247
+ * boolean value indicating whether the current element should be included in the filtered list or not.
248
+ * @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the filtered list should
249
+ * be reversed or not. If `reverse` is set to `true`, the filtered list will be in reverse order. If `reverse` is set
250
+ * to `false` or not provided, the filtered list will be in
251
+ * @returns The `filter` method is returning a new `SinglyLinkedList` object.
252
+ */
253
+ filter(callbackFn: (data: NodeVal, index: number, list: SinglyLinkedList<NodeVal>) => boolean, reverse?: boolean): SinglyLinkedList<NodeVal | {}>;
254
+ /**
255
+ * The `reduce` function iterates over a singly linked list and applies a callback function to each element,
256
+ * accumulating a single value.
257
+ * @param callbackFn - A callback function that will be called for each element in the linked list. It takes four
258
+ * parameters:
259
+ * @param {any} [start] - The `start` parameter is an optional initial value for the accumulator. If provided, the
260
+ * `reduce` function will start accumulating from this value. If not provided, the `reduce` function will use the value
261
+ * of the first element in the linked list as the initial value.
262
+ * @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
263
+ * to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
264
+ * (default), the iteration will start from the head and move towards the tail.
265
+ * @returns The `reduce` method returns the accumulated value after applying the callback function to each element in
266
+ * the linked list.
267
+ */
268
+ reduce(callbackFn: (accumulator: any, currentNode: NodeVal, index: number, list: SinglyLinkedList<NodeVal>) => any, start?: any, reverse?: boolean): any;
269
+ /**
270
+ * The toArray() function converts a NodeVal object into an array of NodeVal objects.
271
+ * @returns An array of NodeVal objects.
272
+ */
273
+ toArray(): NodeVal[];
274
+ /**
275
+ * The `toString` function takes an optional separator and returns a string representation of an array, with each
276
+ * element separated by the specified separator.
277
+ * @param [separator= ] - The separator parameter is a string that specifies the character(s) to be used as a separator
278
+ * between each element in the array when converting it to a string. By default, the separator is set to a space
279
+ * character (' ').
280
+ * @returns The toString method is being returned as a string.
348
281
  */
349
282
  toString(separator?: string): string;
350
283
  /**
351
- * The iterator implementation
352
- * ```ts
353
- * const list = new LinkedList(1, 2, 3);
354
- * for (const val of list) { log(val); } // 1 2 3
355
- * ```
284
+ * The function is an iterator that returns the values of each node in a linked list.
285
+ */
286
+ [Symbol.iterator](): IterableIterator<NodeVal>;
287
+ /**
288
+ * The function removes a node from either end of a singly linked list and returns its value.
289
+ * @param {SinglyLinkedListNode<NodeVal> | null} node - The `node` parameter is a reference to a node in a singly
290
+ * linked list. It can be either a `SinglyLinkedListNode` object or `null`.
291
+ * @returns The value of the removed node if the node is not null, otherwise undefined.
356
292
  */
357
- [Symbol.iterator](): IterableIterator<NodeData>;
358
- /** Private helper function to reduce duplication of pop() and shift() methods */
359
- private removeFromAnyEnd;
293
+ protected removeFromAnyEnd(node: SinglyLinkedListNode<NodeVal> | null): NodeVal | undefined;
360
294
  }