priority-queue-typed 1.49.2 → 1.49.4
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/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/data-structures/graph/abstract-graph.d.ts +8 -17
- package/dist/data-structures/graph/abstract-graph.js +43 -29
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +6 -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/linked-list/doubly-linked-list.d.ts +43 -43
- package/dist/data-structures/linked-list/doubly-linked-list.js +49 -49
- 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/queue.d.ts +33 -33
- package/dist/data-structures/queue/queue.js +40 -40
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/graph/abstract-graph.ts +56 -27
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +53 -53
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/queue.ts +45 -45
|
@@ -70,6 +70,38 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
70
70
|
* Space Complexity: O(n)
|
|
71
71
|
*/
|
|
72
72
|
|
|
73
|
+
/**
|
|
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
|
+
|
|
73
105
|
/**
|
|
74
106
|
* Time Complexity: O(n), where n is the size of the input array.
|
|
75
107
|
* Space Complexity: O(n)
|
|
@@ -141,7 +173,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
141
173
|
}
|
|
142
174
|
|
|
143
175
|
/**
|
|
144
|
-
* Time Complexity: O(
|
|
176
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
145
177
|
* Space Complexity: O(1)
|
|
146
178
|
*/
|
|
147
179
|
|
|
@@ -168,7 +200,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
168
200
|
}
|
|
169
201
|
|
|
170
202
|
/**
|
|
171
|
-
* Time Complexity: O(
|
|
203
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
172
204
|
* Space Complexity: O(1)
|
|
173
205
|
*/
|
|
174
206
|
|
|
@@ -399,11 +431,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
399
431
|
return false;
|
|
400
432
|
}
|
|
401
433
|
|
|
402
|
-
/**
|
|
403
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
404
|
-
* Space Complexity: O(1)
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
434
|
/**
|
|
408
435
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
409
436
|
* Space Complexity: O(1)
|
|
@@ -434,11 +461,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
434
461
|
return true;
|
|
435
462
|
}
|
|
436
463
|
|
|
437
|
-
/**
|
|
438
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
439
|
-
* Space Complexity: O(1)
|
|
440
|
-
*/
|
|
441
|
-
|
|
442
464
|
/**
|
|
443
465
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
444
466
|
* Space Complexity: O(1)
|
|
@@ -475,6 +497,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
475
497
|
return false;
|
|
476
498
|
}
|
|
477
499
|
|
|
500
|
+
/**
|
|
501
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
502
|
+
* Space Complexity: O(1)
|
|
503
|
+
*/
|
|
504
|
+
|
|
478
505
|
/**
|
|
479
506
|
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
480
507
|
* @returns A boolean value is being returned.
|
|
@@ -483,6 +510,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
483
510
|
return this.size === 0;
|
|
484
511
|
}
|
|
485
512
|
|
|
513
|
+
/**
|
|
514
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
515
|
+
* Space Complexity: O(1)
|
|
516
|
+
*/
|
|
517
|
+
|
|
486
518
|
/**
|
|
487
519
|
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
488
520
|
*/
|
|
@@ -548,7 +580,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
548
580
|
|
|
549
581
|
/**
|
|
550
582
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
551
|
-
* Space Complexity: O(
|
|
583
|
+
* Space Complexity: O(n)
|
|
552
584
|
*/
|
|
553
585
|
|
|
554
586
|
/**
|
|
@@ -575,7 +607,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
575
607
|
|
|
576
608
|
/**
|
|
577
609
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
578
|
-
* Space Complexity: O(
|
|
610
|
+
* Space Complexity: O(n)
|
|
579
611
|
*/
|
|
580
612
|
|
|
581
613
|
/**
|
|
@@ -596,7 +628,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
596
628
|
}
|
|
597
629
|
|
|
598
630
|
/**
|
|
599
|
-
* Time Complexity: O(n)
|
|
631
|
+
* Time Complexity: O(n)
|
|
600
632
|
* Space Complexity: O(n)
|
|
601
633
|
*/
|
|
602
634
|
|
|
@@ -640,8 +672,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
640
672
|
}
|
|
641
673
|
|
|
642
674
|
/**
|
|
643
|
-
* Time Complexity: O(
|
|
644
|
-
* Space Complexity: O(
|
|
675
|
+
* Time Complexity: O(1)
|
|
676
|
+
* Space Complexity: O(1)
|
|
645
677
|
*/
|
|
646
678
|
|
|
647
679
|
/**
|
|
@@ -674,8 +706,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
674
706
|
}
|
|
675
707
|
|
|
676
708
|
/**
|
|
677
|
-
* Time Complexity: O(
|
|
678
|
-
* Space Complexity: O(
|
|
709
|
+
* Time Complexity: O(1)
|
|
710
|
+
* Space Complexity: O(1)
|
|
679
711
|
*/
|
|
680
712
|
|
|
681
713
|
/**
|
|
@@ -740,7 +772,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
740
772
|
}
|
|
741
773
|
|
|
742
774
|
/**
|
|
743
|
-
* Time Complexity: O(
|
|
775
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
744
776
|
* Space Complexity: O(1)
|
|
745
777
|
*/
|
|
746
778
|
|
|
@@ -757,7 +789,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
757
789
|
}
|
|
758
790
|
|
|
759
791
|
/**
|
|
760
|
-
* Time Complexity: O(
|
|
792
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
761
793
|
* Space Complexity: O(1)
|
|
762
794
|
*/
|
|
763
795
|
|
|
@@ -773,38 +805,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
773
805
|
this.unshift(value);
|
|
774
806
|
}
|
|
775
807
|
|
|
776
|
-
/**
|
|
777
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
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.
|
|
787
|
-
*/
|
|
788
|
-
get first(): E | undefined {
|
|
789
|
-
return this.head?.value;
|
|
790
|
-
}
|
|
791
|
-
|
|
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;
|
|
806
|
-
}
|
|
807
|
-
|
|
808
808
|
/**
|
|
809
809
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
810
810
|
*/
|
|
@@ -33,8 +33,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
33
33
|
this._tail = undefined;
|
|
34
34
|
this._size = 0;
|
|
35
35
|
if (elements) {
|
|
36
|
-
for (const el of elements)
|
|
37
|
-
this.push(el);
|
|
36
|
+
for (const el of elements) this.push(el);
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
|
|
@@ -348,7 +347,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
348
347
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
349
348
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
350
349
|
*/
|
|
351
|
-
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined
|
|
350
|
+
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined): boolean {
|
|
352
351
|
if (!valueOrNode) return false;
|
|
353
352
|
let value: E;
|
|
354
353
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
@@ -57,6 +57,45 @@ export class SkipList<K, V> {
|
|
|
57
57
|
return this._probability;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
62
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
67
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
68
|
+
*
|
|
69
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
70
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
71
|
+
*/
|
|
72
|
+
get first(): V | undefined {
|
|
73
|
+
const firstNode = this.head.forward[0];
|
|
74
|
+
return firstNode ? firstNode.value : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
79
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
84
|
+
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
85
|
+
*
|
|
86
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
87
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
88
|
+
*/
|
|
89
|
+
get last(): V | undefined {
|
|
90
|
+
let current = this.head;
|
|
91
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
92
|
+
while (current.forward[i]) {
|
|
93
|
+
current = current.forward[i];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return current.value;
|
|
97
|
+
}
|
|
98
|
+
|
|
60
99
|
/**
|
|
61
100
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
62
101
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -125,7 +164,7 @@ export class SkipList<K, V> {
|
|
|
125
164
|
}
|
|
126
165
|
|
|
127
166
|
/**
|
|
128
|
-
* Time Complexity: O(
|
|
167
|
+
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
129
168
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
130
169
|
*/
|
|
131
170
|
|
|
@@ -181,45 +220,6 @@ export class SkipList<K, V> {
|
|
|
181
220
|
return false;
|
|
182
221
|
}
|
|
183
222
|
|
|
184
|
-
/**
|
|
185
|
-
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
186
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
187
|
-
*/
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
191
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
192
|
-
*
|
|
193
|
-
* Get the value of the first element (the smallest element) in the Skip List.
|
|
194
|
-
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
195
|
-
*/
|
|
196
|
-
get first(): V | undefined {
|
|
197
|
-
const firstNode = this.head.forward[0];
|
|
198
|
-
return firstNode ? firstNode.value : undefined;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
203
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
204
|
-
*/
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
208
|
-
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
209
|
-
*
|
|
210
|
-
* Get the value of the last element (the largest element) in the Skip List.
|
|
211
|
-
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
212
|
-
*/
|
|
213
|
-
get last(): V | undefined {
|
|
214
|
-
let current = this.head;
|
|
215
|
-
for (let i = this.level - 1; i >= 0; i--) {
|
|
216
|
-
while (current.forward[i]) {
|
|
217
|
-
current = current.forward[i];
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return current.value;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
223
|
/**
|
|
224
224
|
* Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
|
|
225
225
|
* Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
|
|
@@ -49,6 +49,40 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
49
49
|
return this.nodes.length - this.offset;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
54
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
55
|
+
*
|
|
56
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
57
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
58
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
59
|
+
*/
|
|
60
|
+
get first(): E | undefined {
|
|
61
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Time Complexity: O(1) - constant time as it adds an element to the end of the array.
|
|
66
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
71
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
72
|
+
*
|
|
73
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
74
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
75
|
+
* array is empty, it returns `undefined`.
|
|
76
|
+
*/
|
|
77
|
+
get last(): E | undefined {
|
|
78
|
+
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
83
|
+
* Space Complexity: O(1) - no additional space is used.
|
|
84
|
+
*/
|
|
85
|
+
|
|
52
86
|
/**
|
|
53
87
|
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
54
88
|
* @public
|
|
@@ -62,7 +96,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
62
96
|
}
|
|
63
97
|
|
|
64
98
|
/**
|
|
65
|
-
* Time Complexity: O(1) - constant time as it
|
|
99
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
66
100
|
* Space Complexity: O(1) - no additional space is used.
|
|
67
101
|
*/
|
|
68
102
|
|
|
@@ -80,7 +114,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
80
114
|
}
|
|
81
115
|
|
|
82
116
|
/**
|
|
83
|
-
* Time Complexity: O(
|
|
117
|
+
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
84
118
|
* Space Complexity: O(1) - no additional space is used.
|
|
85
119
|
*/
|
|
86
120
|
|
|
@@ -107,23 +141,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
107
141
|
return first;
|
|
108
142
|
}
|
|
109
143
|
|
|
110
|
-
/**
|
|
111
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
112
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
117
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
118
|
-
*
|
|
119
|
-
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
120
|
-
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
121
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
122
|
-
*/
|
|
123
|
-
get first(): E | undefined {
|
|
124
|
-
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
144
|
/**
|
|
128
145
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
129
146
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -141,23 +158,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
141
158
|
return this.first;
|
|
142
159
|
}
|
|
143
160
|
|
|
144
|
-
/**
|
|
145
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
146
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
147
|
-
*/
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
151
|
-
* Space Complexity: O(1) - no additional space is used.
|
|
152
|
-
*
|
|
153
|
-
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
154
|
-
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
155
|
-
* array is empty, it returns `undefined`.
|
|
156
|
-
*/
|
|
157
|
-
get last(): E | undefined {
|
|
158
|
-
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
161
|
/**
|
|
162
162
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
163
163
|
* Space Complexity: O(1) - no additional space is used.
|
|
@@ -358,12 +358,20 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
358
358
|
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
359
359
|
*/
|
|
360
360
|
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
361
|
+
/**
|
|
362
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
363
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
364
|
+
*/
|
|
365
|
+
get first(): E | undefined {
|
|
366
|
+
return this.head?.value;
|
|
367
|
+
}
|
|
368
|
+
|
|
361
369
|
/**
|
|
362
370
|
* The enqueue function adds a value to the end of an array.
|
|
363
371
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
364
372
|
*/
|
|
365
373
|
enqueue(value: E): boolean {
|
|
366
|
-
return
|
|
374
|
+
return this.push(value);
|
|
367
375
|
}
|
|
368
376
|
|
|
369
377
|
/**
|
|
@@ -374,14 +382,6 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
374
382
|
return this.shift();
|
|
375
383
|
}
|
|
376
384
|
|
|
377
|
-
/**
|
|
378
|
-
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
379
|
-
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
380
|
-
*/
|
|
381
|
-
get first(): E | undefined {
|
|
382
|
-
return this.head?.value;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
385
|
/**
|
|
386
386
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
387
387
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|