min-heap-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
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { IterableElementBase } from "../base";
|
|
2
|
-
import { ElementCallback } from "../../types";
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* data-structure-typed
|
|
6
3
|
*
|
|
@@ -8,6 +5,9 @@ import { ElementCallback } from "../../types";
|
|
|
8
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
6
|
* @license MIT License
|
|
10
7
|
*/
|
|
8
|
+
import type { ElementCallback } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
10
|
+
|
|
11
11
|
export class DoublyLinkedListNode<E = any> {
|
|
12
12
|
value: E;
|
|
13
13
|
next: DoublyLinkedListNode<E> | undefined;
|
|
@@ -25,15 +25,21 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* 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.
|
|
30
|
+
* 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.
|
|
31
|
+
* 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.
|
|
32
|
+
* 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.
|
|
33
|
+
*/
|
|
28
34
|
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
29
35
|
/**
|
|
30
|
-
* The constructor initializes the linked list with an empty head, tail, and
|
|
36
|
+
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
31
37
|
*/
|
|
32
38
|
constructor(elements?: Iterable<E>) {
|
|
33
39
|
super();
|
|
34
40
|
this._head = undefined;
|
|
35
41
|
this._tail = undefined;
|
|
36
|
-
this.
|
|
42
|
+
this._size = 0;
|
|
37
43
|
if (elements) {
|
|
38
44
|
for (const el of elements) {
|
|
39
45
|
this.push(el);
|
|
@@ -53,23 +59,19 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
53
59
|
return this._tail;
|
|
54
60
|
}
|
|
55
61
|
|
|
56
|
-
protected
|
|
57
|
-
|
|
58
|
-
get length(): number {
|
|
59
|
-
return this._length;
|
|
60
|
-
}
|
|
62
|
+
protected _size: number;
|
|
61
63
|
|
|
62
64
|
get size(): number {
|
|
63
|
-
return this.
|
|
65
|
+
return this._size;
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
/**
|
|
67
|
-
* Time Complexity: O(n), where n is the
|
|
69
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
68
70
|
* Space Complexity: O(n)
|
|
69
71
|
*/
|
|
70
72
|
|
|
71
73
|
/**
|
|
72
|
-
* Time Complexity: O(n), where n is the
|
|
74
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
73
75
|
* Space Complexity: O(n)
|
|
74
76
|
*
|
|
75
77
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
@@ -97,7 +99,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
97
99
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
98
100
|
* @param {E} value - The value to be added to the linked list.
|
|
99
101
|
*/
|
|
100
|
-
push(value: E):
|
|
102
|
+
push(value: E): boolean {
|
|
101
103
|
const newNode = new DoublyLinkedListNode(value);
|
|
102
104
|
if (!this.head) {
|
|
103
105
|
this._head = newNode;
|
|
@@ -107,23 +109,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
107
109
|
this.tail!.next = newNode;
|
|
108
110
|
this._tail = newNode;
|
|
109
111
|
}
|
|
110
|
-
this.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Time Complexity: O(1)
|
|
115
|
-
* Space Complexity: O(1)
|
|
116
|
-
*/
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Time Complexity: O(1)
|
|
120
|
-
* Space Complexity: O(1)
|
|
121
|
-
*
|
|
122
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
123
|
-
* @param {E} value - The value to be added to the linked list.
|
|
124
|
-
*/
|
|
125
|
-
addLast(value: E): void {
|
|
126
|
-
this.push(value);
|
|
112
|
+
this._size++;
|
|
113
|
+
return true;
|
|
127
114
|
}
|
|
128
115
|
|
|
129
116
|
/**
|
|
@@ -149,27 +136,10 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
149
136
|
this._tail = removedNode.prev;
|
|
150
137
|
this.tail!.next = undefined;
|
|
151
138
|
}
|
|
152
|
-
this.
|
|
139
|
+
this._size--;
|
|
153
140
|
return removedNode.value;
|
|
154
141
|
}
|
|
155
142
|
|
|
156
|
-
/**
|
|
157
|
-
* Time Complexity: O(1)
|
|
158
|
-
* Space Complexity: O(1)
|
|
159
|
-
*/
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Time Complexity: O(1)
|
|
163
|
-
* Space Complexity: O(1)
|
|
164
|
-
*
|
|
165
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
166
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
167
|
-
* list is empty, it returns undefined.
|
|
168
|
-
*/
|
|
169
|
-
pollLast(): E | undefined {
|
|
170
|
-
return this.pop();
|
|
171
|
-
}
|
|
172
|
-
|
|
173
143
|
/**
|
|
174
144
|
* Time Complexity: O(1)
|
|
175
145
|
* Space Complexity: O(1)
|
|
@@ -193,27 +163,10 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
193
163
|
this._head = removedNode.next;
|
|
194
164
|
this.head!.prev = undefined;
|
|
195
165
|
}
|
|
196
|
-
this.
|
|
166
|
+
this._size--;
|
|
197
167
|
return removedNode.value;
|
|
198
168
|
}
|
|
199
169
|
|
|
200
|
-
/**
|
|
201
|
-
* Time Complexity: O(1)
|
|
202
|
-
* Space Complexity: O(1)
|
|
203
|
-
*/
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Time Complexity: O(1)
|
|
207
|
-
* Space Complexity: O(1)
|
|
208
|
-
*
|
|
209
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
210
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
211
|
-
* list.
|
|
212
|
-
*/
|
|
213
|
-
pollFirst(): E | undefined {
|
|
214
|
-
return this.shift();
|
|
215
|
-
}
|
|
216
|
-
|
|
217
170
|
/**
|
|
218
171
|
* Time Complexity: O(1)
|
|
219
172
|
* Space Complexity: O(1)
|
|
@@ -227,7 +180,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
227
180
|
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
228
181
|
* doubly linked list.
|
|
229
182
|
*/
|
|
230
|
-
unshift(value: E):
|
|
183
|
+
unshift(value: E): boolean {
|
|
231
184
|
const newNode = new DoublyLinkedListNode(value);
|
|
232
185
|
if (!this.head) {
|
|
233
186
|
this._head = newNode;
|
|
@@ -237,56 +190,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
237
190
|
this.head!.prev = newNode;
|
|
238
191
|
this._head = newNode;
|
|
239
192
|
}
|
|
240
|
-
this.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Time Complexity: O(1)
|
|
245
|
-
* Space Complexity: O(1)
|
|
246
|
-
*/
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Time Complexity: O(1)
|
|
250
|
-
* Space Complexity: O(1)
|
|
251
|
-
*
|
|
252
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
253
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
254
|
-
* doubly linked list.
|
|
255
|
-
*/
|
|
256
|
-
addFirst(value: E): void {
|
|
257
|
-
this.unshift(value);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
262
|
-
* Space Complexity: O(1)
|
|
263
|
-
*/
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
267
|
-
* Space Complexity: O(1)
|
|
268
|
-
*
|
|
269
|
-
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
270
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
271
|
-
*/
|
|
272
|
-
getFirst(): E | undefined {
|
|
273
|
-
return this.head?.value;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
278
|
-
* Space Complexity: O(1)
|
|
279
|
-
*/
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
283
|
-
* Space Complexity: O(1)
|
|
284
|
-
*
|
|
285
|
-
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
286
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
287
|
-
*/
|
|
288
|
-
getLast(): E | undefined {
|
|
289
|
-
return this.tail?.value;
|
|
193
|
+
this._size++;
|
|
194
|
+
return true;
|
|
290
195
|
}
|
|
291
196
|
|
|
292
197
|
/**
|
|
@@ -305,7 +210,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
305
210
|
* or the linked list is empty, it will return undefined.
|
|
306
211
|
*/
|
|
307
212
|
getAt(index: number): E | undefined {
|
|
308
|
-
if (index < 0 || index >= this.
|
|
213
|
+
if (index < 0 || index >= this.size) return undefined;
|
|
309
214
|
let current = this.head;
|
|
310
215
|
for (let i = 0; i < index; i++) {
|
|
311
216
|
current = current!.next;
|
|
@@ -330,7 +235,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
330
235
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
331
236
|
*/
|
|
332
237
|
getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
|
|
333
|
-
if (index < 0 || index >= this.
|
|
238
|
+
if (index < 0 || index >= this.size) return undefined;
|
|
334
239
|
let current = this.head;
|
|
335
240
|
for (let i = 0; i < index; i++) {
|
|
336
241
|
current = current!.next;
|
|
@@ -383,13 +288,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
383
288
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
384
289
|
* if the index is out of bounds.
|
|
385
290
|
*/
|
|
386
|
-
|
|
387
|
-
if (index < 0 || index > this.
|
|
291
|
+
addAt(index: number, value: E): boolean {
|
|
292
|
+
if (index < 0 || index > this.size) return false;
|
|
388
293
|
if (index === 0) {
|
|
389
294
|
this.unshift(value);
|
|
390
295
|
return true;
|
|
391
296
|
}
|
|
392
|
-
if (index === this.
|
|
297
|
+
if (index === this.size) {
|
|
393
298
|
this.push(value);
|
|
394
299
|
return true;
|
|
395
300
|
}
|
|
@@ -401,7 +306,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
401
306
|
newNode.next = nextNode;
|
|
402
307
|
prevNode!.next = newNode;
|
|
403
308
|
nextNode!.prev = newNode;
|
|
404
|
-
this.
|
|
309
|
+
this._size++;
|
|
405
310
|
return true;
|
|
406
311
|
}
|
|
407
312
|
|
|
@@ -414,7 +319,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
414
319
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
415
320
|
* Space Complexity: O(1)
|
|
416
321
|
*
|
|
417
|
-
* The `
|
|
322
|
+
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
418
323
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
419
324
|
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
420
325
|
* itself.
|
|
@@ -423,7 +328,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
423
328
|
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
424
329
|
* insertion fails.
|
|
425
330
|
*/
|
|
426
|
-
|
|
331
|
+
addBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
|
|
427
332
|
let existingNode;
|
|
428
333
|
|
|
429
334
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
@@ -443,7 +348,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
443
348
|
if (existingNode === this.head) {
|
|
444
349
|
this._head = newNode;
|
|
445
350
|
}
|
|
446
|
-
this.
|
|
351
|
+
this._size++;
|
|
447
352
|
return true;
|
|
448
353
|
}
|
|
449
354
|
|
|
@@ -459,7 +364,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
459
364
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
460
365
|
* Space Complexity: O(1)
|
|
461
366
|
*
|
|
462
|
-
* The `
|
|
367
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
463
368
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
464
369
|
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
465
370
|
* itself.
|
|
@@ -467,7 +372,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
467
372
|
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
468
373
|
* existing value or node is not found in the doubly linked list.
|
|
469
374
|
*/
|
|
470
|
-
|
|
375
|
+
addAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
|
|
471
376
|
let existingNode;
|
|
472
377
|
|
|
473
378
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
@@ -487,7 +392,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
487
392
|
if (existingNode === this.tail) {
|
|
488
393
|
this._tail = newNode;
|
|
489
394
|
}
|
|
490
|
-
this.
|
|
395
|
+
this._size++;
|
|
491
396
|
return true;
|
|
492
397
|
}
|
|
493
398
|
|
|
@@ -509,18 +414,24 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
509
414
|
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
510
415
|
* bounds.
|
|
511
416
|
*/
|
|
512
|
-
deleteAt(index: number):
|
|
513
|
-
if (index < 0 || index >= this.
|
|
514
|
-
if (index === 0)
|
|
515
|
-
|
|
417
|
+
deleteAt(index: number): boolean {
|
|
418
|
+
if (index < 0 || index >= this.size) return false;
|
|
419
|
+
if (index === 0) {
|
|
420
|
+
this.shift();
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
if (index === this.size - 1) {
|
|
424
|
+
this.pop();
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
516
427
|
|
|
517
428
|
const removedNode = this.getNodeAt(index);
|
|
518
429
|
const prevNode = removedNode!.prev;
|
|
519
430
|
const nextNode = removedNode!.next;
|
|
520
431
|
prevNode!.next = nextNode;
|
|
521
432
|
nextNode!.prev = prevNode;
|
|
522
|
-
this.
|
|
523
|
-
return
|
|
433
|
+
this._size--;
|
|
434
|
+
return true;
|
|
524
435
|
}
|
|
525
436
|
|
|
526
437
|
/**
|
|
@@ -557,7 +468,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
557
468
|
const nextNode = node.next;
|
|
558
469
|
prevNode!.next = nextNode;
|
|
559
470
|
nextNode!.prev = prevNode;
|
|
560
|
-
this.
|
|
471
|
+
this._size--;
|
|
561
472
|
}
|
|
562
473
|
return true;
|
|
563
474
|
}
|
|
@@ -565,20 +476,20 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
565
476
|
}
|
|
566
477
|
|
|
567
478
|
/**
|
|
568
|
-
* The function checks if a variable has a
|
|
479
|
+
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
569
480
|
* @returns A boolean value is being returned.
|
|
570
481
|
*/
|
|
571
482
|
isEmpty(): boolean {
|
|
572
|
-
return this.
|
|
483
|
+
return this.size === 0;
|
|
573
484
|
}
|
|
574
485
|
|
|
575
486
|
/**
|
|
576
|
-
* The `clear` function resets the linked list by setting the head, tail, and
|
|
487
|
+
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
577
488
|
*/
|
|
578
489
|
clear(): void {
|
|
579
490
|
this._head = undefined;
|
|
580
491
|
this._tail = undefined;
|
|
581
|
-
this.
|
|
492
|
+
this._size = 0;
|
|
582
493
|
}
|
|
583
494
|
|
|
584
495
|
/**
|
|
@@ -673,7 +584,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
673
584
|
*
|
|
674
585
|
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
675
586
|
*/
|
|
676
|
-
reverse():
|
|
587
|
+
reverse(): this {
|
|
677
588
|
let current = this.head;
|
|
678
589
|
[this._head, this._tail] = [this.tail, this.head];
|
|
679
590
|
while (current) {
|
|
@@ -681,6 +592,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
681
592
|
[current.prev, current.next] = [current.next, current.prev];
|
|
682
593
|
current = next;
|
|
683
594
|
}
|
|
595
|
+
return this;
|
|
684
596
|
}
|
|
685
597
|
|
|
686
598
|
/**
|
|
@@ -794,13 +706,103 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
794
706
|
return mappedList;
|
|
795
707
|
}
|
|
796
708
|
|
|
709
|
+
/**
|
|
710
|
+
* Time Complexity: O(1)
|
|
711
|
+
* Space Complexity: O(1)
|
|
712
|
+
*/
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Time Complexity: O(1)
|
|
716
|
+
* Space Complexity: O(1)
|
|
717
|
+
*
|
|
718
|
+
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
719
|
+
* @param {E} value - The value to be added to the linked list.
|
|
720
|
+
*/
|
|
721
|
+
addLast(value: E): boolean {
|
|
722
|
+
return this.push(value);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Time Complexity: O(1)
|
|
727
|
+
* Space Complexity: O(1)
|
|
728
|
+
*/
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Time Complexity: O(1)
|
|
732
|
+
* Space Complexity: O(1)
|
|
733
|
+
*
|
|
734
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
735
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
736
|
+
* list is empty, it returns undefined.
|
|
737
|
+
*/
|
|
738
|
+
pollLast(): E | undefined {
|
|
739
|
+
return this.pop();
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* Time Complexity: O(1)
|
|
744
|
+
* Space Complexity: O(1)
|
|
745
|
+
*/
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Time Complexity: O(1)
|
|
749
|
+
* Space Complexity: O(1)
|
|
750
|
+
*
|
|
751
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
752
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
753
|
+
* list.
|
|
754
|
+
*/
|
|
755
|
+
pollFirst(): E | undefined {
|
|
756
|
+
return this.shift();
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/**
|
|
760
|
+
* Time Complexity: O(1)
|
|
761
|
+
* Space Complexity: O(1)
|
|
762
|
+
*/
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Time Complexity: O(1)
|
|
766
|
+
* Space Complexity: O(1)
|
|
767
|
+
*
|
|
768
|
+
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
769
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
770
|
+
* doubly linked list.
|
|
771
|
+
*/
|
|
772
|
+
addFirst(value: E): void {
|
|
773
|
+
this.unshift(value);
|
|
774
|
+
}
|
|
775
|
+
|
|
797
776
|
/**
|
|
798
777
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
799
|
-
* Space Complexity: O(
|
|
778
|
+
* Space Complexity: O(1)
|
|
779
|
+
*/
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
783
|
+
* Space Complexity: O(1)
|
|
784
|
+
*
|
|
785
|
+
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
786
|
+
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
800
787
|
*/
|
|
788
|
+
get first(): E | undefined {
|
|
789
|
+
return this.head?.value;
|
|
790
|
+
}
|
|
801
791
|
|
|
802
|
-
|
|
803
|
-
|
|
792
|
+
/**
|
|
793
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
794
|
+
* Space Complexity: O(1)
|
|
795
|
+
*/
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
799
|
+
* Space Complexity: O(1)
|
|
800
|
+
*
|
|
801
|
+
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
802
|
+
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
803
|
+
*/
|
|
804
|
+
get last(): E | undefined {
|
|
805
|
+
return this.tail?.value;
|
|
804
806
|
}
|
|
805
807
|
|
|
806
808
|
/**
|