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