data-structure-typed 1.12.21 → 1.15.1
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.
- package/README.md +278 -179
- package/dist/data-structures/binary-tree/binary-tree.d.ts +46 -1
- package/dist/data-structures/binary-tree/binary-tree.js +67 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +29 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +40 -5
- package/dist/data-structures/graph/abstract-graph.js +47 -7
- package/dist/data-structures/graph/directed-graph.d.ts +8 -0
- package/dist/data-structures/graph/directed-graph.js +14 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +10 -0
- package/dist/data-structures/graph/undirected-graph.js +23 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-map.js +16 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +7 -1
- package/dist/data-structures/hash/coordinate-set.js +16 -0
- package/dist/data-structures/heap/heap.d.ts +16 -0
- package/dist/data-structures/heap/heap.js +38 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +30 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +71 -4
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +262 -328
- package/dist/data-structures/linked-list/singly-linked-list.js +258 -273
- package/dist/data-structures/priority-queue/priority-queue.d.ts +7 -1
- package/dist/data-structures/priority-queue/priority-queue.js +18 -2
- package/dist/data-structures/queue/deque.d.ts +18 -7
- package/dist/data-structures/queue/deque.js +50 -3
- package/dist/data-structures/types/abstract-graph.d.ts +2 -2
- package/dist/utils/types/utils.d.ts +0 -49
- package/dist/utils/types/utils.js +14 -52
- package/dist/utils/utils.d.ts +1 -97
- package/dist/utils/utils.js +197 -546
- package/package.json +4 -3
- package/src/data-structures/binary-tree/aa-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +84 -14
- package/src/data-structures/binary-tree/segment-tree.ts +45 -13
- package/src/data-structures/graph/abstract-graph.ts +58 -15
- package/src/data-structures/graph/directed-graph.ts +14 -5
- package/src/data-structures/graph/undirected-graph.ts +23 -6
- package/src/data-structures/hash/coordinate-map.ts +13 -1
- package/src/data-structures/hash/coordinate-set.ts +13 -1
- package/src/data-structures/heap/heap.ts +31 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
- package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
- package/src/data-structures/priority-queue/priority-queue.ts +15 -2
- package/src/data-structures/queue/deque.ts +38 -8
- package/src/data-structures/types/abstract-graph.ts +3 -3
- package/src/utils/types/utils.ts +165 -167
- package/src/utils/utils.ts +209 -480
- 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
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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, "
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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.
|
|
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.
|
|
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
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
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
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
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
|
-
*
|
|
151
|
-
*
|
|
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.
|
|
178
|
-
this.
|
|
179
|
-
this.
|
|
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, "
|
|
185
|
-
|
|
186
|
-
|
|
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.
|
|
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
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
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
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
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
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
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.
|
|
243
|
+
if (this.head === null || index < 0 || index >= this.size) {
|
|
225
244
|
return undefined;
|
|
226
245
|
}
|
|
227
|
-
var asc = index < this.
|
|
228
|
-
var stopAt = asc ? index : this.
|
|
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
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
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 (
|
|
269
|
+
SinglyLinkedList.prototype.findNodeIndex = function (callbackFn) {
|
|
249
270
|
var currentIndex = 0;
|
|
250
271
|
var currentNode = this.head;
|
|
251
272
|
while (currentNode) {
|
|
252
|
-
if (
|
|
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
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
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 (
|
|
273
|
-
var nodeIndex = this.findNodeIndex(
|
|
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
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
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 (
|
|
285
|
-
var nodeIndex = this.findNodeIndex(
|
|
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
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
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 (
|
|
297
|
-
var nodeIndex = this.findNodeIndex(
|
|
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
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* @
|
|
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.
|
|
361
|
+
return this.size;
|
|
352
362
|
};
|
|
353
363
|
/**
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
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
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
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
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
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
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
*
|
|
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
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
* @
|
|
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
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
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.
|
|
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
|
-
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
564
|
-
*
|
|
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
|
-
*
|
|
574
|
-
*
|
|
575
|
-
*
|
|
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
|
-
*
|
|
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
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
*
|
|
615
|
-
*
|
|
616
|
-
*
|
|
617
|
-
*
|
|
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.
|
|
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
|
|
641
|
-
*
|
|
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
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
* @param
|
|
665
|
-
*
|
|
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 (
|
|
648
|
+
SinglyLinkedList.prototype.forEach = function (callbackFn, reverse) {
|
|
668
649
|
if (reverse === void 0) { reverse = false; }
|
|
669
|
-
var currentIndex = reverse ? this.
|
|
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
|
-
|
|
655
|
+
callbackFn(currentNode.val, currentIndex, this);
|
|
675
656
|
currentNode = currentNode[nextNode];
|
|
676
657
|
currentIndex += modifier;
|
|
677
658
|
}
|
|
678
659
|
};
|
|
679
660
|
/**
|
|
680
|
-
* The map
|
|
681
|
-
*
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
*
|
|
686
|
-
*
|
|
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
|
-
|
|
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(
|
|
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
|
|
698
|
-
* that
|
|
699
|
-
*
|
|
700
|
-
*
|
|
701
|
-
*
|
|
702
|
-
*
|
|
703
|
-
* @
|
|
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
|
-
|
|
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 (
|
|
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
|
-
*
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
*
|
|
722
|
-
* @param
|
|
723
|
-
*
|
|
724
|
-
*
|
|
725
|
-
|
|
726
|
-
|
|
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.
|
|
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 =
|
|
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
|
-
*
|
|
752
|
-
*
|
|
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
|
-
*
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
*
|
|
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
|
|
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
|
-
/**
|
|
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
|
};
|