data-structure-typed 1.16.1 → 1.17.3

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 (100) hide show
  1. package/README.md +1 -1
  2. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +178 -70
  3. package/dist/data-structures/linked-list/doubly-linked-list.js +475 -244
  4. package/dist/data-structures/linked-list/index.d.ts +1 -0
  5. package/dist/data-structures/linked-list/index.js +1 -0
  6. package/dist/data-structures/linked-list/singly-linked-list.d.ts +111 -248
  7. package/dist/data-structures/linked-list/singly-linked-list.js +338 -619
  8. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  9. package/dist/data-structures/linked-list/skip-linked-list.js +7 -0
  10. package/dist/data-structures/matrix/vector2d.js +2 -2
  11. package/dist/data-structures/types/binary-tree.d.ts +0 -5
  12. package/docs/assets/search.js +1 -1
  13. package/docs/classes/AVLTree.html +94 -94
  14. package/docs/classes/AVLTreeNode.html +37 -37
  15. package/docs/classes/AaTree.html +2 -2
  16. package/docs/classes/AbstractEdge.html +12 -12
  17. package/docs/classes/AbstractGraph.html +30 -30
  18. package/docs/classes/AbstractVertex.html +7 -7
  19. package/docs/classes/ArrayDeque.html +15 -15
  20. package/docs/classes/BST.html +87 -87
  21. package/docs/classes/BSTNode.html +37 -37
  22. package/docs/classes/BTree.html +2 -2
  23. package/docs/classes/BinaryIndexedTree.html +8 -8
  24. package/docs/classes/BinaryTree.html +83 -83
  25. package/docs/classes/BinaryTreeNode.html +37 -37
  26. package/docs/classes/Character.html +5 -5
  27. package/docs/classes/CoordinateMap.html +11 -11
  28. package/docs/classes/CoordinateSet.html +10 -10
  29. package/docs/classes/Deque.html +564 -356
  30. package/docs/classes/DirectedEdge.html +18 -18
  31. package/docs/classes/DirectedGraph.html +42 -42
  32. package/docs/classes/DirectedVertex.html +7 -7
  33. package/docs/classes/DoublyLinkedList.html +546 -313
  34. package/docs/classes/DoublyLinkedListNode.html +53 -36
  35. package/docs/classes/Heap.html +21 -21
  36. package/docs/classes/HeapItem.html +9 -9
  37. package/docs/classes/Matrix2D.html +17 -17
  38. package/docs/classes/MatrixNTI2D.html +5 -5
  39. package/docs/classes/MaxHeap.html +21 -21
  40. package/docs/classes/MaxPriorityQueue.html +35 -35
  41. package/docs/classes/MinHeap.html +21 -21
  42. package/docs/classes/MinPriorityQueue.html +35 -35
  43. package/docs/classes/Navigator.html +11 -11
  44. package/docs/classes/ObjectDeque.html +26 -26
  45. package/docs/classes/PriorityQueue.html +33 -33
  46. package/docs/classes/Queue.html +15 -15
  47. package/docs/classes/RBTree.html +2 -2
  48. package/docs/classes/SegmentTree.html +13 -13
  49. package/docs/classes/SegmentTreeNode.html +27 -27
  50. package/docs/classes/SinglyLinkedList.html +277 -606
  51. package/docs/classes/SinglyLinkedListNode.html +40 -167
  52. package/docs/classes/SkipLinkedList.html +150 -0
  53. package/docs/classes/SplayTree.html +2 -2
  54. package/docs/classes/Stack.html +13 -13
  55. package/docs/classes/TreeMultiSet.html +87 -87
  56. package/docs/classes/TreeNode.html +9 -9
  57. package/docs/classes/Trie.html +14 -14
  58. package/docs/classes/TrieNode.html +12 -12
  59. package/docs/classes/TwoThreeTree.html +2 -2
  60. package/docs/classes/UndirectedEdge.html +14 -14
  61. package/docs/classes/UndirectedGraph.html +36 -36
  62. package/docs/classes/UndirectedVertex.html +7 -7
  63. package/docs/classes/Vector2D.html +29 -29
  64. package/docs/enums/CP.html +5 -5
  65. package/docs/enums/FamilyPosition.html +5 -5
  66. package/docs/enums/LoopType.html +4 -4
  67. package/docs/index.html +3 -3
  68. package/docs/interfaces/AVLTreeDeleted.html +4 -4
  69. package/docs/interfaces/HeapOptions.html +3 -3
  70. package/docs/interfaces/IDirectedGraph.html +8 -8
  71. package/docs/interfaces/IGraph.html +20 -20
  72. package/docs/interfaces/NavigatorParams.html +6 -6
  73. package/docs/interfaces/PriorityQueueOptions.html +5 -5
  74. package/docs/modules.html +2 -2
  75. package/docs/types/BSTComparator.html +2 -2
  76. package/docs/types/BSTDeletedResult.html +2 -2
  77. package/docs/types/BinaryTreeDeleted.html +2 -2
  78. package/docs/types/BinaryTreeNodeId.html +2 -2
  79. package/docs/types/BinaryTreeNodePropertyName.html +2 -2
  80. package/docs/types/DFSOrderPattern.html +2 -2
  81. package/docs/types/DijkstraResult.html +2 -2
  82. package/docs/types/Direction.html +2 -2
  83. package/docs/types/DoublyLinkedListGetBy.html +2 -2
  84. package/docs/types/NodeOrPropertyName.html +2 -2
  85. package/docs/types/PriorityQueueComparator.html +2 -2
  86. package/docs/types/PriorityQueueDFSOrderPattern.html +2 -2
  87. package/docs/types/ResultByProperty.html +2 -2
  88. package/docs/types/ResultsByProperty.html +2 -2
  89. package/docs/types/SegmentTreeNodeVal.html +2 -2
  90. package/docs/types/SpecifyOptional.html +2 -2
  91. package/docs/types/Thunk.html +2 -2
  92. package/docs/types/ToThunkFn.html +2 -2
  93. package/docs/types/TopologicalStatus.html +2 -2
  94. package/docs/types/TreeMultiSetDeletedResult.html +2 -2
  95. package/docs/types/TrlAsyncFn.html +2 -2
  96. package/docs/types/TrlFn.html +2 -2
  97. package/docs/types/Turning.html +2 -2
  98. package/docs/types/VertexId.html +2 -2
  99. package/package.json +1 -3
  100. package/docs/interfaces/BinaryTreeNodeObj.html +0 -168
@@ -1,9 +1,48 @@
1
1
  "use strict";
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
2
29
  Object.defineProperty(exports, "__esModule", { value: true });
3
30
  exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
31
+ /**
32
+ * data-structure-typed
33
+ *
34
+ * @author Tyler Zeng
35
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
36
+ * @license MIT License
37
+ */
4
38
  var DoublyLinkedListNode = /** @class */ (function () {
5
- function DoublyLinkedListNode(nodeValue) {
6
- this._val = nodeValue;
39
+ /**
40
+ * The constructor function initializes the value, next, and previous properties of an object.
41
+ * @param {T} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
42
+ * is defined as a generic type "T".
43
+ */
44
+ function DoublyLinkedListNode(val) {
45
+ this._val = val;
7
46
  this._next = null;
8
47
  this._prev = null;
9
48
  }
@@ -11,8 +50,8 @@ var DoublyLinkedListNode = /** @class */ (function () {
11
50
  get: function () {
12
51
  return this._val;
13
52
  },
14
- set: function (v) {
15
- this._val = v;
53
+ set: function (value) {
54
+ this._val = value;
16
55
  },
17
56
  enumerable: false,
18
57
  configurable: true
@@ -21,8 +60,8 @@ var DoublyLinkedListNode = /** @class */ (function () {
21
60
  get: function () {
22
61
  return this._next;
23
62
  },
24
- set: function (v) {
25
- this._next = v;
63
+ set: function (value) {
64
+ this._next = value;
26
65
  },
27
66
  enumerable: false,
28
67
  configurable: true
@@ -31,8 +70,8 @@ var DoublyLinkedListNode = /** @class */ (function () {
31
70
  get: function () {
32
71
  return this._prev;
33
72
  },
34
- set: function (v) {
35
- this._prev = v;
73
+ set: function (value) {
74
+ this._prev = value;
36
75
  },
37
76
  enumerable: false,
38
77
  configurable: true
@@ -41,325 +80,517 @@ var DoublyLinkedListNode = /** @class */ (function () {
41
80
  }());
42
81
  exports.DoublyLinkedListNode = DoublyLinkedListNode;
43
82
  var DoublyLinkedList = /** @class */ (function () {
83
+ /**
84
+ * The constructor initializes the linked list with an empty head, tail, and length.
85
+ */
44
86
  function DoublyLinkedList() {
45
- this._first = null;
46
- this._last = null;
47
- this._size = 0;
87
+ this._head = null;
88
+ this._tail = null;
89
+ this._length = 0;
48
90
  }
49
- Object.defineProperty(DoublyLinkedList.prototype, "first", {
91
+ Object.defineProperty(DoublyLinkedList.prototype, "head", {
50
92
  get: function () {
51
- return this._first;
93
+ return this._head;
52
94
  },
53
- set: function (v) {
54
- this._first = v;
95
+ set: function (value) {
96
+ this._head = value;
55
97
  },
56
98
  enumerable: false,
57
99
  configurable: true
58
100
  });
59
- Object.defineProperty(DoublyLinkedList.prototype, "last", {
101
+ Object.defineProperty(DoublyLinkedList.prototype, "tail", {
60
102
  get: function () {
61
- return this._last;
103
+ return this._tail;
62
104
  },
63
- set: function (v) {
64
- this._last = v;
105
+ set: function (value) {
106
+ this._tail = value;
65
107
  },
66
108
  enumerable: false,
67
109
  configurable: true
68
110
  });
69
- Object.defineProperty(DoublyLinkedList.prototype, "size", {
111
+ Object.defineProperty(DoublyLinkedList.prototype, "length", {
70
112
  get: function () {
71
- return this._size;
113
+ return this._length;
72
114
  },
73
- set: function (v) {
74
- this._size = v;
115
+ set: function (value) {
116
+ this._length = value;
75
117
  },
76
118
  enumerable: false,
77
119
  configurable: true
78
120
  });
79
121
  /**
80
- * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
122
+ * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
123
+ * given array.
124
+ * @param {T[]} data - The `data` parameter is an array of elements of type `T`.
125
+ * @returns The `fromArray` function returns a DoublyLinkedList object.
81
126
  */
82
- DoublyLinkedList.prototype.getFirst = function () {
83
- return this._first;
127
+ DoublyLinkedList.fromArray = function (data) {
128
+ var e_1, _a;
129
+ var doublyLinkedList = new DoublyLinkedList();
130
+ try {
131
+ for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
132
+ var item = data_1_1.value;
133
+ doublyLinkedList.push(item);
134
+ }
135
+ }
136
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
137
+ finally {
138
+ try {
139
+ if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
140
+ }
141
+ finally { if (e_1) throw e_1.error; }
142
+ }
143
+ return doublyLinkedList;
144
+ };
145
+ DoublyLinkedList.prototype.getLength = function () {
146
+ return this._length;
84
147
  };
85
148
  /**
86
- * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
149
+ * The push function adds a new node with the given value to the end of the doubly linked list.
150
+ * @param {T} val - The value to be added to the linked list.
87
151
  */
88
- DoublyLinkedList.prototype.getLast = function () {
89
- return this._last;
152
+ DoublyLinkedList.prototype.push = function (val) {
153
+ var newNode = new DoublyLinkedListNode(val);
154
+ if (!this.head) {
155
+ this.head = newNode;
156
+ this.tail = newNode;
157
+ }
158
+ else {
159
+ newNode.prev = this.tail;
160
+ this.tail.next = newNode;
161
+ this.tail = newNode;
162
+ }
163
+ this.length++;
90
164
  };
91
165
  /**
92
- * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
166
+ * The `pop()` function removes and returns the value of the last node in a doubly linked list.
167
+ * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
168
+ * list is empty, it returns null.
93
169
  */
94
- DoublyLinkedList.prototype.getSize = function () {
95
- return this._size;
170
+ DoublyLinkedList.prototype.pop = function () {
171
+ if (!this.tail)
172
+ return null;
173
+ var removedNode = this.tail;
174
+ if (this.head === this.tail) {
175
+ this.head = null;
176
+ this.tail = null;
177
+ }
178
+ else {
179
+ this.tail = removedNode.prev;
180
+ this.tail.next = null;
181
+ }
182
+ this.length--;
183
+ return removedNode.val;
96
184
  };
97
185
  /**
98
- * The function adds a new node with a given value to the beginning of a doubly linked list.
99
- * @param {T} val - The `val` parameter represents the value of the element that you want to add to the beginning of
100
- * the doubly linked list.
101
- * @returns A boolean value is being returned.
186
+ * The `shift()` function removes and returns the value of the first node in a doubly linked list.
187
+ * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
188
+ * list.
102
189
  */
103
- DoublyLinkedList.prototype.addFirst = function (val) {
104
- var newNode = new DoublyLinkedListNode(val);
105
- if (this._size === 0) {
106
- this._first = newNode;
107
- this._last = newNode;
190
+ DoublyLinkedList.prototype.shift = function () {
191
+ if (!this.head)
192
+ return null;
193
+ var removedNode = this.head;
194
+ if (this.head === this.tail) {
195
+ this.head = null;
196
+ this.tail = null;
108
197
  }
109
198
  else {
110
- if (this._first)
111
- this._first.prev = newNode;
112
- newNode.next = this._first;
113
- this._first = newNode;
199
+ this.head = removedNode.next;
200
+ this.head.prev = null;
114
201
  }
115
- this._size++;
116
- return true;
202
+ this.length--;
203
+ return removedNode.val;
117
204
  };
118
205
  /**
119
- * The function adds a new node with a given value to the end of a doubly linked list.
120
- * @param {T} val - The `val` parameter represents the value of the element that you want to add to the end of the
206
+ * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
207
+ * @param {T} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
121
208
  * doubly linked list.
122
- * @returns a boolean value, which is always true.
123
209
  */
124
- DoublyLinkedList.prototype.addLast = function (val) {
210
+ DoublyLinkedList.prototype.unshift = function (val) {
125
211
  var newNode = new DoublyLinkedListNode(val);
126
- if (this._size === 0) {
127
- this._first = newNode;
128
- this._last = newNode;
212
+ if (!this.head) {
213
+ this.head = newNode;
214
+ this.tail = newNode;
129
215
  }
130
216
  else {
131
- if (this._last)
132
- this._last.next = newNode;
133
- newNode.prev = this._last;
134
- this._last = newNode;
217
+ newNode.next = this.head;
218
+ this.head.prev = newNode;
219
+ this.head = newNode;
135
220
  }
136
- this._size++;
137
- return true;
221
+ this.length++;
138
222
  };
139
223
  /**
140
- * The `peekFirst` function returns the first node or value in a doubly linked list, depending on the specified
141
- * parameter.
142
- * @param {DoublyLinkedListGetBy} [by] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy. It
143
- * is used to specify whether to return the first node, the value of the first node, or the first node itself.
144
- * @returns The method `peekFirst` returns either the first node of the doubly linked list (`DoublyLinkedListNode<T>`),
145
- * the value of the first node (`T`), or `null` depending on the value of the `by` parameter.
224
+ * The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
225
+ * @param {number} index - The index parameter is a number that represents the position of the element we want to
226
+ * retrieve from the list.
227
+ * @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
228
+ * or the linked list is empty, it will return null.
146
229
  */
147
- DoublyLinkedList.prototype.peekFirst = function (by) {
148
- var _a, _b, _c, _d, _e;
149
- switch (by) {
150
- case 'node':
151
- return (_a = this._first) !== null && _a !== void 0 ? _a : null;
152
- case 'val':
153
- return (_c = (_b = this._first) === null || _b === void 0 ? void 0 : _b.val) !== null && _c !== void 0 ? _c : null;
154
- default:
155
- return (_e = (_d = this._first) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
230
+ DoublyLinkedList.prototype.getAt = function (index) {
231
+ if (index < 0 || index >= this.length)
232
+ return null;
233
+ var current = this.head;
234
+ for (var i = 0; i < index; i++) {
235
+ current = current.next;
156
236
  }
237
+ return current.val;
157
238
  };
158
239
  /**
159
- * The `peekLast` function returns the last node or value in a doubly linked list.
160
- * @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
161
- * It specifies whether to return the last node, the value of the last node, or both. The default value is 'val', which
162
- * means that if no value is provided for the "by" parameter, the method
163
- * @returns The method `peekLast` returns the last node, value, or null based on the specified `by` parameter.
240
+ * The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
241
+ * range.
242
+ * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
243
+ * retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
244
+ * @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<T>` object if the index is within the
245
+ * valid range of the linked list, otherwise it returns `null`.
164
246
  */
165
- DoublyLinkedList.prototype.peekLast = function (by) {
166
- var _a, _b, _c, _d, _e;
167
- if (by === void 0) { by = 'val'; }
168
- switch (by) {
169
- case 'node':
170
- return (_a = this._last) !== null && _a !== void 0 ? _a : null;
171
- case 'val':
172
- return (_c = (_b = this._last) === null || _b === void 0 ? void 0 : _b.val) !== null && _c !== void 0 ? _c : null;
173
- default:
174
- return (_e = (_d = this._last) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
247
+ DoublyLinkedList.prototype.getNodeAt = function (index) {
248
+ if (index < 0 || index >= this.length)
249
+ return null;
250
+ var current = this.head;
251
+ for (var i = 0; i < index; i++) {
252
+ current = current.next;
175
253
  }
254
+ return current;
176
255
  };
177
256
  /**
178
- * The function `pollFirst` removes and returns the first element of a doubly linked list, either as a node or its
179
- * value, depending on the specified parameter.
180
- * @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
181
- * It specifies the criteria by which the first element should be retrieved from the doubly linked list. The default
182
- * value is 'val', which means the first element will be retrieved by its value. Other possible values for "by
183
- * @returns The method `pollFirst` returns either the value of the first node in the doubly linked list, the first node
184
- * itself, or null if the list is empty. The specific return type depends on the value of the `by` parameter. If `by`
185
- * is set to 'node', the method returns the first node. If `by` is set to 'val', the method returns the value
257
+ * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
258
+ * node if found, otherwise it returns null.
259
+ * @param {T} val - The `val` parameter is the value that we want to search for in the doubly linked list.
260
+ * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<T>` if a node with the specified value `val`
261
+ * is found in the linked list. If no such node is found, it returns `null`.
186
262
  */
187
- DoublyLinkedList.prototype.pollFirst = function (by) {
188
- var _a, _b, _c;
189
- if (by === void 0) { by = 'val'; }
190
- if (this._size === 0)
191
- return null;
192
- var oldHead = this._first;
193
- if (this._size === 1) {
194
- this._first = null;
195
- this._last = null;
263
+ DoublyLinkedList.prototype.findNode = function (val) {
264
+ var current = this.head;
265
+ while (current) {
266
+ if (current.val === val) {
267
+ return current;
268
+ }
269
+ current = current.next;
196
270
  }
197
- else {
198
- this._first = (_a = oldHead === null || oldHead === void 0 ? void 0 : oldHead.next) !== null && _a !== void 0 ? _a : null;
199
- if (this._first)
200
- this._first.prev = null;
201
- if (oldHead)
202
- oldHead.next = null;
203
- }
204
- this._size--;
205
- switch (by) {
206
- case 'node':
207
- return oldHead !== null && oldHead !== void 0 ? oldHead : null;
208
- case 'val':
209
- return (_b = oldHead === null || oldHead === void 0 ? void 0 : oldHead.val) !== null && _b !== void 0 ? _b : null;
210
- default:
211
- return (_c = oldHead === null || oldHead === void 0 ? void 0 : oldHead.val) !== null && _c !== void 0 ? _c : null;
271
+ return null;
272
+ };
273
+ /**
274
+ * The `insert` function inserts a value at a specified index in a doubly linked list.
275
+ * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
276
+ * DoublyLinkedList. It is of type number.
277
+ * @param {T} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
278
+ * specified index.
279
+ * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
280
+ * if the index is out of bounds.
281
+ */
282
+ DoublyLinkedList.prototype.insertAt = function (index, val) {
283
+ if (index < 0 || index > this.length)
284
+ return false;
285
+ if (index === 0) {
286
+ this.unshift(val);
287
+ return true;
212
288
  }
289
+ if (index === this.length) {
290
+ this.push(val);
291
+ return true;
292
+ }
293
+ var newNode = new DoublyLinkedListNode(val);
294
+ var prevNode = this.getNodeAt(index - 1);
295
+ var nextNode = prevNode.next;
296
+ newNode.prev = prevNode;
297
+ newNode.next = nextNode;
298
+ prevNode.next = newNode;
299
+ nextNode.prev = newNode;
300
+ this.length++;
301
+ return true;
213
302
  };
214
303
  /**
215
- * The function `pollLast` removes and returns the last element in a doubly linked list, either as a node or its value,
216
- * depending on the specified parameter.
217
- * @param {DoublyLinkedListGetBy} [by=val] - The parameter "by" is of type DoublyLinkedListGetBy, which is an enum that
218
- * can have two possible values: 'node' or 'val'. It determines the type of value that will be returned by the pollLast
219
- * method. If 'node' is specified, the method will return the
220
- * @returns The method `pollLast` returns either a `DoublyLinkedListNode<T>`, the value of the node (`T`), or `null`.
221
- * The specific type that is returned depends on the value of the `by` parameter. If `by` is set to `'node'`, then a
222
- * `DoublyLinkedListNode<T>` is returned. If `by` is set to `'
304
+ * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
305
+ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
306
+ * data structure. It is of type number.
307
+ * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
308
+ * bounds.
223
309
  */
224
- DoublyLinkedList.prototype.pollLast = function (by) {
225
- var _a, _b, _c;
226
- if (by === void 0) { by = 'val'; }
227
- if (this._size === 0)
310
+ DoublyLinkedList.prototype.deleteAt = function (index) {
311
+ if (index < 0 || index >= this.length)
228
312
  return null;
229
- var polled = this._last;
230
- if (this._size === 1) {
231
- this._first = null;
232
- this._last = null;
313
+ if (index === 0)
314
+ return this.shift();
315
+ if (index === this.length - 1)
316
+ return this.pop();
317
+ var removedNode = this.getNodeAt(index);
318
+ var prevNode = removedNode.prev;
319
+ var nextNode = removedNode.next;
320
+ prevNode.next = nextNode;
321
+ nextNode.prev = prevNode;
322
+ this.length--;
323
+ return removedNode.val;
324
+ };
325
+ /**
326
+ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
327
+ * @param {T | DoublyLinkedListNode<T>} valOrNode - The `valOrNode` parameter can accept either a value of type `T` or
328
+ * a `DoublyLinkedListNode<T>` object.
329
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
330
+ * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
331
+ */
332
+ DoublyLinkedList.prototype.delete = function (valOrNode) {
333
+ var node;
334
+ if (valOrNode instanceof (DoublyLinkedListNode)) {
335
+ node = valOrNode;
233
336
  }
234
337
  else {
235
- this._last = (_a = polled === null || polled === void 0 ? void 0 : polled.prev) !== null && _a !== void 0 ? _a : null;
236
- if (this._last)
237
- this._last.next = null;
238
- if (polled)
239
- polled.prev = null;
240
- }
241
- this._size--;
242
- switch (by) {
243
- case 'node':
244
- return polled !== null && polled !== void 0 ? polled : null;
245
- case 'val':
246
- return (_b = polled === null || polled === void 0 ? void 0 : polled.val) !== null && _b !== void 0 ? _b : null;
247
- default:
248
- return (_c = polled === null || polled === void 0 ? void 0 : polled.val) !== null && _c !== void 0 ? _c : null;
338
+ node = this.findNode(valOrNode);
339
+ }
340
+ if (node) {
341
+ if (node === this.head) {
342
+ this.shift();
343
+ }
344
+ else if (node === this.tail) {
345
+ this.pop();
346
+ }
347
+ else {
348
+ var prevNode = node.prev;
349
+ var nextNode = node.next;
350
+ prevNode.next = nextNode;
351
+ nextNode.prev = prevNode;
352
+ this.length--;
353
+ }
354
+ return true;
249
355
  }
356
+ return false;
250
357
  };
251
358
  /**
252
- * Returns the node at the specified index of the linked list.
253
- * If index = 0; first element in the list is returned.
254
- * If index = 3; fourth element in the list is returned.
255
- * @param index Index of the node to be retrieved
256
- * @param by Return value type
359
+ * The `toArray` function converts a linked list into an array.
360
+ * @returns The `toArray()` method is returning an array of type `T[]`.
257
361
  */
258
- DoublyLinkedList.prototype.get = function (index, by) {
259
- var _a, _b;
260
- if (by === void 0) { by = 'val'; }
261
- if (index < 0 || index >= this._size)
262
- return null;
263
- var count, current;
264
- if (index <= this._size / 2) {
265
- count = 0;
266
- current = this._first;
267
- while (count !== index) {
268
- current = current === null || current === void 0 ? void 0 : current.next;
269
- count++;
362
+ DoublyLinkedList.prototype.toArray = function () {
363
+ var array = [];
364
+ var current = this.head;
365
+ while (current) {
366
+ array.push(current.val);
367
+ current = current.next;
368
+ }
369
+ return array;
370
+ };
371
+ /**
372
+ * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
373
+ */
374
+ DoublyLinkedList.prototype.clear = function () {
375
+ this._head = null;
376
+ this._tail = null;
377
+ this._length = 0;
378
+ };
379
+ /**
380
+ * The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
381
+ * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
382
+ * function is used to determine whether a particular value in the linked list satisfies a certain condition.
383
+ * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
384
+ * the callback function. If no element satisfies the condition, it returns `null`.
385
+ */
386
+ DoublyLinkedList.prototype.find = function (callback) {
387
+ var current = this.head;
388
+ while (current) {
389
+ if (callback(current.val)) {
390
+ return current.val;
270
391
  }
392
+ current = current.next;
271
393
  }
272
- else {
273
- count = this._size - 1;
274
- current = this._last;
275
- while (count !== index) {
276
- current = current === null || current === void 0 ? void 0 : current.prev;
277
- count--;
394
+ return null;
395
+ };
396
+ /**
397
+ * The function returns the index of the first occurrence of a given value in a linked list.
398
+ * @param {T} val - The parameter `val` is of type `T`, which means it can be any data type. It represents the value
399
+ * that we are searching for in the linked list.
400
+ * @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
401
+ * list. If the value is not found, it returns -1.
402
+ */
403
+ DoublyLinkedList.prototype.indexOf = function (val) {
404
+ var index = 0;
405
+ var current = this.head;
406
+ while (current) {
407
+ if (current.val === val) {
408
+ return index;
278
409
  }
410
+ index++;
411
+ current = current.next;
279
412
  }
280
- switch (by) {
281
- case 'node':
282
- return current !== null && current !== void 0 ? current : null;
283
- case 'val':
284
- return (_a = current === null || current === void 0 ? void 0 : current.val) !== null && _a !== void 0 ? _a : null;
285
- default:
286
- return (_b = current === null || current === void 0 ? void 0 : current.val) !== null && _b !== void 0 ? _b : null;
413
+ return -1;
414
+ };
415
+ /**
416
+ * The `findLast` function iterates through a linked list from the last node to the first node and returns the last
417
+ * value that satisfies the given callback function, or null if no value satisfies the callback.
418
+ * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
419
+ * function is used to determine whether a given value satisfies a certain condition.
420
+ * @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
421
+ * the callback function. If no value satisfies the condition, it returns `null`.
422
+ */
423
+ DoublyLinkedList.prototype.findLast = function (callback) {
424
+ var current = this.tail;
425
+ while (current) {
426
+ if (callback(current.val)) {
427
+ return current.val;
428
+ }
429
+ current = current.prev;
287
430
  }
431
+ return null;
288
432
  };
289
433
  /**
290
- * Updates the value of the node at the specified index.
291
- * If index = 0; Value of the first element in the list is updated.
292
- * If index = 3; Value of the fourth element in the list is updated.
293
- * @param index Index of the node to be updated
294
- * @param val New value of the node
434
+ * The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
435
+ * @returns The `toArrayReverse()` function returns an array of type `T[]`.
295
436
  */
296
- DoublyLinkedList.prototype.set = function (index, val) {
297
- var foundNode = this.get(index, 'node');
298
- if (foundNode !== null) {
299
- foundNode.val = val;
300
- return true;
437
+ DoublyLinkedList.prototype.toArrayReverse = function () {
438
+ var array = [];
439
+ var current = this.tail;
440
+ while (current) {
441
+ array.push(current.val);
442
+ current = current.prev;
301
443
  }
302
- return false;
444
+ return array;
303
445
  };
304
- DoublyLinkedList.prototype.isEmpty = function () {
305
- return this._size === 0;
446
+ /**
447
+ * The `reverse` function reverses the order of the elements in a doubly linked list.
448
+ */
449
+ DoublyLinkedList.prototype.reverse = function () {
450
+ var _a, _b;
451
+ var current = this.head;
452
+ _a = __read([this.tail, this.head], 2), this.head = _a[0], this.tail = _a[1];
453
+ while (current) {
454
+ var next = current.next;
455
+ _b = __read([current.next, current.prev], 2), current.prev = _b[0], current.next = _b[1];
456
+ current = next;
457
+ }
306
458
  };
307
- // --- start extra methods ---
308
459
  /**
309
- * Inserts a new node at the specified index.
310
- * @param index Index at which the new node has to be inserted
311
- * @param val Value of the new node to be inserted
460
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
461
+ * @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
462
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
463
+ * current node in the linked list.
312
464
  */
313
- DoublyLinkedList.prototype.insert = function (index, val) {
314
- if (index < 0 || index > this._size)
315
- return false;
316
- if (index === 0)
317
- return !!this.addFirst(val);
318
- if (index === this._size)
319
- return !!this.addLast(val);
320
- var newNode = new DoublyLinkedListNode(val);
321
- var prevNode = this.get(index - 1, 'node');
322
- var nextNode = prevNode === null || prevNode === void 0 ? void 0 : prevNode.next;
323
- if (prevNode)
324
- prevNode.next = newNode;
325
- newNode.prev = prevNode;
326
- newNode.next = nextNode !== null && nextNode !== void 0 ? nextNode : null;
327
- if (nextNode)
328
- nextNode.prev = newNode;
329
- this._size++;
330
- return true;
465
+ DoublyLinkedList.prototype.forEach = function (callback) {
466
+ var current = this.head;
467
+ var index = 0;
468
+ while (current) {
469
+ callback(current.val, index);
470
+ current = current.next;
471
+ index++;
472
+ }
331
473
  };
332
474
  /**
333
- * The `remove` function removes an element at a specified index from a data structure, updating the links between
334
- * nodes accordingly.
335
- * @param {number} index - The index parameter represents the position of the element to be removed in the data
336
- * structure. It is of type number.
337
- * @returns The `remove` method returns the value of the removed element (`T`) if the removal is successful, or `null`
338
- * if the index is out of bounds.
475
+ * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
476
+ * DoublyLinkedList with the transformed values.
477
+ * @param callback - The callback parameter is a function that takes a value of type T (the type of values stored in
478
+ * the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
479
+ * DoublyLinkedList).
480
+ * @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
339
481
  */
340
- DoublyLinkedList.prototype.remove = function (index) {
341
- var _a, _b, _c;
342
- if (index < 0 || index > this._size - 1)
343
- return null;
344
- else if (index === 0)
345
- return this.pollFirst();
346
- else if (index === this._size - 1)
347
- return (_b = (_a = this.pollLast('node')) === null || _a === void 0 ? void 0 : _a.val) !== null && _b !== void 0 ? _b : null;
482
+ DoublyLinkedList.prototype.map = function (callback) {
483
+ var mappedList = new DoublyLinkedList();
484
+ var current = this.head;
485
+ while (current) {
486
+ mappedList.push(callback(current.val));
487
+ current = current.next;
488
+ }
489
+ return mappedList;
490
+ };
491
+ /**
492
+ * The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
493
+ * elements that satisfy the given callback function.
494
+ * @param callback - The `callback` parameter is a function that takes a value of type `T` and returns a boolean value.
495
+ * It is used to determine whether a value should be included in the filtered list or not.
496
+ * @returns The filtered list, which is an instance of the DoublyLinkedList class.
497
+ */
498
+ DoublyLinkedList.prototype.filter = function (callback) {
499
+ var filteredList = new DoublyLinkedList();
500
+ var current = this.head;
501
+ while (current) {
502
+ if (callback(current.val)) {
503
+ filteredList.push(current.val);
504
+ }
505
+ current = current.next;
506
+ }
507
+ return filteredList;
508
+ };
509
+ /**
510
+ * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
511
+ * single value.
512
+ * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
513
+ * used to perform a specific operation on each element of the linked list.
514
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
515
+ * point for the reduction operation.
516
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
517
+ * elements in the linked list.
518
+ */
519
+ DoublyLinkedList.prototype.reduce = function (callback, initialValue) {
520
+ var accumulator = initialValue;
521
+ var current = this.head;
522
+ while (current) {
523
+ accumulator = callback(accumulator, current.val);
524
+ current = current.next;
525
+ }
526
+ return accumulator;
527
+ };
528
+ /**
529
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
530
+ * @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
531
+ * after which the new value will be inserted. It can be either the value of the existing node or the existing node
532
+ * itself.
533
+ * @param {T} newValue - The value that you want to insert into the doubly linked list.
534
+ * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
535
+ * existing value or node is not found in the doubly linked list.
536
+ */
537
+ DoublyLinkedList.prototype.insertAfter = function (existingValueOrNode, newValue) {
538
+ var existingNode;
539
+ if (existingValueOrNode instanceof (DoublyLinkedListNode)) {
540
+ existingNode = existingValueOrNode;
541
+ }
348
542
  else {
349
- var prevNode = this.get(index - 1, 'node');
350
- var removeNode = prevNode === null || prevNode === void 0 ? void 0 : prevNode.next;
351
- var nextNode = removeNode === null || removeNode === void 0 ? void 0 : removeNode.next;
352
- if (prevNode)
353
- prevNode.next = nextNode !== null && nextNode !== void 0 ? nextNode : null;
354
- if (nextNode)
355
- nextNode.prev = prevNode;
356
- if (removeNode)
357
- removeNode.next = null;
358
- if (removeNode)
359
- removeNode.prev = null;
360
- this._size--;
361
- return (_c = removeNode === null || removeNode === void 0 ? void 0 : removeNode.val) !== null && _c !== void 0 ? _c : null;
543
+ existingNode = this.findNode(existingValueOrNode);
544
+ }
545
+ if (existingNode) {
546
+ var newNode = new DoublyLinkedListNode(newValue);
547
+ newNode.next = existingNode.next;
548
+ if (existingNode.next) {
549
+ existingNode.next.prev = newNode;
550
+ }
551
+ newNode.prev = existingNode;
552
+ existingNode.next = newNode;
553
+ if (existingNode === this.tail) {
554
+ this.tail = newNode;
555
+ }
556
+ this.length++;
557
+ return true;
558
+ }
559
+ return false;
560
+ };
561
+ /**
562
+ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
563
+ * @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
564
+ * before which the new value will be inserted. It can be either the value of the existing node or the existing node
565
+ * itself.
566
+ * @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
567
+ * list.
568
+ * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
569
+ * insertion fails.
570
+ */
571
+ DoublyLinkedList.prototype.insertBefore = function (existingValueOrNode, newValue) {
572
+ var existingNode;
573
+ if (existingValueOrNode instanceof (DoublyLinkedListNode)) {
574
+ existingNode = existingValueOrNode;
362
575
  }
576
+ else {
577
+ existingNode = this.findNode(existingValueOrNode);
578
+ }
579
+ if (existingNode) {
580
+ var newNode = new DoublyLinkedListNode(newValue);
581
+ newNode.prev = existingNode.prev;
582
+ if (existingNode.prev) {
583
+ existingNode.prev.next = newNode;
584
+ }
585
+ newNode.next = existingNode;
586
+ existingNode.prev = newNode;
587
+ if (existingNode === this.head) {
588
+ this.head = newNode;
589
+ }
590
+ this.length++;
591
+ return true;
592
+ }
593
+ return false;
363
594
  };
364
595
  return DoublyLinkedList;
365
596
  }());