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.
- package/README.md +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +178 -70
- package/dist/data-structures/linked-list/doubly-linked-list.js +475 -244
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +111 -248
- package/dist/data-structures/linked-list/singly-linked-list.js +338 -619
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +7 -0
- package/dist/data-structures/matrix/vector2d.js +2 -2
- package/dist/data-structures/types/binary-tree.d.ts +0 -5
- package/docs/assets/search.js +1 -1
- package/docs/classes/AVLTree.html +94 -94
- package/docs/classes/AVLTreeNode.html +37 -37
- package/docs/classes/AaTree.html +2 -2
- package/docs/classes/AbstractEdge.html +12 -12
- package/docs/classes/AbstractGraph.html +30 -30
- package/docs/classes/AbstractVertex.html +7 -7
- package/docs/classes/ArrayDeque.html +15 -15
- package/docs/classes/BST.html +87 -87
- package/docs/classes/BSTNode.html +37 -37
- package/docs/classes/BTree.html +2 -2
- package/docs/classes/BinaryIndexedTree.html +8 -8
- package/docs/classes/BinaryTree.html +83 -83
- package/docs/classes/BinaryTreeNode.html +37 -37
- package/docs/classes/Character.html +5 -5
- package/docs/classes/CoordinateMap.html +11 -11
- package/docs/classes/CoordinateSet.html +10 -10
- package/docs/classes/Deque.html +564 -356
- package/docs/classes/DirectedEdge.html +18 -18
- package/docs/classes/DirectedGraph.html +42 -42
- package/docs/classes/DirectedVertex.html +7 -7
- package/docs/classes/DoublyLinkedList.html +546 -313
- package/docs/classes/DoublyLinkedListNode.html +53 -36
- package/docs/classes/Heap.html +21 -21
- package/docs/classes/HeapItem.html +9 -9
- package/docs/classes/Matrix2D.html +17 -17
- package/docs/classes/MatrixNTI2D.html +5 -5
- package/docs/classes/MaxHeap.html +21 -21
- package/docs/classes/MaxPriorityQueue.html +35 -35
- package/docs/classes/MinHeap.html +21 -21
- package/docs/classes/MinPriorityQueue.html +35 -35
- package/docs/classes/Navigator.html +11 -11
- package/docs/classes/ObjectDeque.html +26 -26
- package/docs/classes/PriorityQueue.html +33 -33
- package/docs/classes/Queue.html +15 -15
- package/docs/classes/RBTree.html +2 -2
- package/docs/classes/SegmentTree.html +13 -13
- package/docs/classes/SegmentTreeNode.html +27 -27
- package/docs/classes/SinglyLinkedList.html +277 -606
- package/docs/classes/SinglyLinkedListNode.html +40 -167
- package/docs/classes/SkipLinkedList.html +150 -0
- package/docs/classes/SplayTree.html +2 -2
- package/docs/classes/Stack.html +13 -13
- package/docs/classes/TreeMultiSet.html +87 -87
- package/docs/classes/TreeNode.html +9 -9
- package/docs/classes/Trie.html +14 -14
- package/docs/classes/TrieNode.html +12 -12
- package/docs/classes/TwoThreeTree.html +2 -2
- package/docs/classes/UndirectedEdge.html +14 -14
- package/docs/classes/UndirectedGraph.html +36 -36
- package/docs/classes/UndirectedVertex.html +7 -7
- package/docs/classes/Vector2D.html +29 -29
- package/docs/enums/CP.html +5 -5
- package/docs/enums/FamilyPosition.html +5 -5
- package/docs/enums/LoopType.html +4 -4
- package/docs/index.html +3 -3
- package/docs/interfaces/AVLTreeDeleted.html +4 -4
- package/docs/interfaces/HeapOptions.html +3 -3
- package/docs/interfaces/IDirectedGraph.html +8 -8
- package/docs/interfaces/IGraph.html +20 -20
- package/docs/interfaces/NavigatorParams.html +6 -6
- package/docs/interfaces/PriorityQueueOptions.html +5 -5
- package/docs/modules.html +2 -2
- package/docs/types/BSTComparator.html +2 -2
- package/docs/types/BSTDeletedResult.html +2 -2
- package/docs/types/BinaryTreeDeleted.html +2 -2
- package/docs/types/BinaryTreeNodeId.html +2 -2
- package/docs/types/BinaryTreeNodePropertyName.html +2 -2
- package/docs/types/DFSOrderPattern.html +2 -2
- package/docs/types/DijkstraResult.html +2 -2
- package/docs/types/Direction.html +2 -2
- package/docs/types/DoublyLinkedListGetBy.html +2 -2
- package/docs/types/NodeOrPropertyName.html +2 -2
- package/docs/types/PriorityQueueComparator.html +2 -2
- package/docs/types/PriorityQueueDFSOrderPattern.html +2 -2
- package/docs/types/ResultByProperty.html +2 -2
- package/docs/types/ResultsByProperty.html +2 -2
- package/docs/types/SegmentTreeNodeVal.html +2 -2
- package/docs/types/SpecifyOptional.html +2 -2
- package/docs/types/Thunk.html +2 -2
- package/docs/types/ToThunkFn.html +2 -2
- package/docs/types/TopologicalStatus.html +2 -2
- package/docs/types/TreeMultiSetDeletedResult.html +2 -2
- package/docs/types/TrlAsyncFn.html +2 -2
- package/docs/types/TrlFn.html +2 -2
- package/docs/types/Turning.html +2 -2
- package/docs/types/VertexId.html +2 -2
- package/package.json +1 -3
- package/docs/interfaces/BinaryTreeNodeObj.html +0 -168
|
@@ -1,37 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
13
|
-
function step(op) {
|
|
14
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
15
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
16
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
17
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
18
|
-
switch (op[0]) {
|
|
19
|
-
case 0: case 1: t = op; break;
|
|
20
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
21
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
22
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
23
|
-
default:
|
|
24
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
25
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
26
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
27
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
28
|
-
if (t[2]) _.ops.pop();
|
|
29
|
-
_.trys.pop(); continue;
|
|
30
|
-
}
|
|
31
|
-
op = body.call(thisArg, _);
|
|
32
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
33
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
34
|
-
}
|
|
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.");
|
|
35
12
|
};
|
|
36
13
|
var __read = (this && this.__read) || function (o, n) {
|
|
37
14
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
@@ -49,36 +26,24 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
49
26
|
}
|
|
50
27
|
return ar;
|
|
51
28
|
};
|
|
52
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
53
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
54
|
-
if (ar || !(i in from)) {
|
|
55
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
56
|
-
ar[i] = from[i];
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
60
|
-
};
|
|
61
|
-
var __values = (this && this.__values) || function(o) {
|
|
62
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
63
|
-
if (m) return m.call(o);
|
|
64
|
-
if (o && typeof o.length === "number") return {
|
|
65
|
-
next: function () {
|
|
66
|
-
if (o && i >= o.length) o = void 0;
|
|
67
|
-
return { value: o && o[i++], done: !o };
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
71
|
-
};
|
|
72
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
73
30
|
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
74
|
-
|
|
75
|
-
|
|
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
|
+
*/
|
|
76
38
|
var SinglyLinkedListNode = /** @class */ (function () {
|
|
77
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
41
|
+
* @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
|
|
42
|
+
* will be stored in the node of a linked list.
|
|
43
|
+
*/
|
|
44
|
+
function SinglyLinkedListNode(val) {
|
|
78
45
|
this._val = val;
|
|
79
|
-
this.
|
|
80
|
-
this._next = next || null;
|
|
81
|
-
this._list = list || null;
|
|
46
|
+
this._next = null;
|
|
82
47
|
}
|
|
83
48
|
Object.defineProperty(SinglyLinkedListNode.prototype, "val", {
|
|
84
49
|
get: function () {
|
|
@@ -90,16 +55,6 @@ var SinglyLinkedListNode = /** @class */ (function () {
|
|
|
90
55
|
enumerable: false,
|
|
91
56
|
configurable: true
|
|
92
57
|
});
|
|
93
|
-
Object.defineProperty(SinglyLinkedListNode.prototype, "prev", {
|
|
94
|
-
get: function () {
|
|
95
|
-
return this._prev;
|
|
96
|
-
},
|
|
97
|
-
set: function (value) {
|
|
98
|
-
this._prev = value;
|
|
99
|
-
},
|
|
100
|
-
enumerable: false,
|
|
101
|
-
configurable: true
|
|
102
|
-
});
|
|
103
58
|
Object.defineProperty(SinglyLinkedListNode.prototype, "next", {
|
|
104
59
|
get: function () {
|
|
105
60
|
return this._next;
|
|
@@ -110,78 +65,17 @@ var SinglyLinkedListNode = /** @class */ (function () {
|
|
|
110
65
|
enumerable: false,
|
|
111
66
|
configurable: true
|
|
112
67
|
});
|
|
113
|
-
Object.defineProperty(SinglyLinkedListNode.prototype, "list", {
|
|
114
|
-
get: function () {
|
|
115
|
-
return this._list;
|
|
116
|
-
},
|
|
117
|
-
set: function (value) {
|
|
118
|
-
this._list = value;
|
|
119
|
-
},
|
|
120
|
-
enumerable: false,
|
|
121
|
-
configurable: true
|
|
122
|
-
});
|
|
123
|
-
Object.defineProperty(SinglyLinkedListNode.prototype, "index", {
|
|
124
|
-
get: function () {
|
|
125
|
-
var _this = this;
|
|
126
|
-
if (!this.list) {
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
return this.list.findIndex(function (value) { return value === _this.val; });
|
|
130
|
-
},
|
|
131
|
-
enumerable: false,
|
|
132
|
-
configurable: true
|
|
133
|
-
});
|
|
134
|
-
/**
|
|
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>.
|
|
139
|
-
*/
|
|
140
|
-
SinglyLinkedListNode.prototype.insertBefore = function (val) {
|
|
141
|
-
return this.list !== null
|
|
142
|
-
? this.list.insertBefore(this, val)
|
|
143
|
-
: new SinglyLinkedList(val, this.val);
|
|
144
|
-
};
|
|
145
|
-
/**
|
|
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>.
|
|
149
|
-
*/
|
|
150
|
-
SinglyLinkedListNode.prototype.insertAfter = function (val) {
|
|
151
|
-
return this.list !== null
|
|
152
|
-
? this.list.insertAfter(this, val)
|
|
153
|
-
: new SinglyLinkedList(this.val, val);
|
|
154
|
-
};
|
|
155
|
-
/**
|
|
156
|
-
* The `remove()` function removes a node from a singly linked list.
|
|
157
|
-
* @returns The remove() method is returning a SinglyLinkedListNode<NodeVal> object.
|
|
158
|
-
*/
|
|
159
|
-
SinglyLinkedListNode.prototype.remove = function () {
|
|
160
|
-
if (this.list === null) {
|
|
161
|
-
throw new ReferenceError('Node does not belong to any list');
|
|
162
|
-
}
|
|
163
|
-
return this.list.removeNode(this);
|
|
164
|
-
};
|
|
165
68
|
return SinglyLinkedListNode;
|
|
166
69
|
}());
|
|
167
70
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
168
71
|
var SinglyLinkedList = /** @class */ (function () {
|
|
169
72
|
/**
|
|
170
|
-
* The constructor initializes
|
|
171
|
-
* @param {NodeVal[]} args - args is a rest parameter that allows the constructor to accept an arbitrary number of
|
|
172
|
-
* arguments of type NodeVal.
|
|
73
|
+
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
173
74
|
*/
|
|
174
75
|
function SinglyLinkedList() {
|
|
175
|
-
var args = [];
|
|
176
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
177
|
-
args[_i] = arguments[_i];
|
|
178
|
-
}
|
|
179
76
|
this._head = null;
|
|
180
77
|
this._tail = null;
|
|
181
|
-
this.
|
|
182
|
-
for (var i = 0; i < arguments.length; i++) {
|
|
183
|
-
this.append(args[i]);
|
|
184
|
-
}
|
|
78
|
+
this._length = 0;
|
|
185
79
|
}
|
|
186
80
|
Object.defineProperty(SinglyLinkedList.prototype, "head", {
|
|
187
81
|
get: function () {
|
|
@@ -203,583 +97,408 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
203
97
|
enumerable: false,
|
|
204
98
|
configurable: true
|
|
205
99
|
});
|
|
206
|
-
Object.defineProperty(SinglyLinkedList.prototype, "
|
|
100
|
+
Object.defineProperty(SinglyLinkedList.prototype, "length", {
|
|
207
101
|
get: function () {
|
|
208
|
-
return this.
|
|
102
|
+
return this._length;
|
|
209
103
|
},
|
|
210
104
|
set: function (value) {
|
|
211
|
-
this.
|
|
105
|
+
this._length = value;
|
|
212
106
|
},
|
|
213
107
|
enumerable: false,
|
|
214
108
|
configurable: true
|
|
215
109
|
});
|
|
216
110
|
/**
|
|
217
|
-
* The `
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* @returns The
|
|
221
|
-
*/
|
|
222
|
-
SinglyLinkedList.from = function (iterable) {
|
|
223
|
-
return new (SinglyLinkedList.bind.apply(SinglyLinkedList, __spreadArray([void 0], __read(iterable), false)))();
|
|
224
|
-
};
|
|
225
|
-
/**
|
|
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.
|
|
231
|
-
*/
|
|
232
|
-
SinglyLinkedList.prototype.get = function (index) {
|
|
233
|
-
var node = this.getNode(index);
|
|
234
|
-
return node !== undefined ? node.val : undefined;
|
|
235
|
-
};
|
|
236
|
-
/**
|
|
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.
|
|
241
|
-
*/
|
|
242
|
-
SinglyLinkedList.prototype.getNode = function (index) {
|
|
243
|
-
if (this.head === null || index < 0 || index >= this.size) {
|
|
244
|
-
return undefined;
|
|
245
|
-
}
|
|
246
|
-
var asc = index < this.size / 2;
|
|
247
|
-
var stopAt = asc ? index : this.size - index - 1;
|
|
248
|
-
var nextNode = asc ? 'next' : 'prev';
|
|
249
|
-
var currentNode = asc ? this.head : this.tail;
|
|
250
|
-
// TODO after no-non-null-assertion not ensure the logic
|
|
251
|
-
for (var currentIndex = 0; currentIndex < stopAt; currentIndex++) {
|
|
252
|
-
if (currentNode) {
|
|
253
|
-
currentNode = currentNode[nextNode];
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
return currentNode || undefined;
|
|
257
|
-
};
|
|
258
|
-
/**
|
|
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`.
|
|
268
|
-
*/
|
|
269
|
-
SinglyLinkedList.prototype.findNodeIndex = function (callbackFn) {
|
|
270
|
-
var currentIndex = 0;
|
|
271
|
-
var currentNode = this.head;
|
|
272
|
-
while (currentNode) {
|
|
273
|
-
if (callbackFn(currentNode.val, currentIndex, this)) {
|
|
274
|
-
return {
|
|
275
|
-
index: currentIndex,
|
|
276
|
-
node: currentNode,
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
currentNode = currentNode.next;
|
|
280
|
-
currentIndex += 1;
|
|
281
|
-
}
|
|
282
|
-
return undefined;
|
|
283
|
-
};
|
|
284
|
-
/**
|
|
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`.
|
|
290
|
-
*/
|
|
291
|
-
SinglyLinkedList.prototype.findNode = function (callbackFn) {
|
|
292
|
-
var nodeIndex = this.findNodeIndex(callbackFn);
|
|
293
|
-
return nodeIndex !== undefined ? nodeIndex.node : undefined;
|
|
294
|
-
};
|
|
295
|
-
/**
|
|
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`.
|
|
111
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
112
|
+
* array.
|
|
113
|
+
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
|
114
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
302
115
|
*/
|
|
303
|
-
SinglyLinkedList.
|
|
304
|
-
var nodeIndex = this.findNodeIndex(callbackFn);
|
|
305
|
-
return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
|
|
306
|
-
};
|
|
307
|
-
/**
|
|
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.
|
|
313
|
-
*/
|
|
314
|
-
SinglyLinkedList.prototype.findIndex = function (callbackFn) {
|
|
315
|
-
var nodeIndex = this.findNodeIndex(callbackFn);
|
|
316
|
-
return nodeIndex !== undefined ? nodeIndex.index : -1;
|
|
317
|
-
};
|
|
318
|
-
/* The above code is a comment in TypeScript. It is using the triple hash symbol ( */
|
|
319
|
-
SinglyLinkedList.prototype.append = function () {
|
|
116
|
+
SinglyLinkedList.fromArray = function (data) {
|
|
320
117
|
var e_1, _a;
|
|
321
|
-
var
|
|
322
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
323
|
-
args[_i] = arguments[_i];
|
|
324
|
-
}
|
|
118
|
+
var singlyLinkedList = new SinglyLinkedList();
|
|
325
119
|
try {
|
|
326
|
-
for (var
|
|
327
|
-
var
|
|
328
|
-
|
|
329
|
-
if (this.head === null) {
|
|
330
|
-
this.head = node;
|
|
331
|
-
}
|
|
332
|
-
if (this.tail !== null) {
|
|
333
|
-
this.tail.next = node;
|
|
334
|
-
}
|
|
335
|
-
this.tail = node;
|
|
336
|
-
this.size += 1;
|
|
120
|
+
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
|
|
121
|
+
var item = data_1_1.value;
|
|
122
|
+
singlyLinkedList.push(item);
|
|
337
123
|
}
|
|
338
124
|
}
|
|
339
125
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
340
126
|
finally {
|
|
341
127
|
try {
|
|
342
|
-
if (
|
|
128
|
+
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
|
|
343
129
|
}
|
|
344
130
|
finally { if (e_1) throw e_1.error; }
|
|
345
131
|
}
|
|
346
|
-
return
|
|
132
|
+
return singlyLinkedList;
|
|
347
133
|
};
|
|
348
|
-
|
|
349
|
-
|
|
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.
|
|
354
|
-
*/
|
|
355
|
-
SinglyLinkedList.prototype.push = function () {
|
|
356
|
-
var args = [];
|
|
357
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
358
|
-
args[_i] = arguments[_i];
|
|
359
|
-
}
|
|
360
|
-
this.append.apply(this, __spreadArray([], __read(args), false));
|
|
361
|
-
return this.size;
|
|
134
|
+
SinglyLinkedList.prototype.getLength = function () {
|
|
135
|
+
return this._length;
|
|
362
136
|
};
|
|
363
137
|
/**
|
|
364
|
-
* The `
|
|
365
|
-
* @param {
|
|
366
|
-
*
|
|
138
|
+
* The `push` function adds a new node with the given data to the end of a singly linked list.
|
|
139
|
+
* @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
|
|
140
|
+
* any type (T) as specified in the generic type declaration of the class or function.
|
|
367
141
|
*/
|
|
368
|
-
SinglyLinkedList.prototype.
|
|
369
|
-
var
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
142
|
+
SinglyLinkedList.prototype.push = function (data) {
|
|
143
|
+
var newNode = new SinglyLinkedListNode(data);
|
|
144
|
+
if (!this.head) {
|
|
145
|
+
this.head = newNode;
|
|
146
|
+
this.tail = newNode;
|
|
373
147
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
var val = reverseArgs_1_1.value;
|
|
378
|
-
var node = new SinglyLinkedListNode(val, null, this.head, this);
|
|
379
|
-
if (this.tail === null) {
|
|
380
|
-
this.tail = node;
|
|
381
|
-
}
|
|
382
|
-
if (this.head !== null) {
|
|
383
|
-
this.head.prev = node;
|
|
384
|
-
}
|
|
385
|
-
this.head = node;
|
|
386
|
-
this.size += 1;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
390
|
-
finally {
|
|
391
|
-
try {
|
|
392
|
-
if (reverseArgs_1_1 && !reverseArgs_1_1.done && (_a = reverseArgs_1.return)) _a.call(reverseArgs_1);
|
|
393
|
-
}
|
|
394
|
-
finally { if (e_2) throw e_2.error; }
|
|
395
|
-
}
|
|
396
|
-
return this;
|
|
397
|
-
};
|
|
398
|
-
/**
|
|
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.
|
|
405
|
-
*/
|
|
406
|
-
SinglyLinkedList.prototype.insertAt = function (index, val) {
|
|
407
|
-
if (this.head === null) {
|
|
408
|
-
return this.append(val);
|
|
409
|
-
}
|
|
410
|
-
if (index <= 0) {
|
|
411
|
-
return this.prepend(val);
|
|
412
|
-
}
|
|
413
|
-
var currentNode = this.head;
|
|
414
|
-
var currentIndex = 0;
|
|
415
|
-
while (currentIndex < index - 1 && currentNode.next !== null) {
|
|
416
|
-
currentIndex += 1;
|
|
417
|
-
currentNode = currentNode.next;
|
|
148
|
+
else {
|
|
149
|
+
this.tail.next = newNode;
|
|
150
|
+
this.tail = newNode;
|
|
418
151
|
}
|
|
419
|
-
|
|
420
|
-
return this;
|
|
152
|
+
this.length++;
|
|
421
153
|
};
|
|
422
154
|
/**
|
|
423
|
-
* The
|
|
424
|
-
* accordingly.
|
|
425
|
-
* @
|
|
426
|
-
* linked list
|
|
427
|
-
* @returns the removed node.
|
|
155
|
+
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
156
|
+
* pointers accordingly.
|
|
157
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
158
|
+
* the linked list is empty, it returns `null`.
|
|
428
159
|
*/
|
|
429
|
-
SinglyLinkedList.prototype.
|
|
430
|
-
if (
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
}
|
|
439
|
-
if (this.head === node) {
|
|
440
|
-
this.head = node.next;
|
|
160
|
+
SinglyLinkedList.prototype.pop = function () {
|
|
161
|
+
if (!this.head)
|
|
162
|
+
return null;
|
|
163
|
+
if (this.head === this.tail) {
|
|
164
|
+
var val_1 = this.head.val;
|
|
165
|
+
this.head = null;
|
|
166
|
+
this.tail = null;
|
|
167
|
+
this.length--;
|
|
168
|
+
return val_1;
|
|
441
169
|
}
|
|
442
|
-
|
|
443
|
-
|
|
170
|
+
var current = this.head;
|
|
171
|
+
while (current.next !== this.tail) {
|
|
172
|
+
current = current.next;
|
|
444
173
|
}
|
|
445
|
-
this.
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
return
|
|
174
|
+
var val = this.tail.val;
|
|
175
|
+
current.next = null;
|
|
176
|
+
this.tail = current;
|
|
177
|
+
this.length--;
|
|
178
|
+
return val;
|
|
450
179
|
};
|
|
451
180
|
/**
|
|
452
|
-
* The `
|
|
453
|
-
* @
|
|
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`.
|
|
181
|
+
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
182
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
457
183
|
*/
|
|
458
|
-
SinglyLinkedList.prototype.
|
|
459
|
-
|
|
460
|
-
|
|
184
|
+
SinglyLinkedList.prototype.shift = function () {
|
|
185
|
+
if (!this.head)
|
|
186
|
+
return null;
|
|
187
|
+
var removedNode = this.head;
|
|
188
|
+
this.head = this.head.next;
|
|
189
|
+
this.length--;
|
|
190
|
+
return removedNode.val;
|
|
461
191
|
};
|
|
462
192
|
/**
|
|
463
|
-
* The
|
|
193
|
+
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
194
|
+
* @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
464
195
|
* 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.
|
|
469
196
|
*/
|
|
470
|
-
SinglyLinkedList.prototype.
|
|
471
|
-
var
|
|
472
|
-
if (
|
|
473
|
-
this.head =
|
|
197
|
+
SinglyLinkedList.prototype.unshift = function (val) {
|
|
198
|
+
var newNode = new SinglyLinkedListNode(val);
|
|
199
|
+
if (!this.head) {
|
|
200
|
+
this.head = newNode;
|
|
201
|
+
this.tail = newNode;
|
|
474
202
|
}
|
|
475
|
-
|
|
476
|
-
|
|
203
|
+
else {
|
|
204
|
+
newNode.next = this.head;
|
|
205
|
+
this.head = newNode;
|
|
477
206
|
}
|
|
478
|
-
|
|
479
|
-
this.size += 1;
|
|
480
|
-
return this;
|
|
207
|
+
this.length++;
|
|
481
208
|
};
|
|
482
209
|
/**
|
|
483
|
-
* The `
|
|
484
|
-
*
|
|
485
|
-
*
|
|
486
|
-
* @
|
|
487
|
-
*
|
|
488
|
-
* @returns The `sort` method is returning the sorted `SinglyLinkedList` object.
|
|
210
|
+
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
211
|
+
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
212
|
+
* retrieve from the list.
|
|
213
|
+
* @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
|
|
214
|
+
* `null` if the index is out of bounds.
|
|
489
215
|
*/
|
|
490
|
-
SinglyLinkedList.prototype.
|
|
491
|
-
if (
|
|
492
|
-
return
|
|
216
|
+
SinglyLinkedList.prototype.getAt = function (index) {
|
|
217
|
+
if (index < 0 || index >= this.length)
|
|
218
|
+
return null;
|
|
219
|
+
var current = this.head;
|
|
220
|
+
for (var i = 0; i < index; i++) {
|
|
221
|
+
current = current.next;
|
|
493
222
|
}
|
|
494
|
-
|
|
495
|
-
|
|
223
|
+
return current.val;
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
227
|
+
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
228
|
+
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
229
|
+
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
|
|
230
|
+
* specified index exists, or `null` if the index is out of bounds.
|
|
231
|
+
*/
|
|
232
|
+
SinglyLinkedList.prototype.getNodeAt = function (index) {
|
|
233
|
+
var current = this.head;
|
|
234
|
+
for (var i = 0; i < index; i++) {
|
|
235
|
+
current = current.next;
|
|
236
|
+
}
|
|
237
|
+
return current;
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
241
|
+
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
242
|
+
* data structure. It is of type number.
|
|
243
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
244
|
+
* bounds.
|
|
245
|
+
*/
|
|
246
|
+
SinglyLinkedList.prototype.deleteAt = function (index) {
|
|
247
|
+
if (index < 0 || index >= this.length)
|
|
248
|
+
return null;
|
|
249
|
+
if (index === 0)
|
|
250
|
+
return this.shift();
|
|
251
|
+
if (index === this.length - 1)
|
|
252
|
+
return this.pop();
|
|
253
|
+
var prevNode = this.getNodeAt(index - 1);
|
|
254
|
+
var removedNode = prevNode.next;
|
|
255
|
+
prevNode.next = removedNode.next;
|
|
256
|
+
this.length--;
|
|
257
|
+
return removedNode.val;
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* The delete function removes a node with a specific value from a singly linked list.
|
|
261
|
+
* @param {T | SinglyLinkedListNode<T>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `T`
|
|
262
|
+
* or a `SinglyLinkedListNode<T>` object.
|
|
263
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
264
|
+
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
265
|
+
*/
|
|
266
|
+
SinglyLinkedList.prototype.delete = function (valueOrNode) {
|
|
267
|
+
var value;
|
|
268
|
+
if (valueOrNode instanceof (SinglyLinkedListNode)) {
|
|
269
|
+
value = valueOrNode.val;
|
|
496
270
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
if (current !== split) {
|
|
508
|
-
var temp = split.val;
|
|
509
|
-
split.val = current.val;
|
|
510
|
-
current.val = temp;
|
|
271
|
+
else {
|
|
272
|
+
value = valueOrNode;
|
|
273
|
+
}
|
|
274
|
+
var current = this.head, prev = null;
|
|
275
|
+
while (current) {
|
|
276
|
+
if (current.val === value) {
|
|
277
|
+
if (prev === null) {
|
|
278
|
+
this.head = current.next;
|
|
279
|
+
if (current === this.tail) {
|
|
280
|
+
this.tail = null;
|
|
511
281
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
prev.next = current.next;
|
|
285
|
+
if (current === this.tail) {
|
|
286
|
+
this.tail = prev;
|
|
515
287
|
}
|
|
516
288
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
end.val = split.val;
|
|
520
|
-
split.val = pivotData;
|
|
521
|
-
if (start.next === end.prev) {
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
524
|
-
if (split.prev && split !== start) {
|
|
525
|
-
quicksort(start, split.prev);
|
|
289
|
+
this.length--;
|
|
290
|
+
return true;
|
|
526
291
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
quicksort(this.head, this.tail);
|
|
532
|
-
return this;
|
|
292
|
+
prev = current;
|
|
293
|
+
current = current.next;
|
|
294
|
+
}
|
|
295
|
+
return false;
|
|
533
296
|
};
|
|
534
297
|
/**
|
|
535
|
-
* The `
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
* @param {
|
|
539
|
-
*
|
|
298
|
+
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
299
|
+
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
300
|
+
* linked list. It is of type number.
|
|
301
|
+
* @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
|
|
302
|
+
* specified index.
|
|
303
|
+
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
304
|
+
* if the index is out of bounds.
|
|
540
305
|
*/
|
|
541
|
-
SinglyLinkedList.prototype.
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
306
|
+
SinglyLinkedList.prototype.insertAt = function (index, val) {
|
|
307
|
+
if (index < 0 || index > this.length)
|
|
308
|
+
return false;
|
|
309
|
+
if (index === 0) {
|
|
310
|
+
this.unshift(val);
|
|
311
|
+
return true;
|
|
545
312
|
}
|
|
546
|
-
if (
|
|
547
|
-
|
|
313
|
+
if (index === this.length) {
|
|
314
|
+
this.push(val);
|
|
315
|
+
return true;
|
|
548
316
|
}
|
|
549
|
-
|
|
550
|
-
this.
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
* @returns The `shift()` method is returning a value of type `NodeVal` or `undefined`.
|
|
556
|
-
*/
|
|
557
|
-
SinglyLinkedList.prototype.shift = function () {
|
|
558
|
-
return this.removeFromAnyEnd(this.head);
|
|
317
|
+
var newNode = new SinglyLinkedListNode(val);
|
|
318
|
+
var prevNode = this.getNodeAt(index - 1);
|
|
319
|
+
newNode.next = prevNode.next;
|
|
320
|
+
prevNode.next = newNode;
|
|
321
|
+
this.length++;
|
|
322
|
+
return true;
|
|
559
323
|
};
|
|
560
324
|
/**
|
|
561
|
-
* The
|
|
562
|
-
*
|
|
325
|
+
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
326
|
+
* whether it is empty or not.
|
|
327
|
+
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
563
328
|
*/
|
|
564
|
-
SinglyLinkedList.prototype.
|
|
565
|
-
return this.
|
|
566
|
-
};
|
|
567
|
-
/**
|
|
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.
|
|
571
|
-
*/
|
|
572
|
-
SinglyLinkedList.prototype.merge = function (list) {
|
|
573
|
-
if (this.tail !== null) {
|
|
574
|
-
this.tail.next = list.head;
|
|
575
|
-
}
|
|
576
|
-
if (list.head !== null) {
|
|
577
|
-
list.head.prev = this.tail;
|
|
578
|
-
}
|
|
579
|
-
this.head = this.head || list.head;
|
|
580
|
-
this.tail = list.tail || this.tail;
|
|
581
|
-
this.size += list.size;
|
|
582
|
-
list.size = this.size;
|
|
583
|
-
list.head = this.head;
|
|
584
|
-
list.tail = this.tail;
|
|
329
|
+
SinglyLinkedList.prototype.isEmpty = function () {
|
|
330
|
+
return this.length === 0;
|
|
585
331
|
};
|
|
586
332
|
/**
|
|
587
|
-
* The clear
|
|
588
|
-
* @returns The "this" object is being returned.
|
|
333
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
589
334
|
*/
|
|
590
335
|
SinglyLinkedList.prototype.clear = function () {
|
|
591
|
-
this.
|
|
592
|
-
this.
|
|
593
|
-
this.
|
|
594
|
-
return this;
|
|
336
|
+
this._head = null;
|
|
337
|
+
this._tail = null;
|
|
338
|
+
this._length = 0;
|
|
595
339
|
};
|
|
596
340
|
/**
|
|
597
|
-
* The `
|
|
598
|
-
*
|
|
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.
|
|
341
|
+
* The `toArray` function converts a linked list into an array.
|
|
342
|
+
* @returns The `toArray()` method is returning an array of type `T[]`.
|
|
605
343
|
*/
|
|
606
|
-
SinglyLinkedList.prototype.
|
|
607
|
-
var
|
|
608
|
-
var
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
if (finish === undefined || finish < start) {
|
|
613
|
-
finish = this.size;
|
|
614
|
-
}
|
|
615
|
-
var head = this.getNode(start);
|
|
616
|
-
for (var i = 0; i < finish - start && head !== null && head !== undefined; i++) {
|
|
617
|
-
list.append(head.val);
|
|
618
|
-
head = head.next;
|
|
344
|
+
SinglyLinkedList.prototype.toArray = function () {
|
|
345
|
+
var array = [];
|
|
346
|
+
var current = this.head;
|
|
347
|
+
while (current) {
|
|
348
|
+
array.push(current.val);
|
|
349
|
+
current = current.next;
|
|
619
350
|
}
|
|
620
|
-
return
|
|
351
|
+
return array;
|
|
621
352
|
};
|
|
622
353
|
/**
|
|
623
|
-
* The reverse
|
|
624
|
-
* @returns The reverse() method
|
|
354
|
+
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
355
|
+
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
625
356
|
*/
|
|
626
357
|
SinglyLinkedList.prototype.reverse = function () {
|
|
627
|
-
var
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
358
|
+
var _a;
|
|
359
|
+
if (!this.head || this.head === this.tail)
|
|
360
|
+
return;
|
|
361
|
+
var prev = null;
|
|
362
|
+
var current = this.head;
|
|
363
|
+
var next = null;
|
|
364
|
+
while (current) {
|
|
365
|
+
next = current.next;
|
|
366
|
+
current.next = prev;
|
|
367
|
+
prev = current;
|
|
368
|
+
current = next;
|
|
369
|
+
}
|
|
370
|
+
_a = __read([this.tail, this.head], 2), this.head = _a[0], this.tail = _a[1];
|
|
371
|
+
};
|
|
372
|
+
/**
|
|
373
|
+
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
374
|
+
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
375
|
+
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
376
|
+
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
377
|
+
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
378
|
+
*/
|
|
379
|
+
SinglyLinkedList.prototype.find = function (callback) {
|
|
380
|
+
var current = this.head;
|
|
381
|
+
while (current) {
|
|
382
|
+
if (callback(current.val)) {
|
|
383
|
+
return current.val;
|
|
384
|
+
}
|
|
385
|
+
current = current.next;
|
|
633
386
|
}
|
|
634
|
-
|
|
635
|
-
this.tail = this.head;
|
|
636
|
-
this.head = tail;
|
|
637
|
-
return this;
|
|
387
|
+
return null;
|
|
638
388
|
};
|
|
639
389
|
/**
|
|
640
|
-
* The `
|
|
641
|
-
*
|
|
642
|
-
* @
|
|
643
|
-
*
|
|
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.
|
|
390
|
+
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
391
|
+
* @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
392
|
+
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
|
393
|
+
* value is not found, it returns -1.
|
|
647
394
|
*/
|
|
648
|
-
SinglyLinkedList.prototype.
|
|
649
|
-
|
|
650
|
-
var
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
currentIndex += modifier;
|
|
395
|
+
SinglyLinkedList.prototype.indexOf = function (value) {
|
|
396
|
+
var index = 0;
|
|
397
|
+
var current = this.head;
|
|
398
|
+
while (current) {
|
|
399
|
+
if (current.val === value) {
|
|
400
|
+
return index;
|
|
401
|
+
}
|
|
402
|
+
index++;
|
|
403
|
+
current = current.next;
|
|
658
404
|
}
|
|
405
|
+
return -1;
|
|
659
406
|
};
|
|
660
407
|
/**
|
|
661
|
-
* The
|
|
662
|
-
*
|
|
663
|
-
* @param
|
|
664
|
-
*
|
|
665
|
-
*
|
|
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.
|
|
669
|
-
*/
|
|
670
|
-
SinglyLinkedList.prototype.map = function (callbackFn, reverse) {
|
|
671
|
-
var _this = this;
|
|
672
|
-
if (reverse === void 0) { reverse = false; }
|
|
673
|
-
var list = new SinglyLinkedList();
|
|
674
|
-
this.forEach(function (val, index) { return list.append(callbackFn(val, index, _this)); }, reverse);
|
|
675
|
-
return list;
|
|
676
|
-
};
|
|
677
|
-
/**
|
|
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.
|
|
408
|
+
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
409
|
+
* null.
|
|
410
|
+
* @param {T} value - The value parameter is the value that we want to search for in the linked list.
|
|
411
|
+
* @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
|
|
412
|
+
* the specified value is found, the function returns `null`.
|
|
685
413
|
*/
|
|
686
|
-
SinglyLinkedList.prototype.
|
|
687
|
-
var
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
if (callbackFn(val, index, _this)) {
|
|
692
|
-
list.append(val);
|
|
414
|
+
SinglyLinkedList.prototype.findNode = function (value) {
|
|
415
|
+
var current = this.head;
|
|
416
|
+
while (current) {
|
|
417
|
+
if (current.val === value) {
|
|
418
|
+
return current;
|
|
693
419
|
}
|
|
694
|
-
|
|
695
|
-
|
|
420
|
+
current = current.next;
|
|
421
|
+
}
|
|
422
|
+
return null;
|
|
696
423
|
};
|
|
697
424
|
/**
|
|
698
|
-
* The `
|
|
699
|
-
*
|
|
700
|
-
*
|
|
701
|
-
*
|
|
702
|
-
* @
|
|
703
|
-
*
|
|
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.
|
|
425
|
+
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
426
|
+
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node that you want to insert the
|
|
427
|
+
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
428
|
+
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
429
|
+
* @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
430
|
+
* inserted before the existing value, and `false` otherwise.
|
|
710
431
|
*/
|
|
711
|
-
SinglyLinkedList.prototype.
|
|
712
|
-
if (
|
|
713
|
-
|
|
714
|
-
var
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
var result;
|
|
718
|
-
if (start !== undefined) {
|
|
719
|
-
result = start;
|
|
720
|
-
}
|
|
721
|
-
else if (currentElement) {
|
|
722
|
-
result = currentElement.val;
|
|
723
|
-
currentElement = currentElement[nextNode];
|
|
432
|
+
SinglyLinkedList.prototype.insertBefore = function (existingValueOrNode, newValue) {
|
|
433
|
+
if (!this.head)
|
|
434
|
+
return false;
|
|
435
|
+
var existingValue;
|
|
436
|
+
if (existingValueOrNode instanceof (SinglyLinkedListNode)) {
|
|
437
|
+
existingValue = existingValueOrNode.val;
|
|
724
438
|
}
|
|
725
439
|
else {
|
|
726
|
-
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
440
|
+
existingValue = existingValueOrNode;
|
|
441
|
+
}
|
|
442
|
+
if (this.head.val === existingValue) {
|
|
443
|
+
this.unshift(newValue);
|
|
444
|
+
return true;
|
|
445
|
+
}
|
|
446
|
+
var current = this.head;
|
|
447
|
+
while (current.next) {
|
|
448
|
+
if (current.next.val === existingValue) {
|
|
449
|
+
var newNode = new SinglyLinkedListNode(newValue);
|
|
450
|
+
newNode.next = current.next;
|
|
451
|
+
current.next = newNode;
|
|
452
|
+
this.length++;
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
current = current.next;
|
|
732
456
|
}
|
|
733
|
-
return
|
|
734
|
-
};
|
|
735
|
-
/**
|
|
736
|
-
* The toArray() function converts a NodeVal object into an array of NodeVal objects.
|
|
737
|
-
* @returns An array of NodeVal objects.
|
|
738
|
-
*/
|
|
739
|
-
SinglyLinkedList.prototype.toArray = function () {
|
|
740
|
-
return __spreadArray([], __read(this), false);
|
|
741
|
-
};
|
|
742
|
-
/**
|
|
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.
|
|
749
|
-
*/
|
|
750
|
-
SinglyLinkedList.prototype.toString = function (separator) {
|
|
751
|
-
if (separator === void 0) { separator = ' '; }
|
|
752
|
-
return this.reduce(function (s, val) { return "".concat(s).concat(separator).concat(val); });
|
|
457
|
+
return false;
|
|
753
458
|
};
|
|
754
459
|
/**
|
|
755
|
-
* The function
|
|
460
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
461
|
+
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node in the linked list after which
|
|
462
|
+
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
463
|
+
* @param {T} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
464
|
+
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
465
|
+
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
756
466
|
*/
|
|
757
|
-
SinglyLinkedList.prototype
|
|
758
|
-
var
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
case 3: return [2 /*return*/];
|
|
467
|
+
SinglyLinkedList.prototype.insertAfter = function (existingValueOrNode, newValue) {
|
|
468
|
+
var existingNode;
|
|
469
|
+
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
470
|
+
existingNode = existingValueOrNode;
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
existingNode = this.findNode(existingValueOrNode);
|
|
474
|
+
}
|
|
475
|
+
if (existingNode) {
|
|
476
|
+
var newNode = new SinglyLinkedListNode(newValue);
|
|
477
|
+
newNode.next = existingNode.next;
|
|
478
|
+
existingNode.next = newNode;
|
|
479
|
+
if (existingNode === this.tail) {
|
|
480
|
+
this.tail = newNode;
|
|
772
481
|
}
|
|
773
|
-
|
|
482
|
+
this.length++;
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
return false;
|
|
774
486
|
};
|
|
775
487
|
/**
|
|
776
|
-
* The function
|
|
777
|
-
* @param {
|
|
778
|
-
*
|
|
779
|
-
* @returns The value of the removed node if the node is not null, otherwise undefined.
|
|
488
|
+
* The function counts the number of occurrences of a given value in a linked list.
|
|
489
|
+
* @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
490
|
+
* @returns The count of occurrences of the given value in the linked list.
|
|
780
491
|
*/
|
|
781
|
-
SinglyLinkedList.prototype.
|
|
782
|
-
|
|
492
|
+
SinglyLinkedList.prototype.countOccurrences = function (value) {
|
|
493
|
+
var count = 0;
|
|
494
|
+
var current = this.head;
|
|
495
|
+
while (current) {
|
|
496
|
+
if (current.val === value) {
|
|
497
|
+
count++;
|
|
498
|
+
}
|
|
499
|
+
current = current.next;
|
|
500
|
+
}
|
|
501
|
+
return count;
|
|
783
502
|
};
|
|
784
503
|
return SinglyLinkedList;
|
|
785
504
|
}());
|