graph-typed 1.49.0 → 1.49.2
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/dist/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +10 -12
- package/dist/data-structures/binary-tree/avl-tree.js +3 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -58
- package/dist/data-structures/binary-tree/binary-tree.js +6 -6
- package/dist/data-structures/binary-tree/bst.d.ts +15 -15
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/data-structures/graph/abstract-graph.js +27 -31
- package/dist/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/data-structures/graph/directed-graph.js +1 -8
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -8
- package/dist/data-structures/hash/hash-map.d.ts +22 -10
- package/dist/data-structures/hash/hash-map.js +28 -16
- package/dist/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +20 -38
- package/dist/data-structures/heap/heap.js +22 -42
- package/dist/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/data-structures/heap/max-heap.js +10 -7
- package/dist/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/data-structures/heap/min-heap.js +10 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -136
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -23
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -49
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/data-structures/queue/deque.d.ts +76 -80
- package/dist/data-structures/queue/deque.js +106 -122
- package/dist/data-structures/queue/queue.d.ts +30 -16
- package/dist/data-structures/queue/queue.js +31 -24
- package/dist/data-structures/stack/stack.d.ts +17 -8
- package/dist/data-structures/stack/stack.js +9 -9
- package/dist/data-structures/trie/trie.d.ts +14 -5
- package/dist/data-structures/trie/trie.js +13 -13
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +32 -8
- package/dist/types/common.js +22 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +74 -77
- package/src/data-structures/binary-tree/bst.ts +18 -17
- package/src/data-structures/binary-tree/rb-tree.ts +17 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +40 -24
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +33 -60
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
- package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +129 -144
- package/src/data-structures/queue/queue.ts +37 -26
- package/src/data-structures/stack/stack.ts +20 -14
- package/src/data-structures/trie/trie.ts +18 -13
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
|
@@ -2,13 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
4
4
|
const base_1 = require("../base");
|
|
5
|
-
/**
|
|
6
|
-
* data-structure-typed
|
|
7
|
-
*
|
|
8
|
-
* @author Tyler Zeng
|
|
9
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
10
|
-
* @license MIT License
|
|
11
|
-
*/
|
|
12
5
|
class DoublyLinkedListNode {
|
|
13
6
|
/**
|
|
14
7
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
@@ -22,15 +15,21 @@ class DoublyLinkedListNode {
|
|
|
22
15
|
}
|
|
23
16
|
}
|
|
24
17
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
18
|
+
/**
|
|
19
|
+
* 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
20
|
+
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
|
21
|
+
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
22
|
+
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
23
|
+
*/
|
|
25
24
|
class DoublyLinkedList extends base_1.IterableElementBase {
|
|
26
25
|
/**
|
|
27
|
-
* The constructor initializes the linked list with an empty head, tail, and
|
|
26
|
+
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
28
27
|
*/
|
|
29
28
|
constructor(elements) {
|
|
30
29
|
super();
|
|
31
30
|
this._head = undefined;
|
|
32
31
|
this._tail = undefined;
|
|
33
|
-
this.
|
|
32
|
+
this._size = 0;
|
|
34
33
|
if (elements) {
|
|
35
34
|
for (const el of elements) {
|
|
36
35
|
this.push(el);
|
|
@@ -43,18 +42,15 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
43
42
|
get tail() {
|
|
44
43
|
return this._tail;
|
|
45
44
|
}
|
|
46
|
-
get length() {
|
|
47
|
-
return this._length;
|
|
48
|
-
}
|
|
49
45
|
get size() {
|
|
50
|
-
return this.
|
|
46
|
+
return this._size;
|
|
51
47
|
}
|
|
52
48
|
/**
|
|
53
|
-
* Time Complexity: O(n), where n is the
|
|
49
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
54
50
|
* Space Complexity: O(n)
|
|
55
51
|
*/
|
|
56
52
|
/**
|
|
57
|
-
* Time Complexity: O(n), where n is the
|
|
53
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
58
54
|
* Space Complexity: O(n)
|
|
59
55
|
*
|
|
60
56
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
@@ -91,21 +87,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
91
87
|
this.tail.next = newNode;
|
|
92
88
|
this._tail = newNode;
|
|
93
89
|
}
|
|
94
|
-
this.
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Time Complexity: O(1)
|
|
98
|
-
* Space Complexity: O(1)
|
|
99
|
-
*/
|
|
100
|
-
/**
|
|
101
|
-
* Time Complexity: O(1)
|
|
102
|
-
* Space Complexity: O(1)
|
|
103
|
-
*
|
|
104
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
105
|
-
* @param {E} value - The value to be added to the linked list.
|
|
106
|
-
*/
|
|
107
|
-
addLast(value) {
|
|
108
|
-
this.push(value);
|
|
90
|
+
this._size++;
|
|
91
|
+
return true;
|
|
109
92
|
}
|
|
110
93
|
/**
|
|
111
94
|
* Time Complexity: O(1)
|
|
@@ -131,24 +114,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
131
114
|
this._tail = removedNode.prev;
|
|
132
115
|
this.tail.next = undefined;
|
|
133
116
|
}
|
|
134
|
-
this.
|
|
117
|
+
this._size--;
|
|
135
118
|
return removedNode.value;
|
|
136
119
|
}
|
|
137
|
-
/**
|
|
138
|
-
* Time Complexity: O(1)
|
|
139
|
-
* Space Complexity: O(1)
|
|
140
|
-
*/
|
|
141
|
-
/**
|
|
142
|
-
* Time Complexity: O(1)
|
|
143
|
-
* Space Complexity: O(1)
|
|
144
|
-
*
|
|
145
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
146
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
147
|
-
* list is empty, it returns undefined.
|
|
148
|
-
*/
|
|
149
|
-
pollLast() {
|
|
150
|
-
return this.pop();
|
|
151
|
-
}
|
|
152
120
|
/**
|
|
153
121
|
* Time Complexity: O(1)
|
|
154
122
|
* Space Complexity: O(1)
|
|
@@ -173,24 +141,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
173
141
|
this._head = removedNode.next;
|
|
174
142
|
this.head.prev = undefined;
|
|
175
143
|
}
|
|
176
|
-
this.
|
|
144
|
+
this._size--;
|
|
177
145
|
return removedNode.value;
|
|
178
146
|
}
|
|
179
|
-
/**
|
|
180
|
-
* Time Complexity: O(1)
|
|
181
|
-
* Space Complexity: O(1)
|
|
182
|
-
*/
|
|
183
|
-
/**
|
|
184
|
-
* Time Complexity: O(1)
|
|
185
|
-
* Space Complexity: O(1)
|
|
186
|
-
*
|
|
187
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
188
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
189
|
-
* list.
|
|
190
|
-
*/
|
|
191
|
-
pollFirst() {
|
|
192
|
-
return this.shift();
|
|
193
|
-
}
|
|
194
147
|
/**
|
|
195
148
|
* Time Complexity: O(1)
|
|
196
149
|
* Space Complexity: O(1)
|
|
@@ -214,52 +167,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
214
167
|
this.head.prev = newNode;
|
|
215
168
|
this._head = newNode;
|
|
216
169
|
}
|
|
217
|
-
this.
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Time Complexity: O(1)
|
|
221
|
-
* Space Complexity: O(1)
|
|
222
|
-
*/
|
|
223
|
-
/**
|
|
224
|
-
* Time Complexity: O(1)
|
|
225
|
-
* Space Complexity: O(1)
|
|
226
|
-
*
|
|
227
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
228
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
229
|
-
* doubly linked list.
|
|
230
|
-
*/
|
|
231
|
-
addFirst(value) {
|
|
232
|
-
this.unshift(value);
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
236
|
-
* Space Complexity: O(1)
|
|
237
|
-
*/
|
|
238
|
-
/**
|
|
239
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
240
|
-
* Space Complexity: O(1)
|
|
241
|
-
*
|
|
242
|
-
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
243
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
244
|
-
*/
|
|
245
|
-
getFirst() {
|
|
246
|
-
var _a;
|
|
247
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
251
|
-
* Space Complexity: O(1)
|
|
252
|
-
*/
|
|
253
|
-
/**
|
|
254
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
255
|
-
* Space Complexity: O(1)
|
|
256
|
-
*
|
|
257
|
-
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
258
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
259
|
-
*/
|
|
260
|
-
getLast() {
|
|
261
|
-
var _a;
|
|
262
|
-
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.value;
|
|
170
|
+
this._size++;
|
|
171
|
+
return true;
|
|
263
172
|
}
|
|
264
173
|
/**
|
|
265
174
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -276,7 +185,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
276
185
|
* or the linked list is empty, it will return undefined.
|
|
277
186
|
*/
|
|
278
187
|
getAt(index) {
|
|
279
|
-
if (index < 0 || index >= this.
|
|
188
|
+
if (index < 0 || index >= this.size)
|
|
280
189
|
return undefined;
|
|
281
190
|
let current = this.head;
|
|
282
191
|
for (let i = 0; i < index; i++) {
|
|
@@ -300,7 +209,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
300
209
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
301
210
|
*/
|
|
302
211
|
getNodeAt(index) {
|
|
303
|
-
if (index < 0 || index >= this.
|
|
212
|
+
if (index < 0 || index >= this.size)
|
|
304
213
|
return undefined;
|
|
305
214
|
let current = this.head;
|
|
306
215
|
for (let i = 0; i < index; i++) {
|
|
@@ -348,14 +257,14 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
348
257
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
349
258
|
* if the index is out of bounds.
|
|
350
259
|
*/
|
|
351
|
-
|
|
352
|
-
if (index < 0 || index > this.
|
|
260
|
+
addAt(index, value) {
|
|
261
|
+
if (index < 0 || index > this.size)
|
|
353
262
|
return false;
|
|
354
263
|
if (index === 0) {
|
|
355
264
|
this.unshift(value);
|
|
356
265
|
return true;
|
|
357
266
|
}
|
|
358
|
-
if (index === this.
|
|
267
|
+
if (index === this.size) {
|
|
359
268
|
this.push(value);
|
|
360
269
|
return true;
|
|
361
270
|
}
|
|
@@ -366,7 +275,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
366
275
|
newNode.next = nextNode;
|
|
367
276
|
prevNode.next = newNode;
|
|
368
277
|
nextNode.prev = newNode;
|
|
369
|
-
this.
|
|
278
|
+
this._size++;
|
|
370
279
|
return true;
|
|
371
280
|
}
|
|
372
281
|
/**
|
|
@@ -377,7 +286,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
377
286
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
378
287
|
* Space Complexity: O(1)
|
|
379
288
|
*
|
|
380
|
-
* The `
|
|
289
|
+
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
381
290
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
382
291
|
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
383
292
|
* itself.
|
|
@@ -386,7 +295,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
386
295
|
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
387
296
|
* insertion fails.
|
|
388
297
|
*/
|
|
389
|
-
|
|
298
|
+
addBefore(existingValueOrNode, newValue) {
|
|
390
299
|
let existingNode;
|
|
391
300
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
392
301
|
existingNode = existingValueOrNode;
|
|
@@ -405,7 +314,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
405
314
|
if (existingNode === this.head) {
|
|
406
315
|
this._head = newNode;
|
|
407
316
|
}
|
|
408
|
-
this.
|
|
317
|
+
this._size++;
|
|
409
318
|
return true;
|
|
410
319
|
}
|
|
411
320
|
return false;
|
|
@@ -418,7 +327,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
418
327
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
419
328
|
* Space Complexity: O(1)
|
|
420
329
|
*
|
|
421
|
-
* The `
|
|
330
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
422
331
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
423
332
|
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
424
333
|
* itself.
|
|
@@ -426,7 +335,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
426
335
|
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
427
336
|
* existing value or node is not found in the doubly linked list.
|
|
428
337
|
*/
|
|
429
|
-
|
|
338
|
+
addAfter(existingValueOrNode, newValue) {
|
|
430
339
|
let existingNode;
|
|
431
340
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
432
341
|
existingNode = existingValueOrNode;
|
|
@@ -445,7 +354,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
445
354
|
if (existingNode === this.tail) {
|
|
446
355
|
this._tail = newNode;
|
|
447
356
|
}
|
|
448
|
-
this.
|
|
357
|
+
this._size++;
|
|
449
358
|
return true;
|
|
450
359
|
}
|
|
451
360
|
return false;
|
|
@@ -465,19 +374,23 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
465
374
|
* bounds.
|
|
466
375
|
*/
|
|
467
376
|
deleteAt(index) {
|
|
468
|
-
if (index < 0 || index >= this.
|
|
469
|
-
return
|
|
470
|
-
if (index === 0)
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
377
|
+
if (index < 0 || index >= this.size)
|
|
378
|
+
return false;
|
|
379
|
+
if (index === 0) {
|
|
380
|
+
this.shift();
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
if (index === this.size - 1) {
|
|
384
|
+
this.pop();
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
474
387
|
const removedNode = this.getNodeAt(index);
|
|
475
388
|
const prevNode = removedNode.prev;
|
|
476
389
|
const nextNode = removedNode.next;
|
|
477
390
|
prevNode.next = nextNode;
|
|
478
391
|
nextNode.prev = prevNode;
|
|
479
|
-
this.
|
|
480
|
-
return
|
|
392
|
+
this._size--;
|
|
393
|
+
return true;
|
|
481
394
|
}
|
|
482
395
|
/**
|
|
483
396
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -513,26 +426,26 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
513
426
|
const nextNode = node.next;
|
|
514
427
|
prevNode.next = nextNode;
|
|
515
428
|
nextNode.prev = prevNode;
|
|
516
|
-
this.
|
|
429
|
+
this._size--;
|
|
517
430
|
}
|
|
518
431
|
return true;
|
|
519
432
|
}
|
|
520
433
|
return false;
|
|
521
434
|
}
|
|
522
435
|
/**
|
|
523
|
-
* The function checks if a variable has a
|
|
436
|
+
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
524
437
|
* @returns A boolean value is being returned.
|
|
525
438
|
*/
|
|
526
439
|
isEmpty() {
|
|
527
|
-
return this.
|
|
440
|
+
return this.size === 0;
|
|
528
441
|
}
|
|
529
442
|
/**
|
|
530
|
-
* The `clear` function resets the linked list by setting the head, tail, and
|
|
443
|
+
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
531
444
|
*/
|
|
532
445
|
clear() {
|
|
533
446
|
this._head = undefined;
|
|
534
447
|
this._tail = undefined;
|
|
535
|
-
this.
|
|
448
|
+
this._size = 0;
|
|
536
449
|
}
|
|
537
450
|
/**
|
|
538
451
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -627,6 +540,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
627
540
|
[current.prev, current.next] = [current.next, current.prev];
|
|
628
541
|
current = next;
|
|
629
542
|
}
|
|
543
|
+
return this;
|
|
630
544
|
}
|
|
631
545
|
/**
|
|
632
546
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -730,12 +644,94 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
730
644
|
}
|
|
731
645
|
return mappedList;
|
|
732
646
|
}
|
|
647
|
+
/**
|
|
648
|
+
* Time Complexity: O(1)
|
|
649
|
+
* Space Complexity: O(1)
|
|
650
|
+
*/
|
|
651
|
+
/**
|
|
652
|
+
* Time Complexity: O(1)
|
|
653
|
+
* Space Complexity: O(1)
|
|
654
|
+
*
|
|
655
|
+
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
656
|
+
* @param {E} value - The value to be added to the linked list.
|
|
657
|
+
*/
|
|
658
|
+
addLast(value) {
|
|
659
|
+
return this.push(value);
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Time Complexity: O(1)
|
|
663
|
+
* Space Complexity: O(1)
|
|
664
|
+
*/
|
|
665
|
+
/**
|
|
666
|
+
* Time Complexity: O(1)
|
|
667
|
+
* Space Complexity: O(1)
|
|
668
|
+
*
|
|
669
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
670
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
671
|
+
* list is empty, it returns undefined.
|
|
672
|
+
*/
|
|
673
|
+
pollLast() {
|
|
674
|
+
return this.pop();
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Time Complexity: O(1)
|
|
678
|
+
* Space Complexity: O(1)
|
|
679
|
+
*/
|
|
680
|
+
/**
|
|
681
|
+
* Time Complexity: O(1)
|
|
682
|
+
* Space Complexity: O(1)
|
|
683
|
+
*
|
|
684
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
685
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
686
|
+
* list.
|
|
687
|
+
*/
|
|
688
|
+
pollFirst() {
|
|
689
|
+
return this.shift();
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Time Complexity: O(1)
|
|
693
|
+
* Space Complexity: O(1)
|
|
694
|
+
*/
|
|
695
|
+
/**
|
|
696
|
+
* Time Complexity: O(1)
|
|
697
|
+
* Space Complexity: O(1)
|
|
698
|
+
*
|
|
699
|
+
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
700
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
701
|
+
* doubly linked list.
|
|
702
|
+
*/
|
|
703
|
+
addFirst(value) {
|
|
704
|
+
this.unshift(value);
|
|
705
|
+
}
|
|
733
706
|
/**
|
|
734
707
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
735
|
-
* Space Complexity: O(
|
|
708
|
+
* Space Complexity: O(1)
|
|
709
|
+
*/
|
|
710
|
+
/**
|
|
711
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
712
|
+
* Space Complexity: O(1)
|
|
713
|
+
*
|
|
714
|
+
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
715
|
+
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
716
|
+
*/
|
|
717
|
+
get first() {
|
|
718
|
+
var _a;
|
|
719
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
723
|
+
* Space Complexity: O(1)
|
|
736
724
|
*/
|
|
737
|
-
|
|
738
|
-
|
|
725
|
+
/**
|
|
726
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
727
|
+
* Space Complexity: O(1)
|
|
728
|
+
*
|
|
729
|
+
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
730
|
+
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
731
|
+
*/
|
|
732
|
+
get last() {
|
|
733
|
+
var _a;
|
|
734
|
+
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.value;
|
|
739
735
|
}
|
|
740
736
|
/**
|
|
741
737
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { IterableElementBase } from "../base";
|
|
2
|
-
import { ElementCallback } from "../../types";
|
|
3
1
|
/**
|
|
4
2
|
* data-structure-typed
|
|
5
3
|
*
|
|
@@ -7,6 +5,8 @@ import { ElementCallback } from "../../types";
|
|
|
7
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
8
6
|
* @license MIT License
|
|
9
7
|
*/
|
|
8
|
+
import type { ElementCallback } from "../../types";
|
|
9
|
+
import { IterableElementBase } from "../base";
|
|
10
10
|
export declare class SinglyLinkedListNode<E = any> {
|
|
11
11
|
value: E;
|
|
12
12
|
next: SinglyLinkedListNode<E> | undefined;
|
|
@@ -26,8 +26,8 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
26
26
|
get head(): SinglyLinkedListNode<E> | undefined;
|
|
27
27
|
protected _tail: SinglyLinkedListNode<E> | undefined;
|
|
28
28
|
get tail(): SinglyLinkedListNode<E> | undefined;
|
|
29
|
-
protected
|
|
30
|
-
get
|
|
29
|
+
protected _size: number;
|
|
30
|
+
get size(): number;
|
|
31
31
|
/**
|
|
32
32
|
* Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
|
|
33
33
|
* Space Complexity: O(n) - Linear space, as it creates a new node for each element in the array.
|
|
@@ -54,7 +54,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
54
54
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
55
55
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
56
56
|
*/
|
|
57
|
-
push(value: E):
|
|
57
|
+
push(value: E): boolean;
|
|
58
58
|
/**
|
|
59
59
|
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
60
60
|
* Space Complexity: O(1) - Constant space, as it only creates a new node.
|
|
@@ -67,7 +67,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
67
67
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
68
68
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
69
69
|
*/
|
|
70
|
-
addLast(value: E):
|
|
70
|
+
addLast(value: E): boolean;
|
|
71
71
|
/**
|
|
72
72
|
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
73
73
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -132,7 +132,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
132
132
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
133
133
|
* linked list.
|
|
134
134
|
*/
|
|
135
|
-
unshift(value: E):
|
|
135
|
+
unshift(value: E): boolean;
|
|
136
136
|
/**
|
|
137
137
|
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
138
138
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -145,7 +145,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
145
145
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
146
146
|
* linked list.
|
|
147
147
|
*/
|
|
148
|
-
addFirst(value: E):
|
|
148
|
+
addFirst(value: E): boolean;
|
|
149
149
|
/**
|
|
150
150
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
151
151
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -190,7 +190,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
190
190
|
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
191
191
|
* bounds.
|
|
192
192
|
*/
|
|
193
|
-
deleteAt(index: number):
|
|
193
|
+
deleteAt(index: number): boolean;
|
|
194
194
|
/**
|
|
195
195
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
196
196
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -205,7 +205,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
205
205
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
206
206
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
207
207
|
*/
|
|
208
|
-
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined
|
|
208
|
+
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined): boolean;
|
|
209
209
|
/**
|
|
210
210
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
211
211
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -214,7 +214,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
214
214
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
215
215
|
* Space Complexity: O(1) - Constant space.
|
|
216
216
|
*
|
|
217
|
-
* The `
|
|
217
|
+
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
218
218
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
219
219
|
* linked list. It is of type number.
|
|
220
220
|
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
@@ -222,7 +222,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
222
222
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
223
223
|
* if the index is out of bounds.
|
|
224
224
|
*/
|
|
225
|
-
|
|
225
|
+
addAt(index: number, value: E): boolean;
|
|
226
226
|
/**
|
|
227
227
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
228
228
|
* whether it is empty or not.
|
|
@@ -256,7 +256,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
256
256
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
257
257
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
258
258
|
*/
|
|
259
|
-
reverse():
|
|
259
|
+
reverse(): this;
|
|
260
260
|
/**
|
|
261
261
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
262
262
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -309,14 +309,14 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
309
309
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
310
310
|
* Space Complexity: O(1) - Constant space.
|
|
311
311
|
*
|
|
312
|
-
* The `
|
|
312
|
+
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
313
313
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
314
314
|
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
315
315
|
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
316
|
-
* @returns The method `
|
|
316
|
+
* @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
317
317
|
* inserted before the existing value, and `false` otherwise.
|
|
318
318
|
*/
|
|
319
|
-
|
|
319
|
+
addBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
320
320
|
/**
|
|
321
321
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
322
322
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -325,14 +325,14 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
325
325
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
326
326
|
* Space Complexity: O(1) - Constant space.
|
|
327
327
|
*
|
|
328
|
-
* The `
|
|
328
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
329
329
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
330
330
|
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
331
331
|
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
332
332
|
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
333
333
|
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
334
334
|
*/
|
|
335
|
-
|
|
335
|
+
addAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
336
336
|
/**
|
|
337
337
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
338
338
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -387,10 +387,5 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
387
387
|
* of applying the provided `callback` function to each element in the original list.
|
|
388
388
|
*/
|
|
389
389
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T>;
|
|
390
|
-
/**
|
|
391
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
392
|
-
* Space Complexity: O(n)
|
|
393
|
-
*/
|
|
394
|
-
print(): void;
|
|
395
390
|
protected _getIterator(): IterableIterator<E>;
|
|
396
391
|
}
|