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,7 +1,10 @@
1
1
  "use strict";
2
2
  /**
3
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
4
- * @license MIT
3
+ * data-structure-typed
4
+ *
5
+ * @author Tyler Zeng
6
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
5
8
  */
6
9
  var __generator = (this && this.__generator) || function (thisArg, body) {
7
10
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
@@ -68,63 +71,71 @@ var __values = (this && this.__values) || function(o) {
68
71
  };
69
72
  Object.defineProperty(exports, "__esModule", { value: true });
70
73
  exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
71
- /**
72
- * The class which represents one link or node in a linked list
73
- * ```ts
74
- * const node = new SinglyLinkedListNode(1, null, null, null);
75
- * ```
76
- */
74
+ /* The SinglyLinkedListNode class represents a node in a singly linked list and provides methods for inserting, removing,
75
+ and accessing nodes. */
77
76
  var SinglyLinkedListNode = /** @class */ (function () {
78
- function SinglyLinkedListNode(
79
- /** Data stored on the node */
80
- val,
81
- /** The previous node in the list */
82
- prev,
83
- /** The next link in the list */
84
- next,
85
- /** The list this node belongs to */
86
- list) {
87
- this.val = val;
88
- this.prev = prev;
89
- this.next = next;
90
- this.list = list;
77
+ function SinglyLinkedListNode(val, prev, next, list) {
78
+ this._val = val;
79
+ this._prev = prev || null;
80
+ this._next = next || null;
81
+ this._list = list || null;
91
82
  }
92
- Object.defineProperty(SinglyLinkedListNode.prototype, "value", {
93
- /**
94
- * Alias to .val
95
- * ```ts
96
- * new LinkedList(1, 2, 3).head.value; // 1
97
- * ```
98
- */
83
+ Object.defineProperty(SinglyLinkedListNode.prototype, "val", {
84
+ get: function () {
85
+ return this._val;
86
+ },
87
+ set: function (value) {
88
+ this._val = value;
89
+ },
90
+ enumerable: false,
91
+ configurable: true
92
+ });
93
+ Object.defineProperty(SinglyLinkedListNode.prototype, "prev", {
99
94
  get: function () {
100
- return this.val;
95
+ return this._prev;
96
+ },
97
+ set: function (value) {
98
+ this._prev = value;
99
+ },
100
+ enumerable: false,
101
+ configurable: true
102
+ });
103
+ Object.defineProperty(SinglyLinkedListNode.prototype, "next", {
104
+ get: function () {
105
+ return this._next;
106
+ },
107
+ set: function (value) {
108
+ this._next = value;
109
+ },
110
+ enumerable: false,
111
+ configurable: true
112
+ });
113
+ Object.defineProperty(SinglyLinkedListNode.prototype, "list", {
114
+ get: function () {
115
+ return this._list;
116
+ },
117
+ set: function (value) {
118
+ this._list = value;
101
119
  },
102
120
  enumerable: false,
103
121
  configurable: true
104
122
  });
105
123
  Object.defineProperty(SinglyLinkedListNode.prototype, "index", {
106
- /**
107
- * Get the index of this node
108
- * ```ts
109
- * new LinkedList(1, 2, 3).head.index; // 0
110
- * ```
111
- */
112
124
  get: function () {
113
125
  var _this = this;
114
126
  if (!this.list) {
115
127
  return undefined;
116
128
  }
117
- return this.list.findIndex(function (value) { return value === _this.value; });
129
+ return this.list.findIndex(function (value) { return value === _this.val; });
118
130
  },
119
131
  enumerable: false,
120
132
  configurable: true
121
133
  });
122
134
  /**
123
- * Insert a new node before this one
124
- * ```ts
125
- * new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3
126
- * ```
127
- * @param val Data to save in the node
135
+ * The `insertBefore` function inserts a new node with the given value before the current node in a singly linked list.
136
+ * @param {NodeVal} val - The parameter "val" is of type "NodeVal". It represents the value of the node that you want
137
+ * to insert before the current node.
138
+ * @returns The method is returning a SinglyLinkedList<NodeVal>.
128
139
  */
129
140
  SinglyLinkedListNode.prototype.insertBefore = function (val) {
130
141
  return this.list !== null
@@ -132,11 +143,9 @@ var SinglyLinkedListNode = /** @class */ (function () {
132
143
  : new SinglyLinkedList(val, this.val);
133
144
  };
134
145
  /**
135
- * Insert new val after this node
136
- * ```ts
137
- * new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3
138
- * ```
139
- * @param val Data to be saved in the node
146
+ * The function inserts a new node with the given value after the current node in a singly linked list.
147
+ * @param {NodeVal} val - The parameter `val` is the value of the node that you want to insert after the current node.
148
+ * @returns The method is returning a SinglyLinkedList<NodeVal>.
140
149
  */
141
150
  SinglyLinkedListNode.prototype.insertAfter = function (val) {
142
151
  return this.list !== null
@@ -144,10 +153,8 @@ var SinglyLinkedListNode = /** @class */ (function () {
144
153
  : new SinglyLinkedList(this.val, val);
145
154
  };
146
155
  /**
147
- * Remove this node
148
- * ```ts
149
- * new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
150
- * ```
156
+ * The `remove()` function removes a node from a singly linked list.
157
+ * @returns The remove() method is returning a SinglyLinkedListNode<NodeVal> object.
151
158
  */
152
159
  SinglyLinkedListNode.prototype.remove = function () {
153
160
  if (this.list === null) {
@@ -158,71 +165,86 @@ var SinglyLinkedListNode = /** @class */ (function () {
158
165
  return SinglyLinkedListNode;
159
166
  }());
160
167
  exports.SinglyLinkedListNode = SinglyLinkedListNode;
161
- /**
162
- * A doubly linked list
163
- * ```ts
164
- * const list = new LinkedList(1, 2, 3);
165
- * const listFromArray = LinkedList.from([1, 2, 3]);
166
- * ```
167
- */
168
168
  var SinglyLinkedList = /** @class */ (function () {
169
+ /**
170
+ * The constructor initializes a linked list with the given arguments as nodes.
171
+ * @param {NodeVal[]} args - args is a rest parameter that allows the constructor to accept an arbitrary number of
172
+ * arguments of type NodeVal.
173
+ */
169
174
  function SinglyLinkedList() {
170
175
  var args = [];
171
176
  for (var _i = 0; _i < arguments.length; _i++) {
172
177
  args[_i] = arguments[_i];
173
178
  }
174
- this.head = null;
175
- this.tail = null;
176
- this.size = 0;
179
+ this._head = null;
180
+ this._tail = null;
181
+ this._size = 0;
177
182
  for (var i = 0; i < arguments.length; i++) {
178
183
  this.append(args[i]);
179
184
  }
180
185
  }
181
- Object.defineProperty(SinglyLinkedList.prototype, "length", {
182
- /**
183
- * The length of the list
184
- */
186
+ Object.defineProperty(SinglyLinkedList.prototype, "head", {
187
+ get: function () {
188
+ return this._head;
189
+ },
190
+ set: function (value) {
191
+ this._head = value;
192
+ },
193
+ enumerable: false,
194
+ configurable: true
195
+ });
196
+ Object.defineProperty(SinglyLinkedList.prototype, "tail", {
185
197
  get: function () {
186
- return this.size;
198
+ return this._tail;
199
+ },
200
+ set: function (value) {
201
+ this._tail = value;
202
+ },
203
+ enumerable: false,
204
+ configurable: true
205
+ });
206
+ Object.defineProperty(SinglyLinkedList.prototype, "size", {
207
+ get: function () {
208
+ return this._size;
209
+ },
210
+ set: function (value) {
211
+ this._size = value;
187
212
  },
188
213
  enumerable: false,
189
214
  configurable: true
190
215
  });
191
216
  /**
192
- * Convert any iterable to a new linked list
193
- * ```javascript
194
- * const array = [1, 2, 3];
195
- * const list = LinkedList.from(array);
196
- * ```
197
- * @param iterable Any iterable datatype like Array or Map
217
+ * The `from` function in TypeScript creates a new SinglyLinkedList instance from an iterable object.
218
+ * @param iterable - The `iterable` parameter is an object that can be iterated over, such as an array or a string. It
219
+ * contains a collection of elements of type `T`.
220
+ * @returns The method is returning a new instance of the SinglyLinkedList class.
198
221
  */
199
222
  SinglyLinkedList.from = function (iterable) {
200
223
  return new (SinglyLinkedList.bind.apply(SinglyLinkedList, __spreadArray([void 0], __read(iterable), false)))();
201
224
  };
202
225
  /**
203
- * Get the node val at a specified index, zero based
204
- * ```ts
205
- * new LinkedList(1, 2, 3).get(0); // 1
206
- * ```
207
- * @param index to retrieve val at
226
+ * The `get` function returns the value of a node at a given index in a data structure.
227
+ * @param {number} index - The index parameter is a number that represents the position of the node in the data
228
+ * structure.
229
+ * @returns The method is returning the value of the node at the specified index if the node exists, otherwise it
230
+ * returns undefined.
208
231
  */
209
232
  SinglyLinkedList.prototype.get = function (index) {
210
233
  var node = this.getNode(index);
211
234
  return node !== undefined ? node.val : undefined;
212
235
  };
213
236
  /**
214
- * Get the node at index, zero based
215
- * ```ts
216
- * new LinkedList(1, 2, 3).getNode(0);
217
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
218
- * ```
237
+ * The function `getNode` returns the node at a given index in a singly linked list.
238
+ * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
239
+ * retrieve from the linked list.
240
+ * @returns a SinglyLinkedListNode<NodeVal> object or undefined.
219
241
  */
220
242
  SinglyLinkedList.prototype.getNode = function (index) {
221
- if (this.head === null || index < 0 || index >= this.length) {
243
+ if (this.head === null || index < 0 || index >= this.size) {
222
244
  return undefined;
223
245
  }
224
- var asc = index < this.length / 2;
225
- var stopAt = asc ? index : this.length - index - 1;
246
+ var asc = index < this.size / 2;
247
+ var stopAt = asc ? index : this.size - index - 1;
226
248
  var nextNode = asc ? 'next' : 'prev';
227
249
  var currentNode = asc ? this.head : this.tail;
228
250
  // TODO after no-non-null-assertion not ensure the logic
@@ -234,19 +256,21 @@ var SinglyLinkedList = /** @class */ (function () {
234
256
  return currentNode || undefined;
235
257
  };
236
258
  /**
237
- * Return the first node and its index in the list that
238
- * satisfies the testing function
239
- * ```ts
240
- * new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
241
- * // { node: SinglyLinkedListNode, index: 0 }
242
- * ```
243
- * @param f A function to be applied to the val of each node
259
+ * The function `findNodeIndex` searches for a node in a singly linked list that satisfies a given condition and
260
+ * returns its index and the node itself.
261
+ * @param callbackFn - The callbackFn parameter is a function that takes three arguments: data, index, and list. It is
262
+ * used to determine whether a node in the singly linked list matches a certain condition. The function should return a
263
+ * boolean value indicating whether the condition is met for the given node.
264
+ * @returns The function `findNodeIndex` returns an object with two properties: `node` and `index`. The `node` property
265
+ * contains the node that matches the condition specified in the `callbackFn` function, and the `index` property
266
+ * contains the index of that node in the linked list. If no node matches the condition, the function returns
267
+ * `undefined`.
244
268
  */
245
- SinglyLinkedList.prototype.findNodeIndex = function (f) {
269
+ SinglyLinkedList.prototype.findNodeIndex = function (callbackFn) {
246
270
  var currentIndex = 0;
247
271
  var currentNode = this.head;
248
272
  while (currentNode) {
249
- if (f(currentNode.val, currentIndex, this)) {
273
+ if (callbackFn(currentNode.val, currentIndex, this)) {
250
274
  return {
251
275
  index: currentIndex,
252
276
  node: currentNode,
@@ -258,51 +282,40 @@ var SinglyLinkedList = /** @class */ (function () {
258
282
  return undefined;
259
283
  };
260
284
  /**
261
- * Returns the first node in the list that
262
- * satisfies the provided testing function. Otherwise undefined is returned.
263
- * ```ts
264
- * new LinkedList(1, 2, 3).findNode(val => val === 1);
265
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
266
- * ```
267
- * @param f Function to test val against
285
+ * The findNode function searches for a node in a singly linked list based on a given callback function.
286
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
287
+ * value indicating whether the current node matches the desired criteria.
288
+ * @returns The function `findNode` returns a `SinglyLinkedListNode<NodeVal>` if a node satisfying the condition
289
+ * specified by the `callbackFn` is found in the linked list. If no such node is found, it returns `undefined`.
268
290
  */
269
- SinglyLinkedList.prototype.findNode = function (f) {
270
- var nodeIndex = this.findNodeIndex(f);
291
+ SinglyLinkedList.prototype.findNode = function (callbackFn) {
292
+ var nodeIndex = this.findNodeIndex(callbackFn);
271
293
  return nodeIndex !== undefined ? nodeIndex.node : undefined;
272
294
  };
273
295
  /**
274
- * Returns the value of the first element in the list that
275
- * satisfies the provided testing function. Otherwise undefined is returned.
276
- * ```ts
277
- * new LinkedList(1, 2, 3).find(val => val === 1); // 1
278
- * ```
279
- * @param f Function to test val against
296
+ * The `find` function in TypeScript searches for a node in a singly linked list based on a given callback function and
297
+ * returns the value of the found node.
298
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
299
+ * value indicating whether the condition is met for a particular node in the linked list.
300
+ * @returns The method `find` returns the `NodeVal` value of the first node in the linked list that satisfies the
301
+ * condition specified by the `callbackFn` function. If no node satisfies the condition, it returns `undefined`.
280
302
  */
281
- SinglyLinkedList.prototype.find = function (f) {
282
- var nodeIndex = this.findNodeIndex(f);
303
+ SinglyLinkedList.prototype.find = function (callbackFn) {
304
+ var nodeIndex = this.findNodeIndex(callbackFn);
283
305
  return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
284
306
  };
285
307
  /**
286
- * Returns the index of the first node in the list that
287
- * satisfies the provided testing function. Ohterwise -1 is returned.
288
- * ```ts
289
- * new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
290
- * ```
291
- * @param f Function to test val against
308
+ * The findIndex function returns the index of the first node in a singly linked list that satisfies a given condition,
309
+ * or -1 if no such node is found.
310
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
311
+ * value indicating whether the condition is met for a particular node in the singly linked list.
312
+ * @returns The method `findIndex` returns a number.
292
313
  */
293
- SinglyLinkedList.prototype.findIndex = function (f) {
294
- var nodeIndex = this.findNodeIndex(f);
314
+ SinglyLinkedList.prototype.findIndex = function (callbackFn) {
315
+ var nodeIndex = this.findNodeIndex(callbackFn);
295
316
  return nodeIndex !== undefined ? nodeIndex.index : -1;
296
317
  };
297
- /**
298
- * Append one or any number of nodes to the end of the list.
299
- * This modifies the list in place and returns the list itself
300
- * to make this method chainable.
301
- * ```ts
302
- * new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
303
- * ```
304
- * @param args Data to be stored in the node, takes any number of arguments
305
- */
318
+ /* The above code is a comment in TypeScript. It is using the triple hash symbol ( */
306
319
  SinglyLinkedList.prototype.append = function () {
307
320
  var e_1, _a;
308
321
  var args = [];
@@ -333,11 +346,11 @@ var SinglyLinkedList = /** @class */ (function () {
333
346
  return this;
334
347
  };
335
348
  /**
336
- * Synonym for append
337
- * ```ts
338
- * new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4
339
- * ```
340
- * @param args Data to be stored, takes any number of arguments
349
+ * The push function appends multiple NodeVal objects to a data structure and returns the new size of the data
350
+ * structure.
351
+ * @param {NodeVal[]} args - args is a rest parameter of type NodeVal[]. It allows the function to accept any number
352
+ * of arguments of type NodeVal.
353
+ * @returns The size of the data structure after the nodes are appended.
341
354
  */
342
355
  SinglyLinkedList.prototype.push = function () {
343
356
  var args = [];
@@ -345,15 +358,12 @@ var SinglyLinkedList = /** @class */ (function () {
345
358
  args[_i] = arguments[_i];
346
359
  }
347
360
  this.append.apply(this, __spreadArray([], __read(args), false));
348
- return this.length;
361
+ return this.size;
349
362
  };
350
363
  /**
351
- * Prepend any number of val arguments to the list. The
352
- * argument list is prepended as a block to reduce confusion:
353
- * ```javascript
354
- * new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]
355
- * ```
356
- * @param args Data to be stored in the node, accepts any number of arguments
364
+ * The `prepend` function adds new nodes to the beginning of a singly linked list.
365
+ * @param {NodeVal[]} args - An array of NodeVal objects.
366
+ * @returns The `prepend` method is returning the updated `SinglyLinkedList` object.
357
367
  */
358
368
  SinglyLinkedList.prototype.prepend = function () {
359
369
  var e_2, _a;
@@ -386,14 +396,12 @@ var SinglyLinkedList = /** @class */ (function () {
386
396
  return this;
387
397
  };
388
398
  /**
389
- * Insert a new node at a given index position. If index is
390
- * out of bounds, the node is appended, if index is negative
391
- * or 0, it will be prepended.
392
- * ```ts
393
- * new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
394
- * ```
395
- * @param index The index to insert the new node at
396
- * @param val Data to be stored on the new node
399
+ * The `insertAt` function inserts a value at a specified index in a singly linked list.
400
+ * @param {number} index - The index parameter is a number that represents the position at which the new node should be
401
+ * inserted in the linked list.
402
+ * @param {NodeVal} val - The `val` parameter represents the value of the node that you want to insert into the linked
403
+ * list.
404
+ * @returns The method `insertAt` returns the updated `SinglyLinkedList` object.
397
405
  */
398
406
  SinglyLinkedList.prototype.insertAt = function (index, val) {
399
407
  if (this.head === null) {
@@ -412,13 +420,11 @@ var SinglyLinkedList = /** @class */ (function () {
412
420
  return this;
413
421
  };
414
422
  /**
415
- * Remove the specified node from the list and return the removed
416
- * node afterwards.
417
- * ```ts
418
- * const list = new LinkedList(1, 2, 3);
419
- * list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
420
- * ```
421
- * @param node The node to be removed
423
+ * The removeNode function removes a node from a singly linked list and updates the head, tail, and size properties
424
+ * accordingly.
425
+ * @param node - The `node` parameter is of type `SinglyLinkedListNode<NodeVal>`, which represents a node in a singly
426
+ * linked list.
427
+ * @returns the removed node.
422
428
  */
423
429
  SinglyLinkedList.prototype.removeNode = function (node) {
424
430
  if (node.list !== this) {
@@ -443,24 +449,23 @@ var SinglyLinkedList = /** @class */ (function () {
443
449
  return node;
444
450
  };
445
451
  /**
446
- * Remove the node at the specified index
447
- * ```ts
448
- * new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }
449
- * ```
450
- * @param index Index at which to remove
452
+ * The `removeAt` function removes a node at a specified index from a singly linked list.
453
+ * @param {number} index - The index parameter is a number that represents the position of the node to be removed in
454
+ * the singly linked list.
455
+ * @returns The method `removeAt` returns a `SinglyLinkedListNode<NodeVal>` if the node at the specified index is
456
+ * found and removed successfully. If the node is not found, it returns `undefined`.
451
457
  */
452
458
  SinglyLinkedList.prototype.removeAt = function (index) {
453
459
  var node = this.getNode(index);
454
460
  return node !== undefined ? this.removeNode(node) : undefined;
455
461
  };
456
462
  /**
457
- * Insert a new node before the reference node
458
- * ```ts
459
- * const list = new LinkedList(1, 3);
460
- * list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3
461
- * ```
462
- * @param referenceNode The node reference
463
- * @param val Data to save in the node
463
+ * The `insertBefore` function inserts a new node with a given value before a specified reference node in a singly
464
+ * linked list.
465
+ * @param referenceNode - The referenceNode parameter is the node in the linked list before which the new node will be
466
+ * inserted.
467
+ * @param {NodeVal} val - The value of the new node that will be inserted before the reference node.
468
+ * @returns The method is returning the updated SinglyLinkedList object.
464
469
  */
465
470
  SinglyLinkedList.prototype.insertBefore = function (referenceNode, val) {
466
471
  var node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
@@ -475,17 +480,18 @@ var SinglyLinkedList = /** @class */ (function () {
475
480
  return this;
476
481
  };
477
482
  /**
478
- * Sorts the linked list using the provided compare function
479
- * @param compare A function used to compare the val of two nodes. It should return
480
- * a boolean. True will insert a before b, false will insert b before a.
481
- * (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1,
482
- * the sort order will be ascending.
483
+ * The `sort` function uses the quicksort algorithm to sort the elements of a singly linked list based on a provided
484
+ * comparison function.
485
+ * @param start - The `start` parameter is the starting node of the sublist that needs to be sorted.
486
+ * @param end - The `end` parameter is a reference to the last node in the linked list. It is used as the pivot element
487
+ * for the quicksort algorithm.
488
+ * @returns The `sort` method is returning the sorted `SinglyLinkedList` object.
483
489
  */
484
490
  SinglyLinkedList.prototype.sort = function (compare) {
485
491
  if (this.head === null || this.tail === null) {
486
492
  return this;
487
493
  }
488
- if (this.length < 2) {
494
+ if (this.size < 2) {
489
495
  return this;
490
496
  }
491
497
  var quicksort = function (start, end) {
@@ -526,13 +532,11 @@ var SinglyLinkedList = /** @class */ (function () {
526
532
  return this;
527
533
  };
528
534
  /**
529
- * Insert a new node after this one
530
- * ```ts
531
- * const list = new LinkedList(2, 3);
532
- * list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3
533
- * ```
534
- * @param referenceNode The reference node
535
- * @param val Data to be saved in the node
535
+ * The `insertAfter` function inserts a new node with a given value after a specified reference node in a singly linked
536
+ * list.
537
+ * @param referenceNode - The referenceNode parameter is the node after which the new node will be inserted.
538
+ * @param {NodeVal} val - The value of the new node that will be inserted after the reference node.
539
+ * @returns The `insertAfter` method is returning the updated `SinglyLinkedList` object.
536
540
  */
537
541
  SinglyLinkedList.prototype.insertAfter = function (referenceNode, val) {
538
542
  var node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
@@ -547,35 +551,23 @@ var SinglyLinkedList = /** @class */ (function () {
547
551
  return this;
548
552
  };
549
553
  /**
550
- * Remove the first node from the list and return the val of the removed node
551
- * or undefined
552
- * ```ts
553
- * new LinkedList(1, 2, 3).shift(); // 1
554
- * ```
554
+ * The `shift()` function removes and returns the first element from a linked list.
555
+ * @returns The `shift()` method is returning a value of type `NodeVal` or `undefined`.
555
556
  */
556
557
  SinglyLinkedList.prototype.shift = function () {
557
558
  return this.removeFromAnyEnd(this.head);
558
559
  };
559
560
  /**
560
- * Remove the last node from the list and return the val of the removed node
561
- * or undefined if the list was empty
562
- * ```ts
563
- * new LinkedList(1, 2, 3).pop(); // 3
564
- * ```
561
+ * The `pop()` function removes and returns the last element from a linked list.
562
+ * @returns The `pop()` method is returning a value of type `NodeVal` or `undefined`.
565
563
  */
566
564
  SinglyLinkedList.prototype.pop = function () {
567
565
  return this.removeFromAnyEnd(this.tail);
568
566
  };
569
567
  /**
570
- * Merge the current list with another. Both lists will be
571
- * equal after merging.
572
- * ```ts
573
- * const list = new LinkedList(1, 2);
574
- * const otherList = new LinkedList(3);
575
- * list.merge(otherList);
576
- * (list === otherList); // true
577
- * ```
578
- * @param list The list to be merged
568
+ * The merge function merges two singly linked lists by updating the next and prev pointers, as well as the head, tail,
569
+ * and size properties.
570
+ * @param list - The parameter "list" is a SinglyLinkedList object that contains nodes with data of type NodeVal.
579
571
  */
580
572
  SinglyLinkedList.prototype.merge = function (list) {
581
573
  if (this.tail !== null) {
@@ -592,11 +584,8 @@ var SinglyLinkedList = /** @class */ (function () {
592
584
  list.tail = this.tail;
593
585
  };
594
586
  /**
595
- * Removes all nodes from a list
596
- *
597
- * ```ts
598
- * list.clear();
599
- * ```
587
+ * The clear() function resets the linked list by setting the head and tail to null and the size to 0.
588
+ * @returns The "this" object is being returned.
600
589
  */
601
590
  SinglyLinkedList.prototype.clear = function () {
602
591
  this.head = null;
@@ -605,18 +594,15 @@ var SinglyLinkedList = /** @class */ (function () {
605
594
  return this;
606
595
  };
607
596
  /**
608
- * The slice() method returns a shallow copy of a
609
- * portion of a list into a new list object selected
610
- * from start to end (end not included).
611
- * The original list will not be modified.
612
- * ```ts
613
- * const list = new LinkedList(1, 2, 3, 4, 5);
614
- * const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
615
- * ```
616
- * @param start Start index
617
- * @param end End index, optional
597
+ * The `slice` function returns a new SinglyLinkedList containing a portion of the original list, starting from the
598
+ * specified index and ending at the optional end index.
599
+ * @param {number} start - The `start` parameter is a number that represents the index at which to start slicing the
600
+ * linked list.
601
+ * @param {number} [end] - The `end` parameter is an optional number that specifies the index at which to end the
602
+ * slicing. If no value is provided for `end`, or if the provided value is less than the `start` index, the slicing
603
+ * will continue until the end of the list.
604
+ * @returns a new SinglyLinkedList containing the sliced elements from the original list.
618
605
  */
619
- // eslint-disable-next-line @typescript-eslint/ban-types
620
606
  SinglyLinkedList.prototype.slice = function (start, end) {
621
607
  var list = new SinglyLinkedList();
622
608
  var finish = end;
@@ -624,7 +610,7 @@ var SinglyLinkedList = /** @class */ (function () {
624
610
  return list;
625
611
  }
626
612
  if (finish === undefined || finish < start) {
627
- finish = this.length;
613
+ finish = this.size;
628
614
  }
629
615
  var head = this.getNode(start);
630
616
  for (var i = 0; i < finish - start && head !== null && head !== undefined; i++) {
@@ -634,11 +620,8 @@ var SinglyLinkedList = /** @class */ (function () {
634
620
  return list;
635
621
  };
636
622
  /**
637
- * The reverse() function reverses the list in place and returns the list
638
- * itself.
639
- * ```ts
640
- * new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
641
- * ```
623
+ * The reverse() function reverses the order of nodes in a singly linked list.
624
+ * @returns The reverse() method is returning the reversed SinglyLinkedList.
642
625
  */
643
626
  SinglyLinkedList.prototype.reverse = function () {
644
627
  var currentNode = this.head;
@@ -654,75 +637,80 @@ var SinglyLinkedList = /** @class */ (function () {
654
637
  return this;
655
638
  };
656
639
  /**
657
- * The forEach() method executes a provided function once for each list node.
658
- * ```ts
659
- * new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
660
- * ```
661
- * @param f Function to execute for each element, taking up to three arguments.
662
- * @param reverse Indicates if the list should be walked in reverse order, default is false
640
+ * The `forEach` function iterates over a singly linked list and applies a callback function to each node, either in
641
+ * forward or reverse order.
642
+ * @param callbackFn - A callback function that will be called for each element in the linked list. It takes three
643
+ * parameters:
644
+ * @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
645
+ * to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
646
+ * (default), the iteration will start from the head and move towards the tail.
663
647
  */
664
- SinglyLinkedList.prototype.forEach = function (f, reverse) {
648
+ SinglyLinkedList.prototype.forEach = function (callbackFn, reverse) {
665
649
  if (reverse === void 0) { reverse = false; }
666
- var currentIndex = reverse ? this.length - 1 : 0;
650
+ var currentIndex = reverse ? this.size - 1 : 0;
667
651
  var currentNode = reverse ? this.tail : this.head;
668
652
  var modifier = reverse ? -1 : 1;
669
653
  var nextNode = reverse ? 'prev' : 'next';
670
654
  while (currentNode) {
671
- f(currentNode.val, currentIndex, this);
655
+ callbackFn(currentNode.val, currentIndex, this);
672
656
  currentNode = currentNode[nextNode];
673
657
  currentIndex += modifier;
674
658
  }
675
659
  };
676
660
  /**
677
- * The map() method creates a new list with the results of
678
- * calling a provided function on every node in the calling list.
679
- * ```ts
680
- * new LinkedList(1, 2, 3).map(val => val + 10); // 11 <=> 12 <=> 13
681
- * ```
682
- * @param f Function that produces an node of the new list, taking up to three arguments
683
- * @param reverse Indicates if the list should be mapped in reverse order, default is false
661
+ * The map function takes a callback function and applies it to each element in the linked list, returning a new linked
662
+ * list with the results.
663
+ * @param callbackFn - A callback function that will be applied to each element in the linked list. It takes three
664
+ * parameters:
665
+ * @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the mapping should be
666
+ * done in reverse order or not. If `reverse` is set to `true`, the mapping will be done in reverse order. If `reverse`
667
+ * is set to `false` or not provided, the mapping will be
668
+ * @returns The `map` function is returning a new `SinglyLinkedList` object.
684
669
  */
685
- // eslint-disable-next-line @typescript-eslint/ban-types
686
- SinglyLinkedList.prototype.map = function (f, reverse) {
670
+ SinglyLinkedList.prototype.map = function (callbackFn, reverse) {
687
671
  var _this = this;
688
672
  if (reverse === void 0) { reverse = false; }
689
673
  var list = new SinglyLinkedList();
690
- this.forEach(function (val, index) { return list.append(f(val, index, _this)); }, reverse);
674
+ this.forEach(function (val, index) { return list.append(callbackFn(val, index, _this)); }, reverse);
691
675
  return list;
692
676
  };
693
677
  /**
694
- * The filter() method creates a new list with all nodes
695
- * that pass the test implemented by the provided function.
696
- * ```ts
697
- * new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
698
- * ```
699
- * @param f Function to test each node val in the list. Return true to keep the node
700
- * @param reverse Indicates if the list should be filtered in reverse order, default is false
678
+ * The `filter` function filters the elements of a singly linked list based on a given callback function.
679
+ * @param callbackFn - A callback function that takes three parameters: data, index, and list. It should return a
680
+ * boolean value indicating whether the current element should be included in the filtered list or not.
681
+ * @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the filtered list should
682
+ * be reversed or not. If `reverse` is set to `true`, the filtered list will be in reverse order. If `reverse` is set
683
+ * to `false` or not provided, the filtered list will be in
684
+ * @returns The `filter` method is returning a new `SinglyLinkedList` object.
701
685
  */
702
- // eslint-disable-next-line @typescript-eslint/ban-types
703
- SinglyLinkedList.prototype.filter = function (f, reverse) {
686
+ SinglyLinkedList.prototype.filter = function (callbackFn, reverse) {
704
687
  var _this = this;
705
688
  if (reverse === void 0) { reverse = false; }
706
689
  var list = new SinglyLinkedList();
707
690
  this.forEach(function (val, index) {
708
- if (f(val, index, _this)) {
691
+ if (callbackFn(val, index, _this)) {
709
692
  list.append(val);
710
693
  }
711
694
  }, reverse);
712
695
  return list;
713
696
  };
714
697
  /**
715
- * Reduce over each node in the list
716
- * ```ts
717
- * new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
718
- * ```
719
- * @param f A reducer function
720
- * @param start An initial value
721
- * @returns The final state of the accumulator
722
- */
723
- SinglyLinkedList.prototype.reduce = function (f, start, reverse) {
698
+ * The `reduce` function iterates over a singly linked list and applies a callback function to each element,
699
+ * accumulating a single value.
700
+ * @param callbackFn - A callback function that will be called for each element in the linked list. It takes four
701
+ * parameters:
702
+ * @param {any} [start] - The `start` parameter is an optional initial value for the accumulator. If provided, the
703
+ * `reduce` function will start accumulating from this value. If not provided, the `reduce` function will use the value
704
+ * of the first element in the linked list as the initial value.
705
+ * @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
706
+ * to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
707
+ * (default), the iteration will start from the head and move towards the tail.
708
+ * @returns The `reduce` method returns the accumulated value after applying the callback function to each element in
709
+ * the linked list.
710
+ */
711
+ SinglyLinkedList.prototype.reduce = function (callbackFn, start, reverse) {
724
712
  if (reverse === void 0) { reverse = false; }
725
- var currentIndex = reverse ? this.length - 1 : 0;
713
+ var currentIndex = reverse ? this.size - 1 : 0;
726
714
  var modifier = reverse ? -1 : 1;
727
715
  var nextNode = reverse ? 'prev' : 'next';
728
716
  var currentElement = reverse ? this.tail : this.head;
@@ -738,38 +726,33 @@ var SinglyLinkedList = /** @class */ (function () {
738
726
  throw new TypeError('Reduce of empty LinkedList with no initial value');
739
727
  }
740
728
  while (currentElement) {
741
- result = f(result, currentElement.val, currentIndex, this);
729
+ result = callbackFn(result, currentElement.val, currentIndex, this);
742
730
  currentIndex += modifier;
743
731
  currentElement = currentElement[nextNode];
744
732
  }
745
733
  return result;
746
734
  };
747
735
  /**
748
- * Convert the linked list to an array
749
- * ```ts
750
- * new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
751
- * ```
736
+ * The toArray() function converts a NodeVal object into an array of NodeVal objects.
737
+ * @returns An array of NodeVal objects.
752
738
  */
753
739
  SinglyLinkedList.prototype.toArray = function () {
754
740
  return __spreadArray([], __read(this), false);
755
741
  };
756
742
  /**
757
- * Convert a linked list to string
758
- * ```ts
759
- * new LinkedList('one', 'two', 'three').toString(' <=> ') === 'one <=> two <=> three';
760
- * ```
761
- * @param separator Optional string to be placed in between val nodes, default is one space
743
+ * The `toString` function takes an optional separator and returns a string representation of an array, with each
744
+ * element separated by the specified separator.
745
+ * @param [separator= ] - The separator parameter is a string that specifies the character(s) to be used as a separator
746
+ * between each element in the array when converting it to a string. By default, the separator is set to a space
747
+ * character (' ').
748
+ * @returns The toString method is being returned as a string.
762
749
  */
763
750
  SinglyLinkedList.prototype.toString = function (separator) {
764
751
  if (separator === void 0) { separator = ' '; }
765
752
  return this.reduce(function (s, val) { return "".concat(s).concat(separator).concat(val); });
766
753
  };
767
754
  /**
768
- * The iterator implementation
769
- * ```ts
770
- * const list = new LinkedList(1, 2, 3);
771
- * for (const val of list) { log(val); } // 1 2 3
772
- * ```
755
+ * The function is an iterator that returns the values of each node in a linked list.
773
756
  */
774
757
  SinglyLinkedList.prototype[Symbol.iterator] = function () {
775
758
  var element;
@@ -789,7 +772,12 @@ var SinglyLinkedList = /** @class */ (function () {
789
772
  }
790
773
  });
791
774
  };
792
- /** Private helper function to reduce duplication of pop() and shift() methods */
775
+ /**
776
+ * The function removes a node from either end of a singly linked list and returns its value.
777
+ * @param {SinglyLinkedListNode<NodeVal> | null} node - The `node` parameter is a reference to a node in a singly
778
+ * linked list. It can be either a `SinglyLinkedListNode` object or `null`.
779
+ * @returns The value of the removed node if the node is not null, otherwise undefined.
780
+ */
793
781
  SinglyLinkedList.prototype.removeFromAnyEnd = function (node) {
794
782
  return node !== null ? this.removeNode(node).val : undefined;
795
783
  };