data-structure-typed 1.12.21 → 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 (42) hide show
  1. package/README.md +278 -179
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
  3. package/dist/data-structures/binary-tree/binary-tree.js +67 -0
  4. package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
  5. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  6. package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
  7. package/dist/data-structures/graph/abstract-graph.js +47 -7
  8. package/dist/data-structures/graph/directed-graph.d.ts +8 -0
  9. package/dist/data-structures/graph/directed-graph.js +14 -2
  10. package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
  11. package/dist/data-structures/graph/undirected-graph.js +23 -1
  12. package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
  13. package/dist/data-structures/hash/coordinate-map.js +16 -0
  14. package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
  15. package/dist/data-structures/hash/coordinate-set.js +16 -0
  16. package/dist/data-structures/heap/heap.d.ts +16 -0
  17. package/dist/data-structures/heap/heap.js +38 -0
  18. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
  20. package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
  21. package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
  22. package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
  23. package/dist/data-structures/priority-queue/priority-queue.js +18 -2
  24. package/dist/data-structures/queue/deque.d.ts +18 -7
  25. package/dist/data-structures/queue/deque.js +50 -3
  26. package/dist/data-structures/types/abstract-graph.d.ts +2 -2
  27. package/package.json +2 -2
  28. package/src/data-structures/binary-tree/aa-tree.ts +1 -1
  29. package/src/data-structures/binary-tree/binary-tree.ts +84 -14
  30. package/src/data-structures/binary-tree/segment-tree.ts +45 -13
  31. package/src/data-structures/graph/abstract-graph.ts +58 -15
  32. package/src/data-structures/graph/directed-graph.ts +14 -5
  33. package/src/data-structures/graph/undirected-graph.ts +23 -6
  34. package/src/data-structures/hash/coordinate-map.ts +13 -1
  35. package/src/data-structures/hash/coordinate-set.ts +13 -1
  36. package/src/data-structures/heap/heap.ts +31 -0
  37. package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
  38. package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
  39. package/src/data-structures/priority-queue/priority-queue.ts +15 -2
  40. package/src/data-structures/queue/deque.ts +38 -8
  41. package/src/data-structures/types/abstract-graph.ts +3 -3
  42. package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
@@ -71,63 +71,71 @@ var __values = (this && this.__values) || function(o) {
71
71
  };
72
72
  Object.defineProperty(exports, "__esModule", { value: true });
73
73
  exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
74
- /**
75
- * The class which represents one link or node in a linked list
76
- * ```ts
77
- * const node = new SinglyLinkedListNode(1, null, null, null);
78
- * ```
79
- */
74
+ /* The SinglyLinkedListNode class represents a node in a singly linked list and provides methods for inserting, removing,
75
+ and accessing nodes. */
80
76
  var SinglyLinkedListNode = /** @class */ (function () {
81
- function SinglyLinkedListNode(
82
- /** Data stored on the node */
83
- val,
84
- /** The previous node in the list */
85
- prev,
86
- /** The next link in the list */
87
- next,
88
- /** The list this node belongs to */
89
- list) {
90
- this.val = val;
91
- this.prev = prev;
92
- this.next = next;
93
- 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;
94
82
  }
95
- Object.defineProperty(SinglyLinkedListNode.prototype, "value", {
96
- /**
97
- * Alias to .val
98
- * ```ts
99
- * new LinkedList(1, 2, 3).head.value; // 1
100
- * ```
101
- */
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", {
102
94
  get: function () {
103
- 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;
104
119
  },
105
120
  enumerable: false,
106
121
  configurable: true
107
122
  });
108
123
  Object.defineProperty(SinglyLinkedListNode.prototype, "index", {
109
- /**
110
- * Get the index of this node
111
- * ```ts
112
- * new LinkedList(1, 2, 3).head.index; // 0
113
- * ```
114
- */
115
124
  get: function () {
116
125
  var _this = this;
117
126
  if (!this.list) {
118
127
  return undefined;
119
128
  }
120
- return this.list.findIndex(function (value) { return value === _this.value; });
129
+ return this.list.findIndex(function (value) { return value === _this.val; });
121
130
  },
122
131
  enumerable: false,
123
132
  configurable: true
124
133
  });
125
134
  /**
126
- * Insert a new node before this one
127
- * ```ts
128
- * new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3
129
- * ```
130
- * @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>.
131
139
  */
132
140
  SinglyLinkedListNode.prototype.insertBefore = function (val) {
133
141
  return this.list !== null
@@ -135,11 +143,9 @@ var SinglyLinkedListNode = /** @class */ (function () {
135
143
  : new SinglyLinkedList(val, this.val);
136
144
  };
137
145
  /**
138
- * Insert new val after this node
139
- * ```ts
140
- * new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3
141
- * ```
142
- * @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>.
143
149
  */
144
150
  SinglyLinkedListNode.prototype.insertAfter = function (val) {
145
151
  return this.list !== null
@@ -147,10 +153,8 @@ var SinglyLinkedListNode = /** @class */ (function () {
147
153
  : new SinglyLinkedList(this.val, val);
148
154
  };
149
155
  /**
150
- * Remove this node
151
- * ```ts
152
- * new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
153
- * ```
156
+ * The `remove()` function removes a node from a singly linked list.
157
+ * @returns The remove() method is returning a SinglyLinkedListNode<NodeVal> object.
154
158
  */
155
159
  SinglyLinkedListNode.prototype.remove = function () {
156
160
  if (this.list === null) {
@@ -161,71 +165,86 @@ var SinglyLinkedListNode = /** @class */ (function () {
161
165
  return SinglyLinkedListNode;
162
166
  }());
163
167
  exports.SinglyLinkedListNode = SinglyLinkedListNode;
164
- /**
165
- * A doubly linked list
166
- * ```ts
167
- * const list = new LinkedList(1, 2, 3);
168
- * const listFromArray = LinkedList.from([1, 2, 3]);
169
- * ```
170
- */
171
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
+ */
172
174
  function SinglyLinkedList() {
173
175
  var args = [];
174
176
  for (var _i = 0; _i < arguments.length; _i++) {
175
177
  args[_i] = arguments[_i];
176
178
  }
177
- this.head = null;
178
- this.tail = null;
179
- this.size = 0;
179
+ this._head = null;
180
+ this._tail = null;
181
+ this._size = 0;
180
182
  for (var i = 0; i < arguments.length; i++) {
181
183
  this.append(args[i]);
182
184
  }
183
185
  }
184
- Object.defineProperty(SinglyLinkedList.prototype, "length", {
185
- /**
186
- * The length of the list
187
- */
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", {
188
197
  get: function () {
189
- 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;
190
212
  },
191
213
  enumerable: false,
192
214
  configurable: true
193
215
  });
194
216
  /**
195
- * Convert any iterable to a new linked list
196
- * ```javascript
197
- * const array = [1, 2, 3];
198
- * const list = LinkedList.from(array);
199
- * ```
200
- * @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.
201
221
  */
202
222
  SinglyLinkedList.from = function (iterable) {
203
223
  return new (SinglyLinkedList.bind.apply(SinglyLinkedList, __spreadArray([void 0], __read(iterable), false)))();
204
224
  };
205
225
  /**
206
- * Get the node val at a specified index, zero based
207
- * ```ts
208
- * new LinkedList(1, 2, 3).get(0); // 1
209
- * ```
210
- * @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.
211
231
  */
212
232
  SinglyLinkedList.prototype.get = function (index) {
213
233
  var node = this.getNode(index);
214
234
  return node !== undefined ? node.val : undefined;
215
235
  };
216
236
  /**
217
- * Get the node at index, zero based
218
- * ```ts
219
- * new LinkedList(1, 2, 3).getNode(0);
220
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
221
- * ```
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.
222
241
  */
223
242
  SinglyLinkedList.prototype.getNode = function (index) {
224
- if (this.head === null || index < 0 || index >= this.length) {
243
+ if (this.head === null || index < 0 || index >= this.size) {
225
244
  return undefined;
226
245
  }
227
- var asc = index < this.length / 2;
228
- var stopAt = asc ? index : this.length - index - 1;
246
+ var asc = index < this.size / 2;
247
+ var stopAt = asc ? index : this.size - index - 1;
229
248
  var nextNode = asc ? 'next' : 'prev';
230
249
  var currentNode = asc ? this.head : this.tail;
231
250
  // TODO after no-non-null-assertion not ensure the logic
@@ -237,19 +256,21 @@ var SinglyLinkedList = /** @class */ (function () {
237
256
  return currentNode || undefined;
238
257
  };
239
258
  /**
240
- * Return the first node and its index in the list that
241
- * satisfies the testing function
242
- * ```ts
243
- * new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
244
- * // { node: SinglyLinkedListNode, index: 0 }
245
- * ```
246
- * @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`.
247
268
  */
248
- SinglyLinkedList.prototype.findNodeIndex = function (f) {
269
+ SinglyLinkedList.prototype.findNodeIndex = function (callbackFn) {
249
270
  var currentIndex = 0;
250
271
  var currentNode = this.head;
251
272
  while (currentNode) {
252
- if (f(currentNode.val, currentIndex, this)) {
273
+ if (callbackFn(currentNode.val, currentIndex, this)) {
253
274
  return {
254
275
  index: currentIndex,
255
276
  node: currentNode,
@@ -261,51 +282,40 @@ var SinglyLinkedList = /** @class */ (function () {
261
282
  return undefined;
262
283
  };
263
284
  /**
264
- * Returns the first node in the list that
265
- * satisfies the provided testing function. Otherwise undefined is returned.
266
- * ```ts
267
- * new LinkedList(1, 2, 3).findNode(val => val === 1);
268
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
269
- * ```
270
- * @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`.
271
290
  */
272
- SinglyLinkedList.prototype.findNode = function (f) {
273
- var nodeIndex = this.findNodeIndex(f);
291
+ SinglyLinkedList.prototype.findNode = function (callbackFn) {
292
+ var nodeIndex = this.findNodeIndex(callbackFn);
274
293
  return nodeIndex !== undefined ? nodeIndex.node : undefined;
275
294
  };
276
295
  /**
277
- * Returns the value of the first element in the list that
278
- * satisfies the provided testing function. Otherwise undefined is returned.
279
- * ```ts
280
- * new LinkedList(1, 2, 3).find(val => val === 1); // 1
281
- * ```
282
- * @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`.
283
302
  */
284
- SinglyLinkedList.prototype.find = function (f) {
285
- var nodeIndex = this.findNodeIndex(f);
303
+ SinglyLinkedList.prototype.find = function (callbackFn) {
304
+ var nodeIndex = this.findNodeIndex(callbackFn);
286
305
  return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
287
306
  };
288
307
  /**
289
- * Returns the index of the first node in the list that
290
- * satisfies the provided testing function. Ohterwise -1 is returned.
291
- * ```ts
292
- * new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
293
- * ```
294
- * @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.
295
313
  */
296
- SinglyLinkedList.prototype.findIndex = function (f) {
297
- var nodeIndex = this.findNodeIndex(f);
314
+ SinglyLinkedList.prototype.findIndex = function (callbackFn) {
315
+ var nodeIndex = this.findNodeIndex(callbackFn);
298
316
  return nodeIndex !== undefined ? nodeIndex.index : -1;
299
317
  };
300
- /**
301
- * Append one or any number of nodes to the end of the list.
302
- * This modifies the list in place and returns the list itself
303
- * to make this method chainable.
304
- * ```ts
305
- * new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
306
- * ```
307
- * @param args Data to be stored in the node, takes any number of arguments
308
- */
318
+ /* The above code is a comment in TypeScript. It is using the triple hash symbol ( */
309
319
  SinglyLinkedList.prototype.append = function () {
310
320
  var e_1, _a;
311
321
  var args = [];
@@ -336,11 +346,11 @@ var SinglyLinkedList = /** @class */ (function () {
336
346
  return this;
337
347
  };
338
348
  /**
339
- * Synonym for append
340
- * ```ts
341
- * new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4
342
- * ```
343
- * @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.
344
354
  */
345
355
  SinglyLinkedList.prototype.push = function () {
346
356
  var args = [];
@@ -348,15 +358,12 @@ var SinglyLinkedList = /** @class */ (function () {
348
358
  args[_i] = arguments[_i];
349
359
  }
350
360
  this.append.apply(this, __spreadArray([], __read(args), false));
351
- return this.length;
361
+ return this.size;
352
362
  };
353
363
  /**
354
- * Prepend any number of val arguments to the list. The
355
- * argument list is prepended as a block to reduce confusion:
356
- * ```javascript
357
- * new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]
358
- * ```
359
- * @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.
360
367
  */
361
368
  SinglyLinkedList.prototype.prepend = function () {
362
369
  var e_2, _a;
@@ -389,14 +396,12 @@ var SinglyLinkedList = /** @class */ (function () {
389
396
  return this;
390
397
  };
391
398
  /**
392
- * Insert a new node at a given index position. If index is
393
- * out of bounds, the node is appended, if index is negative
394
- * or 0, it will be prepended.
395
- * ```ts
396
- * new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
397
- * ```
398
- * @param index The index to insert the new node at
399
- * @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.
400
405
  */
401
406
  SinglyLinkedList.prototype.insertAt = function (index, val) {
402
407
  if (this.head === null) {
@@ -415,13 +420,11 @@ var SinglyLinkedList = /** @class */ (function () {
415
420
  return this;
416
421
  };
417
422
  /**
418
- * Remove the specified node from the list and return the removed
419
- * node afterwards.
420
- * ```ts
421
- * const list = new LinkedList(1, 2, 3);
422
- * list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
423
- * ```
424
- * @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.
425
428
  */
426
429
  SinglyLinkedList.prototype.removeNode = function (node) {
427
430
  if (node.list !== this) {
@@ -446,24 +449,23 @@ var SinglyLinkedList = /** @class */ (function () {
446
449
  return node;
447
450
  };
448
451
  /**
449
- * Remove the node at the specified index
450
- * ```ts
451
- * new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }
452
- * ```
453
- * @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`.
454
457
  */
455
458
  SinglyLinkedList.prototype.removeAt = function (index) {
456
459
  var node = this.getNode(index);
457
460
  return node !== undefined ? this.removeNode(node) : undefined;
458
461
  };
459
462
  /**
460
- * Insert a new node before the reference node
461
- * ```ts
462
- * const list = new LinkedList(1, 3);
463
- * list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3
464
- * ```
465
- * @param referenceNode The node reference
466
- * @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.
467
469
  */
468
470
  SinglyLinkedList.prototype.insertBefore = function (referenceNode, val) {
469
471
  var node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
@@ -478,17 +480,18 @@ var SinglyLinkedList = /** @class */ (function () {
478
480
  return this;
479
481
  };
480
482
  /**
481
- * Sorts the linked list using the provided compare function
482
- * @param compare A function used to compare the val of two nodes. It should return
483
- * a boolean. True will insert a before b, false will insert b before a.
484
- * (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1,
485
- * 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.
486
489
  */
487
490
  SinglyLinkedList.prototype.sort = function (compare) {
488
491
  if (this.head === null || this.tail === null) {
489
492
  return this;
490
493
  }
491
- if (this.length < 2) {
494
+ if (this.size < 2) {
492
495
  return this;
493
496
  }
494
497
  var quicksort = function (start, end) {
@@ -529,13 +532,11 @@ var SinglyLinkedList = /** @class */ (function () {
529
532
  return this;
530
533
  };
531
534
  /**
532
- * Insert a new node after this one
533
- * ```ts
534
- * const list = new LinkedList(2, 3);
535
- * list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3
536
- * ```
537
- * @param referenceNode The reference node
538
- * @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.
539
540
  */
540
541
  SinglyLinkedList.prototype.insertAfter = function (referenceNode, val) {
541
542
  var node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
@@ -550,35 +551,23 @@ var SinglyLinkedList = /** @class */ (function () {
550
551
  return this;
551
552
  };
552
553
  /**
553
- * Remove the first node from the list and return the val of the removed node
554
- * or undefined
555
- * ```ts
556
- * new LinkedList(1, 2, 3).shift(); // 1
557
- * ```
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`.
558
556
  */
559
557
  SinglyLinkedList.prototype.shift = function () {
560
558
  return this.removeFromAnyEnd(this.head);
561
559
  };
562
560
  /**
563
- * Remove the last node from the list and return the val of the removed node
564
- * or undefined if the list was empty
565
- * ```ts
566
- * new LinkedList(1, 2, 3).pop(); // 3
567
- * ```
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`.
568
563
  */
569
564
  SinglyLinkedList.prototype.pop = function () {
570
565
  return this.removeFromAnyEnd(this.tail);
571
566
  };
572
567
  /**
573
- * Merge the current list with another. Both lists will be
574
- * equal after merging.
575
- * ```ts
576
- * const list = new LinkedList(1, 2);
577
- * const otherList = new LinkedList(3);
578
- * list.merge(otherList);
579
- * (list === otherList); // true
580
- * ```
581
- * @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.
582
571
  */
583
572
  SinglyLinkedList.prototype.merge = function (list) {
584
573
  if (this.tail !== null) {
@@ -595,11 +584,8 @@ var SinglyLinkedList = /** @class */ (function () {
595
584
  list.tail = this.tail;
596
585
  };
597
586
  /**
598
- * Removes all nodes from a list
599
- *
600
- * ```ts
601
- * list.clear();
602
- * ```
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.
603
589
  */
604
590
  SinglyLinkedList.prototype.clear = function () {
605
591
  this.head = null;
@@ -608,18 +594,15 @@ var SinglyLinkedList = /** @class */ (function () {
608
594
  return this;
609
595
  };
610
596
  /**
611
- * The slice() method returns a shallow copy of a
612
- * portion of a list into a new list object selected
613
- * from start to end (end not included).
614
- * The original list will not be modified.
615
- * ```ts
616
- * const list = new LinkedList(1, 2, 3, 4, 5);
617
- * const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
618
- * ```
619
- * @param start Start index
620
- * @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.
621
605
  */
622
- // eslint-disable-next-line @typescript-eslint/ban-types
623
606
  SinglyLinkedList.prototype.slice = function (start, end) {
624
607
  var list = new SinglyLinkedList();
625
608
  var finish = end;
@@ -627,7 +610,7 @@ var SinglyLinkedList = /** @class */ (function () {
627
610
  return list;
628
611
  }
629
612
  if (finish === undefined || finish < start) {
630
- finish = this.length;
613
+ finish = this.size;
631
614
  }
632
615
  var head = this.getNode(start);
633
616
  for (var i = 0; i < finish - start && head !== null && head !== undefined; i++) {
@@ -637,11 +620,8 @@ var SinglyLinkedList = /** @class */ (function () {
637
620
  return list;
638
621
  };
639
622
  /**
640
- * The reverse() function reverses the list in place and returns the list
641
- * itself.
642
- * ```ts
643
- * new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
644
- * ```
623
+ * The reverse() function reverses the order of nodes in a singly linked list.
624
+ * @returns The reverse() method is returning the reversed SinglyLinkedList.
645
625
  */
646
626
  SinglyLinkedList.prototype.reverse = function () {
647
627
  var currentNode = this.head;
@@ -657,75 +637,80 @@ var SinglyLinkedList = /** @class */ (function () {
657
637
  return this;
658
638
  };
659
639
  /**
660
- * The forEach() method executes a provided function once for each list node.
661
- * ```ts
662
- * new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
663
- * ```
664
- * @param f Function to execute for each element, taking up to three arguments.
665
- * @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.
666
647
  */
667
- SinglyLinkedList.prototype.forEach = function (f, reverse) {
648
+ SinglyLinkedList.prototype.forEach = function (callbackFn, reverse) {
668
649
  if (reverse === void 0) { reverse = false; }
669
- var currentIndex = reverse ? this.length - 1 : 0;
650
+ var currentIndex = reverse ? this.size - 1 : 0;
670
651
  var currentNode = reverse ? this.tail : this.head;
671
652
  var modifier = reverse ? -1 : 1;
672
653
  var nextNode = reverse ? 'prev' : 'next';
673
654
  while (currentNode) {
674
- f(currentNode.val, currentIndex, this);
655
+ callbackFn(currentNode.val, currentIndex, this);
675
656
  currentNode = currentNode[nextNode];
676
657
  currentIndex += modifier;
677
658
  }
678
659
  };
679
660
  /**
680
- * The map() method creates a new list with the results of
681
- * calling a provided function on every node in the calling list.
682
- * ```ts
683
- * new LinkedList(1, 2, 3).map(val => val + 10); // 11 <=> 12 <=> 13
684
- * ```
685
- * @param f Function that produces an node of the new list, taking up to three arguments
686
- * @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.
687
669
  */
688
- // eslint-disable-next-line @typescript-eslint/ban-types
689
- SinglyLinkedList.prototype.map = function (f, reverse) {
670
+ SinglyLinkedList.prototype.map = function (callbackFn, reverse) {
690
671
  var _this = this;
691
672
  if (reverse === void 0) { reverse = false; }
692
673
  var list = new SinglyLinkedList();
693
- 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);
694
675
  return list;
695
676
  };
696
677
  /**
697
- * The filter() method creates a new list with all nodes
698
- * that pass the test implemented by the provided function.
699
- * ```ts
700
- * new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
701
- * ```
702
- * @param f Function to test each node val in the list. Return true to keep the node
703
- * @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.
704
685
  */
705
- // eslint-disable-next-line @typescript-eslint/ban-types
706
- SinglyLinkedList.prototype.filter = function (f, reverse) {
686
+ SinglyLinkedList.prototype.filter = function (callbackFn, reverse) {
707
687
  var _this = this;
708
688
  if (reverse === void 0) { reverse = false; }
709
689
  var list = new SinglyLinkedList();
710
690
  this.forEach(function (val, index) {
711
- if (f(val, index, _this)) {
691
+ if (callbackFn(val, index, _this)) {
712
692
  list.append(val);
713
693
  }
714
694
  }, reverse);
715
695
  return list;
716
696
  };
717
697
  /**
718
- * Reduce over each node in the list
719
- * ```ts
720
- * new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
721
- * ```
722
- * @param f A reducer function
723
- * @param start An initial value
724
- * @returns The final state of the accumulator
725
- */
726
- 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) {
727
712
  if (reverse === void 0) { reverse = false; }
728
- var currentIndex = reverse ? this.length - 1 : 0;
713
+ var currentIndex = reverse ? this.size - 1 : 0;
729
714
  var modifier = reverse ? -1 : 1;
730
715
  var nextNode = reverse ? 'prev' : 'next';
731
716
  var currentElement = reverse ? this.tail : this.head;
@@ -741,38 +726,33 @@ var SinglyLinkedList = /** @class */ (function () {
741
726
  throw new TypeError('Reduce of empty LinkedList with no initial value');
742
727
  }
743
728
  while (currentElement) {
744
- result = f(result, currentElement.val, currentIndex, this);
729
+ result = callbackFn(result, currentElement.val, currentIndex, this);
745
730
  currentIndex += modifier;
746
731
  currentElement = currentElement[nextNode];
747
732
  }
748
733
  return result;
749
734
  };
750
735
  /**
751
- * Convert the linked list to an array
752
- * ```ts
753
- * new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
754
- * ```
736
+ * The toArray() function converts a NodeVal object into an array of NodeVal objects.
737
+ * @returns An array of NodeVal objects.
755
738
  */
756
739
  SinglyLinkedList.prototype.toArray = function () {
757
740
  return __spreadArray([], __read(this), false);
758
741
  };
759
742
  /**
760
- * Convert a linked list to string
761
- * ```ts
762
- * new LinkedList('one', 'two', 'three').toString(' <=> ') === 'one <=> two <=> three';
763
- * ```
764
- * @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.
765
749
  */
766
750
  SinglyLinkedList.prototype.toString = function (separator) {
767
751
  if (separator === void 0) { separator = ' '; }
768
752
  return this.reduce(function (s, val) { return "".concat(s).concat(separator).concat(val); });
769
753
  };
770
754
  /**
771
- * The iterator implementation
772
- * ```ts
773
- * const list = new LinkedList(1, 2, 3);
774
- * for (const val of list) { log(val); } // 1 2 3
775
- * ```
755
+ * The function is an iterator that returns the values of each node in a linked list.
776
756
  */
777
757
  SinglyLinkedList.prototype[Symbol.iterator] = function () {
778
758
  var element;
@@ -792,7 +772,12 @@ var SinglyLinkedList = /** @class */ (function () {
792
772
  }
793
773
  });
794
774
  };
795
- /** 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
+ */
796
781
  SinglyLinkedList.prototype.removeFromAnyEnd = function (node) {
797
782
  return node !== null ? this.removeNode(node).val : undefined;
798
783
  };