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