data-structure-typed 1.12.11 → 1.15.0

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