doubly-linked-list-typed 2.0.5 → 2.1.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 (102) hide show
  1. package/README.md +14 -14
  2. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  3. package/dist/data-structures/base/iterable-element-base.js +149 -107
  4. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  5. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  6. package/dist/data-structures/base/linear-base.d.ts +250 -192
  7. package/dist/data-structures/base/linear-base.js +137 -274
  8. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  9. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  13. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  14. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  15. package/dist/data-structures/binary-tree/binary-tree.js +598 -869
  16. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  17. package/dist/data-structures/binary-tree/bst.js +505 -481
  18. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  19. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  20. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  21. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  22. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  23. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  24. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  25. package/dist/data-structures/graph/abstract-graph.js +267 -237
  26. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  27. package/dist/data-structures/graph/directed-graph.js +146 -233
  28. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  29. package/dist/data-structures/graph/map-graph.js +56 -59
  30. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  31. package/dist/data-structures/graph/undirected-graph.js +129 -149
  32. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  33. package/dist/data-structures/hash/hash-map.js +270 -457
  34. package/dist/data-structures/heap/heap.d.ts +214 -289
  35. package/dist/data-structures/heap/heap.js +340 -349
  36. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  37. package/dist/data-structures/heap/max-heap.js +11 -66
  38. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  39. package/dist/data-structures/heap/min-heap.js +11 -66
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  43. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  44. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  45. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  51. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  52. package/dist/data-structures/queue/deque.d.ts +227 -254
  53. package/dist/data-structures/queue/deque.js +309 -348
  54. package/dist/data-structures/queue/queue.d.ts +180 -201
  55. package/dist/data-structures/queue/queue.js +265 -248
  56. package/dist/data-structures/stack/stack.d.ts +124 -102
  57. package/dist/data-structures/stack/stack.js +181 -125
  58. package/dist/data-structures/trie/trie.d.ts +164 -165
  59. package/dist/data-structures/trie/trie.js +189 -172
  60. package/dist/interfaces/binary-tree.d.ts +56 -6
  61. package/dist/interfaces/graph.d.ts +16 -0
  62. package/dist/types/data-structures/base/base.d.ts +1 -1
  63. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  64. package/dist/types/utils/utils.d.ts +1 -0
  65. package/dist/utils/utils.d.ts +1 -1
  66. package/dist/utils/utils.js +2 -1
  67. package/package.json +2 -2
  68. package/src/data-structures/base/iterable-element-base.ts +238 -115
  69. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  70. package/src/data-structures/base/linear-base.ts +271 -277
  71. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  72. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  73. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  74. package/src/data-structures/binary-tree/binary-tree.ts +664 -893
  75. package/src/data-structures/binary-tree/bst.ts +568 -570
  76. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  77. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  78. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  79. package/src/data-structures/graph/abstract-graph.ts +339 -264
  80. package/src/data-structures/graph/directed-graph.ts +146 -236
  81. package/src/data-structures/graph/map-graph.ts +63 -60
  82. package/src/data-structures/graph/undirected-graph.ts +129 -152
  83. package/src/data-structures/hash/hash-map.ts +274 -496
  84. package/src/data-structures/heap/heap.ts +389 -402
  85. package/src/data-structures/heap/max-heap.ts +12 -76
  86. package/src/data-structures/heap/min-heap.ts +13 -76
  87. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  88. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  89. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  90. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  91. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  92. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  93. package/src/data-structures/queue/deque.ts +381 -357
  94. package/src/data-structures/queue/queue.ts +310 -264
  95. package/src/data-structures/stack/stack.ts +217 -131
  96. package/src/data-structures/trie/trie.ts +240 -175
  97. package/src/interfaces/binary-tree.ts +240 -6
  98. package/src/interfaces/graph.ts +37 -0
  99. package/src/types/data-structures/base/base.ts +5 -5
  100. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  101. package/src/types/utils/utils.ts +2 -0
  102. package/src/utils/utils.ts +9 -14
@@ -7,24 +7,44 @@
7
7
  */
8
8
  import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
9
9
  import { LinearLinkedBase, LinkedListNode } from '../base/linear-base';
10
+ /**
11
+ * Node of a singly linked list; stores value and the next link.
12
+ * @remarks Time O(1), Space O(1)
13
+ * @template E
14
+ */
10
15
  export declare class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
11
16
  /**
12
- * The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
13
- * @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
14
- * will be stored in the node of a linked list.
17
+ * Create a list node.
18
+ * @remarks Time O(1), Space O(1)
19
+ * @param value - Element value to store.
20
+ * @returns New node instance.
15
21
  */
16
22
  constructor(value: E);
17
23
  protected _next: SinglyLinkedListNode<E> | undefined;
24
+ /**
25
+ * Get the next node.
26
+ * @remarks Time O(1), Space O(1)
27
+ * @returns Next node or undefined.
28
+ */
18
29
  get next(): SinglyLinkedListNode<E> | undefined;
30
+ /**
31
+ * Set the next node.
32
+ * @remarks Time O(1), Space O(1)
33
+ * @param value - Next node or undefined.
34
+ * @returns void
35
+ */
19
36
  set next(value: SinglyLinkedListNode<E> | undefined);
20
37
  }
21
38
  /**
39
+ * Singly linked list with O(1) push/pop-like ends operations and linear scans.
40
+ * @remarks Time O(1), Space O(1)
41
+ * @template E
42
+ * @template R
22
43
  * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
23
44
  * 2. Bidirectional Traversal: Unlike doubly linked lists, singly linked lists can be easily traversed forwards but not backwards.
24
45
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
25
46
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
26
47
  * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
27
- *
28
48
  * @example
29
49
  * // implementation of a basic text editor
30
50
  * class TextEditor {
@@ -93,408 +113,339 @@ export declare class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
93
113
  * console.log(editor.getText()); // 'Haello'
94
114
  */
95
115
  export declare class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
116
+ protected _equals: (a: E, b: E) => boolean;
117
+ /**
118
+ * Create a SinglyLinkedList and optionally bulk-insert elements.
119
+ * @remarks Time O(N), Space O(N)
120
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
121
+ * @param [options] - Options such as maxLen and toElementFn.
122
+ * @returns New SinglyLinkedList instance.
123
+ */
96
124
  constructor(elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>, options?: SinglyLinkedListOptions<E, R>);
97
125
  protected _head: SinglyLinkedListNode<E> | undefined;
126
+ /**
127
+ * Get the head node.
128
+ * @remarks Time O(1), Space O(1)
129
+ * @returns Head node or undefined.
130
+ */
98
131
  get head(): SinglyLinkedListNode<E> | undefined;
99
132
  protected _tail: SinglyLinkedListNode<E> | undefined;
133
+ /**
134
+ * Get the tail node.
135
+ * @remarks Time O(1), Space O(1)
136
+ * @returns Tail node or undefined.
137
+ */
100
138
  get tail(): SinglyLinkedListNode<E> | undefined;
101
- get first(): E | undefined;
102
- get last(): E | undefined;
103
139
  protected _length: number;
140
+ /**
141
+ * Get the number of elements.
142
+ * @remarks Time O(1), Space O(1)
143
+ * @returns Current length.
144
+ */
104
145
  get length(): number;
105
146
  /**
106
- * Time Complexity: O(n)
107
- * Space Complexity: O(n)
108
- *
109
- * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
110
- * array.
111
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
112
- * @returns The `fromArray` function returns a `SinglyLinkedList` object.
147
+ * Get the first element value.
148
+ * @remarks Time O(1), Space O(1)
149
+ * @returns First element or undefined.
150
+ */
151
+ get first(): E | undefined;
152
+ /**
153
+ * Get the last element value.
154
+ * @remarks Time O(1), Space O(1)
155
+ * @returns Last element or undefined.
156
+ */
157
+ get last(): E | undefined;
158
+ /**
159
+ * Create a new list from an iterable of elements.
160
+ * @remarks Time O(N), Space O(N)
161
+ * @template E
162
+ * @template R
163
+ * @template S
164
+ * @param this - The constructor (subclass) to instantiate.
165
+ * @param data - Iterable of elements to insert.
166
+ * @param [options] - Options forwarded to the constructor.
167
+ * @returns A new list populated with the iterable's elements.
113
168
  */
114
- static fromArray<E>(data: E[]): SinglyLinkedList<E, any>;
169
+ static from<E, R = any, S extends SinglyLinkedList<E, R> = SinglyLinkedList<E, R>>(this: new (elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>, options?: SinglyLinkedListOptions<E, R>) => S, data: Iterable<E>, options?: SinglyLinkedListOptions<E, R>): S;
115
170
  /**
116
- * Time Complexity: O(1)
117
- * Space Complexity: O(1)
118
- *
119
- * The `push` function adds a new element or node to the end of a singly linked list.
120
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
121
- * method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
122
- * @returns The `push` method is returning a boolean value, specifically `true`.
171
+ * Append an element/node to the tail.
172
+ * @remarks Time O(1), Space O(1)
173
+ * @param elementOrNode - Element or node to append.
174
+ * @returns True when appended.
123
175
  */
124
176
  push(elementOrNode: E | SinglyLinkedListNode<E>): boolean;
125
177
  /**
126
- * Time Complexity: O(n)
127
- * Space Complexity: O(1)
128
- *
129
- * The `pop` function removes and returns the value of the last element in a linked list.
130
- * @returns The method is returning the value of the element that is being popped from the end of the
131
- * list.
178
+ * Remove and return the tail element.
179
+ * @remarks Time O(N), Space O(1)
180
+ * @returns Removed element or undefined.
132
181
  */
133
182
  pop(): E | undefined;
134
183
  /**
135
- * Time Complexity: O(1)
136
- * Space Complexity: O(1)
137
- *
138
- * The `shift()` function removes and returns the value of the first element in a linked list.
139
- * @returns The value of the removed node.
184
+ * Remove and return the head element.
185
+ * @remarks Time O(1), Space O(1)
186
+ * @returns Removed element or undefined.
140
187
  */
141
188
  shift(): E | undefined;
142
189
  /**
143
- * Time Complexity: O(1)
144
- * Space Complexity: O(1)
145
- *
146
- * The unshift function adds a new element or node to the beginning of a singly linked list in
147
- * TypeScript.
148
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
149
- * `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
150
- * element of type `E`.
151
- * @returns The `unshift` method is returning a boolean value, specifically `true`.
190
+ * Prepend an element/node to the head.
191
+ * @remarks Time O(1), Space O(1)
192
+ * @param elementOrNode - Element or node to prepend.
193
+ * @returns True when prepended.
152
194
  */
153
195
  unshift(elementOrNode: E | SinglyLinkedListNode<E>): boolean;
154
196
  /**
155
- * Time Complexity: O(k)
156
- * Space Complexity: O(k)
157
- *
158
- * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
159
- * transformation function if provided.
160
- * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
161
- * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
162
- * or `SinglyLinkedListNode<E>`.
163
- * @returns The `pushMany` function returns an array of boolean values indicating whether each
164
- * element was successfully pushed into the data structure.
197
+ * Append a sequence of elements/nodes.
198
+ * @remarks Time O(N), Space O(1)
199
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
200
+ * @returns Array of per-element success flags.
165
201
  */
166
202
  pushMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[];
167
203
  /**
168
- * Time Complexity: O(k)
169
- * Space Complexity: O(k)
170
- *
171
- * The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
172
- * converting them using a provided function.
173
- * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
174
- * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
175
- * `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
176
- * performs an `unshift` operation on the linked list for each
177
- * @returns The `unshiftMany` function is returning an array of boolean values, where each value
178
- * represents the result of calling the `unshift` method on the current instance of the class.
204
+ * Prepend a sequence of elements/nodes.
205
+ * @remarks Time O(N), Space O(1)
206
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
207
+ * @returns Array of per-element success flags.
179
208
  */
180
209
  unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>): boolean[];
181
210
  /**
182
- * Time Complexity: O(n)
183
- * Space Complexity: O(1)
184
- *
185
- * This function searches for a specific element in a singly linked list based on a given node or
186
- * predicate.
187
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
188
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
189
- * the following types:
190
- * @returns The `get` method returns the value of the first node in the singly linked list that
191
- * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
211
+ * Find the first value matching a predicate (by node).
212
+ * @remarks Time O(N), Space O(1)
213
+ * @param elementNodeOrPredicate - Element, node, or node predicate to match.
214
+ * @returns Matched value or undefined.
192
215
  */
193
216
  search(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): E | undefined;
194
217
  /**
195
- * Time Complexity: O(n)
196
- * Space Complexity: O(1)
197
- *
198
- * The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range.
199
- * @param {number} index - The index parameter is a number that represents the position of the element we want to
200
- * retrieve from the list.
201
- * @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or
202
- * `undefined` if the index is out of bounds.
218
+ * Get the element at a given index.
219
+ * @remarks Time O(N), Space O(1)
220
+ * @param index - Zero-based index.
221
+ * @returns Element or undefined.
203
222
  */
204
223
  at(index: number): E | undefined;
205
224
  /**
206
- * Time Complexity: O(1)
207
- * Space Complexity: O(1)
208
- *
209
- * The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
210
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
211
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
212
- * one of the following types:
213
- * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
214
- * instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
215
- * parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
216
- * the function returns `false`.
225
+ * Type guard: check whether the input is a SinglyLinkedListNode.
226
+ * @remarks Time O(1), Space O(1)
227
+ * @param elementNodeOrPredicate - Element, node, or predicate.
228
+ * @returns True if the value is a SinglyLinkedListNode.
217
229
  */
218
230
  isNode(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is SinglyLinkedListNode<E>;
219
231
  /**
220
- * Time Complexity: O(n)
221
- * Space Complexity: O(1)
222
- *
223
- * The function `getNodeAt` returns the node at a given index in a singly linked list.
224
- * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
225
- * retrieve from the linked list. It indicates the zero-based index of the node we want to access.
226
- * @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
227
- * specified index exists, or `undefined` if the index is out of bounds.
232
+ * Get the node reference at a given index.
233
+ * @remarks Time O(N), Space O(1)
234
+ * @param index - Zero-based index.
235
+ * @returns Node or undefined.
228
236
  */
229
237
  getNodeAt(index: number): SinglyLinkedListNode<E> | undefined;
230
238
  /**
231
- * Time Complexity: O(n)
232
- * Space Complexity: O(1)
233
- *
234
- * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
235
- * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
236
- * data structure. It is of type number.
237
- * @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
238
- * bounds.
239
+ * Delete the element at an index.
240
+ * @remarks Time O(N), Space O(1)
241
+ * @param index - Zero-based index.
242
+ * @returns Removed element or undefined.
239
243
  */
240
244
  deleteAt(index: number): E | undefined;
241
245
  /**
242
- * Time Complexity: O(n)
243
- * Space Complexity: O(1)
244
- *
245
- * The delete function removes a node with a specific value from a singly linked list.
246
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
247
- * or a `SinglyLinkedListNode<E>` object.
248
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
249
- * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
246
+ * Delete the first match by value/node.
247
+ * @remarks Time O(N), Space O(1)
248
+ * @param [elementOrNode] - Element or node to remove; if omitted/undefined, nothing happens.
249
+ * @returns True if removed.
250
250
  */
251
251
  delete(elementOrNode: E | SinglyLinkedListNode<E> | undefined): boolean;
252
252
  /**
253
- * Time Complexity: O(n)
254
- * Space Complexity: O(1)
255
- *
256
- * The `addAt` function inserts a new element or node at a specified index in a singly linked list.
257
- * @param {number} index - The `index` parameter represents the position at which you want to add a
258
- * new element or node in the linked list. It is a number that indicates the index where the new
259
- * element or node should be inserted.
260
- * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
261
- * `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
262
- * parameter represents the element or node that you want to add to the linked list at the specified
263
- * index.
264
- * @returns The `addAt` method returns a boolean value - `true` if the element or node was
265
- * successfully added at the specified index, and `false` if the index is out of bounds.
253
+ * Insert a new element/node at an index, shifting following nodes.
254
+ * @remarks Time O(N), Space O(1)
255
+ * @param index - Zero-based index.
256
+ * @param newElementOrNode - Element or node to insert.
257
+ * @returns True if inserted.
266
258
  */
267
259
  addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
268
260
  /**
269
- * Time Complexity: O(n)
270
- * Space Complexity: O(1)
271
- *
272
- * The function setAt(index, value) updates the value at a specified index in a data structure if the
273
- * index exists.
274
- * @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
275
- * data structure where you want to set a new value.
276
- * @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
277
- * want to set at the specified index in the data structure.
278
- * @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
279
- * is successfully updated, and `false` if the index is out of bounds (i.e., the node at that index
280
- * does not exist).
261
+ * Set the element value at an index.
262
+ * @remarks Time O(N), Space O(1)
263
+ * @param index - Zero-based index.
264
+ * @param value - New value.
265
+ * @returns True if updated.
281
266
  */
282
267
  setAt(index: number, value: E): boolean;
283
268
  /**
284
- * Time Complexity: O(1)
285
- * Space Complexity: O(1)
286
- *
287
- * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
288
- * whether it is empty or not.
289
- * @returns A boolean value indicating whether the length of the object is equal to 0.
269
+ * Check whether the list is empty.
270
+ * @remarks Time O(1), Space O(1)
271
+ * @returns True if length is 0.
290
272
  */
291
273
  isEmpty(): boolean;
292
274
  /**
293
- * Time Complexity: O(1)
294
- * Space Complexity: O(1)
295
- *
296
- * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
275
+ * Remove all nodes and reset length.
276
+ * @remarks Time O(N), Space O(1)
277
+ * @returns void
297
278
  */
298
279
  clear(): void;
299
280
  /**
300
- * Time Complexity: O(n)
301
- * Space Complexity: O(1)
302
- *
303
- * The `reverse` function reverses the order of the nodes in a singly linked list.
304
- * @returns The reverse() method does not return anything. It has a return type of void.
281
+ * Reverse the list in place.
282
+ * @remarks Time O(N), Space O(1)
283
+ * @returns This list.
305
284
  */
306
285
  reverse(): this;
307
286
  /**
308
- * Time Complexity: O(n)
309
- * Space Complexity: O(1)
310
- *
311
- * The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
312
- * element, node, or predicate.
313
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
314
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
315
- * of the following types:
316
- * @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
317
- * found based on the provided predicate, or it returns `undefined` if no matching node is found or
318
- * if the input parameter is `undefined`.
287
+ * Find a node by value, reference, or predicate.
288
+ * @remarks Time O(N), Space O(1)
289
+ * @param [elementNodeOrPredicate] - Element, node, or node predicate to match.
290
+ * @returns Matching node or undefined.
319
291
  */
320
292
  getNode(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined): SinglyLinkedListNode<E> | undefined;
321
293
  /**
322
- * Time Complexity: O(n)
323
- * Space Complexity: O(1)
324
- *
325
- * The function `addBefore` in TypeScript adds a new element or node before an existing element or
326
- * node in a singly linked list.
327
- * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
328
- * element or node in the linked list before which you want to add a new element or node.
329
- * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
330
- * `addBefore` method represents the element or node that you want to insert before the existing
331
- * element or node in the linked list. This new element can be of type `E` or a
332
- * `SinglyLinkedListNode<E>`.
333
- * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
334
- * successfully added before the existing element or node, and `false` if the operation was
335
- * unsuccessful.
294
+ * Insert a new element/node before an existing one.
295
+ * @remarks Time O(N), Space O(1)
296
+ * @param existingElementOrNode - Existing element or node.
297
+ * @param newElementOrNode - Element or node to insert.
298
+ * @returns True if inserted.
336
299
  */
337
300
  addBefore(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
338
301
  /**
339
- * Time Complexity: O(n)
340
- * Space Complexity: O(1)
341
- *
342
- * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
343
- * in a singly linked list.
344
- * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
345
- * an element of type E or a SinglyLinkedListNode of type E.
346
- * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
347
- * `addAfter` method represents the element or node that you want to add after the existing element
348
- * or node in a singly linked list. This parameter can be either the value of the new element or a
349
- * reference to a `SinglyLinkedListNode` containing
350
- * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
351
- * successfully added after the existing element or node, and `false` if the existing element or node
352
- * was not found.
302
+ * Insert a new element/node after an existing one.
303
+ * @remarks Time O(N), Space O(1)
304
+ * @param existingElementOrNode - Existing element or node.
305
+ * @param newElementOrNode - Element or node to insert.
306
+ * @returns True if inserted.
353
307
  */
354
308
  addAfter(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
355
309
  /**
356
- * Time Complexity: O(n)
357
- * Space Complexity: O(1)
358
- *
359
- * The function `splice` in TypeScript overrides the default behavior to remove and insert elements
360
- * in a singly linked list while handling boundary cases.
361
- * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
362
- * to start modifying the list. It specifies the position where elements will be added or removed.
363
- * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
364
- * number of elements to remove from the array starting at the specified `start` index. If
365
- * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
366
- * elements can still be inserted at
367
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements to be
368
- * inserted into the list at the specified `start` index. These elements will be inserted in place of
369
- * the elements that are removed from the list. The `splice` method allows you to add new elements to
370
- * the list while
371
- * @returns The `splice` method is returning a `SinglyLinkedList` containing the elements that were
372
- * removed from the original list during the splice operation.
310
+ * Remove and/or insert elements at a position (array-like behavior).
311
+ * @remarks Time O(N + M), Space O(M)
312
+ * @param start - Start index (clamped to [0, length]).
313
+ * @param [deleteCount] - Number of elements to remove (default 0).
314
+ * @param [items] - Elements to insert after `start`.
315
+ * @returns A new list containing the removed elements (typed as `this`).
373
316
  */
374
317
  splice(start: number, deleteCount?: number, ...items: E[]): this;
375
318
  /**
376
- * Time Complexity: O(n)
377
- * Space Complexity: O(1)
378
- *
379
- * The function `countOccurrences` iterates through a singly linked list and counts the occurrences
380
- * of a specified element or nodes that satisfy a given predicate.
381
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
382
- * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
383
- * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
384
- * node, or predicate function in the singly linked list.
319
+ * Count how many nodes match a value/node/predicate.
320
+ * @remarks Time O(N), Space O(1)
321
+ * @param elementOrNode - Element, node, or node predicate to match.
322
+ * @returns Number of matches in the list.
385
323
  */
386
324
  countOccurrences(elementOrNode: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number;
387
325
  /**
388
- * Time Complexity: O(n)
389
- * Space Complexity: O(n)
390
- *
391
- * The `clone` function returns a new instance of the `SinglyLinkedList` class with the same values
392
- * as the original list.
393
- * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
394
- * is a clone of the original list.
326
+ * Set the equality comparator used to compare values.
327
+ * @remarks Time O(1), Space O(1)
328
+ * @param equals - Equality predicate (a, b) → boolean.
329
+ * @returns This list.
330
+ */
331
+ setEquality(equals: (a: E, b: E) => boolean): this;
332
+ /**
333
+ * Delete the first node whose value matches a predicate.
334
+ * @remarks Time O(N), Space O(1)
335
+ * @param predicate - Predicate (value, index, list) → boolean to decide deletion.
336
+ * @returns True if a node was removed.
337
+ */
338
+ deleteWhere(predicate: (value: E, index: number, list: this) => boolean): boolean;
339
+ /**
340
+ * Deep clone this list (values are copied by reference).
341
+ * @remarks Time O(N), Space O(N)
342
+ * @returns A new list with the same element sequence.
395
343
  */
396
344
  clone(): this;
397
345
  /**
398
- * Time Complexity: O(n)
399
- * Space Complexity: O(n)
400
- *
401
- * The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
402
- * list and applying a callback function to each element to determine if it should be included in the
403
- * filtered list.
404
- * @param callback - The callback parameter is a function that will be called for each element in the
405
- * list. It takes three arguments: the current element, the index of the current element, and the
406
- * list itself. The callback function should return a boolean value indicating whether the current
407
- * element should be included in the filtered list or not
408
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
409
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
410
- * passed as the `this` value to the `callback` function. If `thisArg` is
411
- * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
412
- * elements that pass the filter condition specified by the `callback` function.
413
- */
414
- filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): SinglyLinkedList<E, R>;
415
- /**
416
- * Time Complexity: O(n)
417
- * Space Complexity: O(n)
418
- *
419
- * The `map` function takes a callback function and returns a new SinglyLinkedList with the results
420
- * of applying the callback to each element in the original list.
421
- * @param callback - The `callback` parameter is a function that will be called for each element in
422
- * the original list. It takes three arguments: `current` (the current element being processed),
423
- * `index` (the index of the current element), and `this` (the original list). It should return a
424
- * value
425
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
426
- * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
427
- * input and returns the converted element. If this parameter is not provided, the raw element will
428
- * be used as is.
429
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
430
- * specify the value of `this` within the callback function. It is used to set the context or scope
431
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
432
- * value of
433
- * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
434
- */
435
- map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): SinglyLinkedList<EM, RM>;
436
- /**
437
- * The function `_createInstance` returns a new instance of `SinglyLinkedList` with the specified
438
- * options.
439
- * @param [options] - The `options` parameter in the `_createInstance` method is of type
440
- * `SinglyLinkedListOptions<E, R>`, which is used to configure the behavior of the `SinglyLinkedList`
441
- * instance being created. It is an optional parameter, meaning it can be omitted when calling the
442
- * method.
443
- * @returns An instance of the `SinglyLinkedList` class with an empty array and the provided options
444
- * is being returned.
346
+ * Filter values into a new list of the same class.
347
+ * @remarks Time O(N), Space O(N)
348
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
349
+ * @param [thisArg] - Value for `this` inside the callback.
350
+ * @returns A new list with kept values.
445
351
  */
446
- protected _createInstance(options?: SinglyLinkedListOptions<E, R>): this;
352
+ filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): this;
447
353
  /**
448
- * The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
354
+ * Map values into a new list of the same class.
355
+ * @remarks Time O(N), Space O(N)
356
+ * @param callback - Mapping function (value, index, list) → newValue.
357
+ * @param [thisArg] - Value for `this` inside the callback.
358
+ * @returns A new list with mapped values.
449
359
  */
450
- protected _getIterator(): IterableIterator<E>;
360
+ mapSame(callback: ElementCallback<E, R, E>, thisArg?: any): this;
451
361
  /**
452
- * The function returns an iterator that iterates over the elements of a collection in reverse order.
362
+ * Map values into a new list (possibly different element type).
363
+ * @remarks Time O(N), Space O(N)
364
+ * @template EM
365
+ * @template RM
366
+ * @param callback - Mapping function (value, index, list) → newElement.
367
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
368
+ * @param [thisArg] - Value for `this` inside the callback.
369
+ * @returns A new SinglyLinkedList with mapped values.
453
370
  */
454
- protected _getReverseIterator(): IterableIterator<E>;
371
+ map<EM, RM = any>(callback: ElementCallback<E, R, EM>, options?: SinglyLinkedListOptions<EM, RM>, thisArg?: any): SinglyLinkedList<EM, RM>;
455
372
  /**
456
- * The function `_getNodeIterator` returns an iterator that iterates over the nodes of a singly
457
- * linked list.
373
+ * (Protected) Create a node from a value.
374
+ * @remarks Time O(1), Space O(1)
375
+ * @param value - Value to wrap in a node.
376
+ * @returns A new SinglyLinkedListNode instance.
458
377
  */
459
- protected _getNodeIterator(): IterableIterator<SinglyLinkedListNode<E>>;
378
+ protected _createNode(value: E): SinglyLinkedListNode<E>;
460
379
  /**
461
- * The _isPredicate function in TypeScript checks if the input is a function that takes a
462
- * SinglyLinkedListNode as an argument and returns a boolean.
463
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
464
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
465
- * @returns The _isPredicate method is returning a boolean value based on whether the
466
- * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
467
- * function, the method will return true, indicating that it is a predicate function. If it is not a
468
- * function, the method will return false.
380
+ * (Protected) Check if input is a node predicate function.
381
+ * @remarks Time O(1), Space O(1)
382
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
383
+ * @returns True if input is a predicate function.
469
384
  */
470
385
  protected _isPredicate(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is (node: SinglyLinkedListNode<E>) => boolean;
471
386
  /**
472
- * The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
473
- * node if necessary.
474
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
475
- * an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
476
- * @returns A SinglyLinkedListNode<E> object is being returned.
387
+ * (Protected) Normalize input into a node instance.
388
+ * @remarks Time O(1), Space O(1)
389
+ * @param elementOrNode - Element or node.
390
+ * @returns A SinglyLinkedListNode for the provided input.
477
391
  */
478
392
  protected _ensureNode(elementOrNode: E | SinglyLinkedListNode<E>): SinglyLinkedListNode<E>;
479
393
  /**
480
- * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
481
- * function, or a value to compare with the node's value.
482
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
483
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
484
- * @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
485
- * function is returned that checks if a given node is equal to the input node. If the input is a
486
- * predicate function, it is returned as is. If the input is neither a node nor a predicate function,
487
- * a function is returned that checks if a given node's value is equal to the input
394
+ * (Protected) Normalize input into a node predicate.
395
+ * @remarks Time O(1), Space O(1)
396
+ * @param elementNodeOrPredicate - Element, node, or predicate.
397
+ * @returns A predicate taking a node and returning true/false.
488
398
  */
489
399
  protected _ensurePredicate(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): (node: SinglyLinkedListNode<E>) => boolean;
490
400
  /**
491
- * The function `_getPrevNode` returns the node before a given node in a singly linked list.
492
- * @param node - The `node` parameter in the `_getPrevNode` method is a reference to a node in a
493
- * singly linked list. The method is used to find the node that comes before the given node in the
494
- * linked list.
495
- * @returns The `_getPrevNode` method returns either the previous node of the input node in a singly
496
- * linked list or `undefined` if the input node is the head of the list or if the input node is not
497
- * found in the list.
401
+ * (Protected) Get the previous node of a given node.
402
+ * @remarks Time O(N), Space O(1)
403
+ * @param node - A node in the list.
404
+ * @returns Previous node or undefined.
498
405
  */
499
406
  protected _getPrevNode(node: SinglyLinkedListNode<E>): SinglyLinkedListNode<E> | undefined;
407
+ /**
408
+ * (Protected) Iterate values from head to tail.
409
+ * @remarks Time O(N), Space O(1)
410
+ * @returns Iterator of values (E).
411
+ */
412
+ protected _getIterator(): IterableIterator<E>;
413
+ /**
414
+ * (Protected) Iterate values from tail to head.
415
+ * @remarks Time O(N), Space O(N)
416
+ * @returns Iterator of values (E).
417
+ */
418
+ protected _getReverseIterator(): IterableIterator<E>;
419
+ /**
420
+ * (Protected) Iterate nodes from head to tail.
421
+ * @remarks Time O(N), Space O(1)
422
+ * @returns Iterator of nodes.
423
+ */
424
+ protected _getNodeIterator(): IterableIterator<SinglyLinkedListNode<E>>;
425
+ /**
426
+ * (Protected) Create an empty instance of the same concrete class.
427
+ * @remarks Time O(1), Space O(1)
428
+ * @param [options] - Options forwarded to the constructor.
429
+ * @returns An empty like-kind list instance.
430
+ */
431
+ protected _createInstance(options?: SinglyLinkedListOptions<E, R>): this;
432
+ /**
433
+ * (Protected) Create a like-kind instance and seed it from an iterable.
434
+ * @remarks Time O(N), Space O(N)
435
+ * @template EM
436
+ * @template RM
437
+ * @param [elements] - Iterable used to seed the new list.
438
+ * @param [options] - Options forwarded to the constructor.
439
+ * @returns A like-kind SinglyLinkedList instance.
440
+ */
441
+ protected _createLike<EM, RM>(elements?: Iterable<EM> | Iterable<RM> | Iterable<SinglyLinkedListNode<EM>>, options?: SinglyLinkedListOptions<EM, RM>): SinglyLinkedList<EM, RM>;
442
+ /**
443
+ * (Protected) Spawn an empty like-kind list instance.
444
+ * @remarks Time O(1), Space O(1)
445
+ * @template EM
446
+ * @template RM
447
+ * @param [options] - Options forwarded to the constructor.
448
+ * @returns An empty like-kind SinglyLinkedList instance.
449
+ */
450
+ protected _spawnLike<EM, RM>(options?: SinglyLinkedListOptions<EM, RM>): SinglyLinkedList<EM, RM>;
500
451
  }