avl-tree-typed 1.49.1 → 1.49.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/dist/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/data-structures/graph/abstract-graph.js +43 -12
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +2 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/data-structures/hash/hash-map.js +16 -15
- package/dist/data-structures/heap/heap.d.ts +6 -35
- package/dist/data-structures/heap/heap.js +10 -42
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/data-structures/linked-list/doubly-linked-list.js +143 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/data-structures/queue/deque.d.ts +70 -75
- package/dist/data-structures/queue/deque.js +100 -110
- package/dist/data-structures/queue/queue.d.ts +37 -38
- package/dist/data-structures/queue/queue.js +46 -49
- package/dist/data-structures/stack/stack.d.ts +2 -3
- package/dist/data-structures/stack/stack.js +2 -5
- package/dist/data-structures/trie/trie.d.ts +1 -2
- package/dist/data-structures/trie/trie.js +2 -5
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
|
@@ -33,13 +33,13 @@ export class DoublyLinkedListNode<E = any> {
|
|
|
33
33
|
*/
|
|
34
34
|
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
35
35
|
/**
|
|
36
|
-
* 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.
|
|
37
37
|
*/
|
|
38
38
|
constructor(elements?: Iterable<E>) {
|
|
39
39
|
super();
|
|
40
40
|
this._head = undefined;
|
|
41
41
|
this._tail = undefined;
|
|
42
|
-
this.
|
|
42
|
+
this._size = 0;
|
|
43
43
|
if (elements) {
|
|
44
44
|
for (const el of elements) {
|
|
45
45
|
this.push(el);
|
|
@@ -59,23 +59,51 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
59
59
|
return this._tail;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
protected
|
|
63
|
-
|
|
64
|
-
get length(): number {
|
|
65
|
-
return this._length;
|
|
66
|
-
}
|
|
62
|
+
protected _size: number;
|
|
67
63
|
|
|
68
64
|
get size(): number {
|
|
69
|
-
return this.
|
|
65
|
+
return this._size;
|
|
70
66
|
}
|
|
71
67
|
|
|
72
68
|
/**
|
|
73
|
-
* Time Complexity: O(n), where n is the
|
|
69
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
74
70
|
* Space Complexity: O(n)
|
|
75
71
|
*/
|
|
76
72
|
|
|
77
73
|
/**
|
|
78
|
-
* Time Complexity: O(n), where n is the
|
|
74
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
75
|
+
* Space Complexity: O(1)
|
|
76
|
+
*
|
|
77
|
+
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
78
|
+
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
79
|
+
*/
|
|
80
|
+
get first(): E | undefined {
|
|
81
|
+
return this.head?.value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Time Complexity: O(1)
|
|
86
|
+
* Space Complexity: O(1)
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*
|
|
93
|
+
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
94
|
+
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
95
|
+
*/
|
|
96
|
+
get last(): E | undefined {
|
|
97
|
+
return this.tail?.value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Time Complexity: O(1)
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
79
107
|
* Space Complexity: O(n)
|
|
80
108
|
*
|
|
81
109
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
@@ -103,7 +131,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
103
131
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
104
132
|
* @param {E} value - The value to be added to the linked list.
|
|
105
133
|
*/
|
|
106
|
-
push(value: E):
|
|
134
|
+
push(value: E): boolean {
|
|
107
135
|
const newNode = new DoublyLinkedListNode(value);
|
|
108
136
|
if (!this.head) {
|
|
109
137
|
this._head = newNode;
|
|
@@ -113,23 +141,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
113
141
|
this.tail!.next = newNode;
|
|
114
142
|
this._tail = newNode;
|
|
115
143
|
}
|
|
116
|
-
this.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Time Complexity: O(1)
|
|
121
|
-
* Space Complexity: O(1)
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Time Complexity: O(1)
|
|
126
|
-
* Space Complexity: O(1)
|
|
127
|
-
*
|
|
128
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
129
|
-
* @param {E} value - The value to be added to the linked list.
|
|
130
|
-
*/
|
|
131
|
-
addLast(value: E): void {
|
|
132
|
-
this.push(value);
|
|
144
|
+
this._size++;
|
|
145
|
+
return true;
|
|
133
146
|
}
|
|
134
147
|
|
|
135
148
|
/**
|
|
@@ -155,29 +168,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
155
168
|
this._tail = removedNode.prev;
|
|
156
169
|
this.tail!.next = undefined;
|
|
157
170
|
}
|
|
158
|
-
this.
|
|
171
|
+
this._size--;
|
|
159
172
|
return removedNode.value;
|
|
160
173
|
}
|
|
161
174
|
|
|
162
175
|
/**
|
|
163
|
-
* Time Complexity: O(
|
|
164
|
-
* Space Complexity: O(1)
|
|
165
|
-
*/
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Time Complexity: O(1)
|
|
169
|
-
* Space Complexity: O(1)
|
|
170
|
-
*
|
|
171
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
172
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
173
|
-
* list is empty, it returns undefined.
|
|
174
|
-
*/
|
|
175
|
-
pollLast(): E | undefined {
|
|
176
|
-
return this.pop();
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Time Complexity: O(1)
|
|
176
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
181
177
|
* Space Complexity: O(1)
|
|
182
178
|
*/
|
|
183
179
|
|
|
@@ -199,29 +195,12 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
199
195
|
this._head = removedNode.next;
|
|
200
196
|
this.head!.prev = undefined;
|
|
201
197
|
}
|
|
202
|
-
this.
|
|
198
|
+
this._size--;
|
|
203
199
|
return removedNode.value;
|
|
204
200
|
}
|
|
205
201
|
|
|
206
202
|
/**
|
|
207
|
-
* Time Complexity: O(
|
|
208
|
-
* Space Complexity: O(1)
|
|
209
|
-
*/
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Time Complexity: O(1)
|
|
213
|
-
* Space Complexity: O(1)
|
|
214
|
-
*
|
|
215
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
216
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
217
|
-
* list.
|
|
218
|
-
*/
|
|
219
|
-
pollFirst(): E | undefined {
|
|
220
|
-
return this.shift();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Time Complexity: O(1)
|
|
203
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
225
204
|
* Space Complexity: O(1)
|
|
226
205
|
*/
|
|
227
206
|
|
|
@@ -233,7 +212,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
233
212
|
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
234
213
|
* doubly linked list.
|
|
235
214
|
*/
|
|
236
|
-
unshift(value: E):
|
|
215
|
+
unshift(value: E): boolean {
|
|
237
216
|
const newNode = new DoublyLinkedListNode(value);
|
|
238
217
|
if (!this.head) {
|
|
239
218
|
this._head = newNode;
|
|
@@ -243,56 +222,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
243
222
|
this.head!.prev = newNode;
|
|
244
223
|
this._head = newNode;
|
|
245
224
|
}
|
|
246
|
-
this.
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Time Complexity: O(1)
|
|
251
|
-
* Space Complexity: O(1)
|
|
252
|
-
*/
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Time Complexity: O(1)
|
|
256
|
-
* Space Complexity: O(1)
|
|
257
|
-
*
|
|
258
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
259
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
260
|
-
* doubly linked list.
|
|
261
|
-
*/
|
|
262
|
-
addFirst(value: E): void {
|
|
263
|
-
this.unshift(value);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
268
|
-
* Space Complexity: O(1)
|
|
269
|
-
*/
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
273
|
-
* Space Complexity: O(1)
|
|
274
|
-
*
|
|
275
|
-
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
276
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
277
|
-
*/
|
|
278
|
-
getFirst(): E | undefined {
|
|
279
|
-
return this.head?.value;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
284
|
-
* Space Complexity: O(1)
|
|
285
|
-
*/
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
289
|
-
* Space Complexity: O(1)
|
|
290
|
-
*
|
|
291
|
-
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
292
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
293
|
-
*/
|
|
294
|
-
getLast(): E | undefined {
|
|
295
|
-
return this.tail?.value;
|
|
225
|
+
this._size++;
|
|
226
|
+
return true;
|
|
296
227
|
}
|
|
297
228
|
|
|
298
229
|
/**
|
|
@@ -311,7 +242,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
311
242
|
* or the linked list is empty, it will return undefined.
|
|
312
243
|
*/
|
|
313
244
|
getAt(index: number): E | undefined {
|
|
314
|
-
if (index < 0 || index >= this.
|
|
245
|
+
if (index < 0 || index >= this.size) return undefined;
|
|
315
246
|
let current = this.head;
|
|
316
247
|
for (let i = 0; i < index; i++) {
|
|
317
248
|
current = current!.next;
|
|
@@ -336,7 +267,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
336
267
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
337
268
|
*/
|
|
338
269
|
getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
|
|
339
|
-
if (index < 0 || index >= this.
|
|
270
|
+
if (index < 0 || index >= this.size) return undefined;
|
|
340
271
|
let current = this.head;
|
|
341
272
|
for (let i = 0; i < index; i++) {
|
|
342
273
|
current = current!.next;
|
|
@@ -389,13 +320,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
389
320
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
390
321
|
* if the index is out of bounds.
|
|
391
322
|
*/
|
|
392
|
-
|
|
393
|
-
if (index < 0 || index > this.
|
|
323
|
+
addAt(index: number, value: E): boolean {
|
|
324
|
+
if (index < 0 || index > this.size) return false;
|
|
394
325
|
if (index === 0) {
|
|
395
326
|
this.unshift(value);
|
|
396
327
|
return true;
|
|
397
328
|
}
|
|
398
|
-
if (index === this.
|
|
329
|
+
if (index === this.size) {
|
|
399
330
|
this.push(value);
|
|
400
331
|
return true;
|
|
401
332
|
}
|
|
@@ -407,7 +338,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
407
338
|
newNode.next = nextNode;
|
|
408
339
|
prevNode!.next = newNode;
|
|
409
340
|
nextNode!.prev = newNode;
|
|
410
|
-
this.
|
|
341
|
+
this._size++;
|
|
411
342
|
return true;
|
|
412
343
|
}
|
|
413
344
|
|
|
@@ -420,7 +351,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
420
351
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
421
352
|
* Space Complexity: O(1)
|
|
422
353
|
*
|
|
423
|
-
* The `
|
|
354
|
+
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
424
355
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
425
356
|
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
426
357
|
* itself.
|
|
@@ -429,7 +360,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
429
360
|
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
430
361
|
* insertion fails.
|
|
431
362
|
*/
|
|
432
|
-
|
|
363
|
+
addBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
|
|
433
364
|
let existingNode;
|
|
434
365
|
|
|
435
366
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
@@ -449,7 +380,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
449
380
|
if (existingNode === this.head) {
|
|
450
381
|
this._head = newNode;
|
|
451
382
|
}
|
|
452
|
-
this.
|
|
383
|
+
this._size++;
|
|
453
384
|
return true;
|
|
454
385
|
}
|
|
455
386
|
|
|
@@ -465,7 +396,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
465
396
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
466
397
|
* Space Complexity: O(1)
|
|
467
398
|
*
|
|
468
|
-
* The `
|
|
399
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
469
400
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
470
401
|
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
471
402
|
* itself.
|
|
@@ -473,7 +404,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
473
404
|
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
474
405
|
* existing value or node is not found in the doubly linked list.
|
|
475
406
|
*/
|
|
476
|
-
|
|
407
|
+
addAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
|
|
477
408
|
let existingNode;
|
|
478
409
|
|
|
479
410
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
@@ -493,18 +424,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
493
424
|
if (existingNode === this.tail) {
|
|
494
425
|
this._tail = newNode;
|
|
495
426
|
}
|
|
496
|
-
this.
|
|
427
|
+
this._size++;
|
|
497
428
|
return true;
|
|
498
429
|
}
|
|
499
430
|
|
|
500
431
|
return false;
|
|
501
432
|
}
|
|
502
433
|
|
|
503
|
-
/**
|
|
504
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
505
|
-
* Space Complexity: O(1)
|
|
506
|
-
*/
|
|
507
|
-
|
|
508
434
|
/**
|
|
509
435
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
510
436
|
* Space Complexity: O(1)
|
|
@@ -515,25 +441,26 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
515
441
|
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
516
442
|
* bounds.
|
|
517
443
|
*/
|
|
518
|
-
deleteAt(index: number):
|
|
519
|
-
if (index < 0 || index >= this.
|
|
520
|
-
if (index === 0)
|
|
521
|
-
|
|
444
|
+
deleteAt(index: number): boolean {
|
|
445
|
+
if (index < 0 || index >= this.size) return false;
|
|
446
|
+
if (index === 0) {
|
|
447
|
+
this.shift();
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
if (index === this.size - 1) {
|
|
451
|
+
this.pop();
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
522
454
|
|
|
523
455
|
const removedNode = this.getNodeAt(index);
|
|
524
456
|
const prevNode = removedNode!.prev;
|
|
525
457
|
const nextNode = removedNode!.next;
|
|
526
458
|
prevNode!.next = nextNode;
|
|
527
459
|
nextNode!.prev = prevNode;
|
|
528
|
-
this.
|
|
529
|
-
return
|
|
460
|
+
this._size--;
|
|
461
|
+
return true;
|
|
530
462
|
}
|
|
531
463
|
|
|
532
|
-
/**
|
|
533
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
534
|
-
* Space Complexity: O(1)
|
|
535
|
-
*/
|
|
536
|
-
|
|
537
464
|
/**
|
|
538
465
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
539
466
|
* Space Complexity: O(1)
|
|
@@ -563,7 +490,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
563
490
|
const nextNode = node.next;
|
|
564
491
|
prevNode!.next = nextNode;
|
|
565
492
|
nextNode!.prev = prevNode;
|
|
566
|
-
this.
|
|
493
|
+
this._size--;
|
|
567
494
|
}
|
|
568
495
|
return true;
|
|
569
496
|
}
|
|
@@ -571,20 +498,30 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
571
498
|
}
|
|
572
499
|
|
|
573
500
|
/**
|
|
574
|
-
*
|
|
501
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
502
|
+
* Space Complexity: O(1)
|
|
503
|
+
*/
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
575
507
|
* @returns A boolean value is being returned.
|
|
576
508
|
*/
|
|
577
509
|
isEmpty(): boolean {
|
|
578
|
-
return this.
|
|
510
|
+
return this.size === 0;
|
|
579
511
|
}
|
|
580
512
|
|
|
581
513
|
/**
|
|
582
|
-
*
|
|
514
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
515
|
+
* Space Complexity: O(1)
|
|
516
|
+
*/
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
583
520
|
*/
|
|
584
521
|
clear(): void {
|
|
585
522
|
this._head = undefined;
|
|
586
523
|
this._tail = undefined;
|
|
587
|
-
this.
|
|
524
|
+
this._size = 0;
|
|
588
525
|
}
|
|
589
526
|
|
|
590
527
|
/**
|
|
@@ -643,7 +580,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
643
580
|
|
|
644
581
|
/**
|
|
645
582
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
646
|
-
* Space Complexity: O(
|
|
583
|
+
* Space Complexity: O(n)
|
|
647
584
|
*/
|
|
648
585
|
|
|
649
586
|
/**
|
|
@@ -670,7 +607,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
670
607
|
|
|
671
608
|
/**
|
|
672
609
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
673
|
-
* Space Complexity: O(
|
|
610
|
+
* Space Complexity: O(n)
|
|
674
611
|
*/
|
|
675
612
|
|
|
676
613
|
/**
|
|
@@ -679,7 +616,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
679
616
|
*
|
|
680
617
|
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
681
618
|
*/
|
|
682
|
-
reverse():
|
|
619
|
+
reverse(): this {
|
|
683
620
|
let current = this.head;
|
|
684
621
|
[this._head, this._tail] = [this.tail, this.head];
|
|
685
622
|
while (current) {
|
|
@@ -687,10 +624,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
687
624
|
[current.prev, current.next] = [current.next, current.prev];
|
|
688
625
|
current = next;
|
|
689
626
|
}
|
|
627
|
+
return this;
|
|
690
628
|
}
|
|
691
629
|
|
|
692
630
|
/**
|
|
693
|
-
* Time Complexity: O(n)
|
|
631
|
+
* Time Complexity: O(n)
|
|
694
632
|
* Space Complexity: O(n)
|
|
695
633
|
*/
|
|
696
634
|
|
|
@@ -734,8 +672,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
734
672
|
}
|
|
735
673
|
|
|
736
674
|
/**
|
|
737
|
-
* Time Complexity: O(
|
|
738
|
-
* Space Complexity: O(
|
|
675
|
+
* Time Complexity: O(1)
|
|
676
|
+
* Space Complexity: O(1)
|
|
739
677
|
*/
|
|
740
678
|
|
|
741
679
|
/**
|
|
@@ -768,8 +706,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
768
706
|
}
|
|
769
707
|
|
|
770
708
|
/**
|
|
771
|
-
* Time Complexity: O(
|
|
772
|
-
* Space Complexity: O(
|
|
709
|
+
* Time Complexity: O(1)
|
|
710
|
+
* Space Complexity: O(1)
|
|
773
711
|
*/
|
|
774
712
|
|
|
775
713
|
/**
|
|
@@ -800,13 +738,71 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
800
738
|
return mappedList;
|
|
801
739
|
}
|
|
802
740
|
|
|
741
|
+
/**
|
|
742
|
+
* Time Complexity: O(1)
|
|
743
|
+
* Space Complexity: O(1)
|
|
744
|
+
*/
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Time Complexity: O(1)
|
|
748
|
+
* Space Complexity: O(1)
|
|
749
|
+
*
|
|
750
|
+
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
751
|
+
* @param {E} value - The value to be added to the linked list.
|
|
752
|
+
*/
|
|
753
|
+
addLast(value: E): boolean {
|
|
754
|
+
return this.push(value);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Time Complexity: O(1)
|
|
759
|
+
* Space Complexity: O(1)
|
|
760
|
+
*/
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* Time Complexity: O(1)
|
|
764
|
+
* Space Complexity: O(1)
|
|
765
|
+
*
|
|
766
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
767
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
768
|
+
* list is empty, it returns undefined.
|
|
769
|
+
*/
|
|
770
|
+
pollLast(): E | undefined {
|
|
771
|
+
return this.pop();
|
|
772
|
+
}
|
|
773
|
+
|
|
803
774
|
/**
|
|
804
775
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
805
|
-
* Space Complexity: O(
|
|
776
|
+
* Space Complexity: O(1)
|
|
777
|
+
*/
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Time Complexity: O(1)
|
|
781
|
+
* Space Complexity: O(1)
|
|
782
|
+
*
|
|
783
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
784
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
785
|
+
* list.
|
|
786
|
+
*/
|
|
787
|
+
pollFirst(): E | undefined {
|
|
788
|
+
return this.shift();
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
793
|
+
* Space Complexity: O(1)
|
|
806
794
|
*/
|
|
807
795
|
|
|
808
|
-
|
|
809
|
-
|
|
796
|
+
/**
|
|
797
|
+
* Time Complexity: O(1)
|
|
798
|
+
* Space Complexity: O(1)
|
|
799
|
+
*
|
|
800
|
+
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
801
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
802
|
+
* doubly linked list.
|
|
803
|
+
*/
|
|
804
|
+
addFirst(value: E): void {
|
|
805
|
+
this.unshift(value);
|
|
810
806
|
}
|
|
811
807
|
|
|
812
808
|
/**
|