min-heap-typed 1.50.4 → 1.50.5
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 +10 -8
- package/dist/data-structures/base/iterable-base.js +8 -12
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/data-structures/linked-list/doubly-linked-list.js +16 -86
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +27 -69
- package/dist/data-structures/linked-list/singly-linked-list.js +35 -79
- package/dist/data-structures/queue/deque.d.ts +0 -53
- package/dist/data-structures/queue/deque.js +0 -61
- package/dist/data-structures/queue/queue.d.ts +0 -70
- package/dist/data-structures/queue/queue.js +0 -87
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +14 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
- package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
- package/src/data-structures/queue/deque.ts +0 -67
- package/src/data-structures/queue/queue.ts +0 -98
|
@@ -194,14 +194,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
194
194
|
*/
|
|
195
195
|
|
|
196
196
|
/**
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* The push
|
|
201
|
-
* @param {E} value - The value to be added to the linked list.
|
|
197
|
+
* The push function adds a new element to the end of a doubly linked list.
|
|
198
|
+
* @param {E} element - The "element" parameter represents the value that you want to add to the
|
|
199
|
+
* doubly linked list.
|
|
200
|
+
* @returns The `push` method is returning a boolean value, `true`.
|
|
202
201
|
*/
|
|
203
|
-
push(
|
|
204
|
-
const newNode = new DoublyLinkedListNode(
|
|
202
|
+
push(element: E): boolean {
|
|
203
|
+
const newNode = new DoublyLinkedListNode(element);
|
|
205
204
|
if (!this.head) {
|
|
206
205
|
this._head = newNode;
|
|
207
206
|
this._tail = newNode;
|
|
@@ -220,12 +219,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
220
219
|
*/
|
|
221
220
|
|
|
222
221
|
/**
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
227
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
228
|
-
* list is empty, it returns undefined.
|
|
222
|
+
* The `pop()` function removes and returns the value of the last element in a linked list.
|
|
223
|
+
* @returns The method is returning the value of the removed node.
|
|
229
224
|
*/
|
|
230
225
|
pop(): E | undefined {
|
|
231
226
|
if (!this.tail) return undefined;
|
|
@@ -247,12 +242,8 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
247
242
|
*/
|
|
248
243
|
|
|
249
244
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
254
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
255
|
-
* list.
|
|
245
|
+
* The `shift()` function removes and returns the value of the first element in a doubly linked list.
|
|
246
|
+
* @returns The value of the removed node.
|
|
256
247
|
*/
|
|
257
248
|
shift(): E | undefined {
|
|
258
249
|
if (!this.head) return undefined;
|
|
@@ -274,15 +265,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
274
265
|
*/
|
|
275
266
|
|
|
276
267
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* The unshift
|
|
281
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
282
|
-
* doubly linked list.
|
|
268
|
+
* The unshift function adds a new element to the beginning of a doubly linked list.
|
|
269
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
270
|
+
* add to the beginning of the doubly linked list.
|
|
271
|
+
* @returns The `unshift` method is returning a boolean value, `true`.
|
|
283
272
|
*/
|
|
284
|
-
unshift(
|
|
285
|
-
const newNode = new DoublyLinkedListNode(
|
|
273
|
+
unshift(element: E): boolean {
|
|
274
|
+
const newNode = new DoublyLinkedListNode(element);
|
|
286
275
|
if (!this.head) {
|
|
287
276
|
this._head = newNode;
|
|
288
277
|
this._tail = newNode;
|
|
@@ -811,73 +800,6 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
811
800
|
return mappedList;
|
|
812
801
|
}
|
|
813
802
|
|
|
814
|
-
/**
|
|
815
|
-
* Time Complexity: O(1)
|
|
816
|
-
* Space Complexity: O(1)
|
|
817
|
-
*/
|
|
818
|
-
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: O(1)
|
|
821
|
-
* Space Complexity: O(1)
|
|
822
|
-
*
|
|
823
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
824
|
-
* @param {E} value - The value to be added to the linked list.
|
|
825
|
-
*/
|
|
826
|
-
addLast(value: E): boolean {
|
|
827
|
-
return this.push(value);
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* Time Complexity: O(1)
|
|
832
|
-
* Space Complexity: O(1)
|
|
833
|
-
*/
|
|
834
|
-
|
|
835
|
-
/**
|
|
836
|
-
* Time Complexity: O(1)
|
|
837
|
-
* Space Complexity: O(1)
|
|
838
|
-
*
|
|
839
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
840
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
841
|
-
* list is empty, it returns undefined.
|
|
842
|
-
*/
|
|
843
|
-
pollLast(): E | undefined {
|
|
844
|
-
return this.pop();
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
/**
|
|
848
|
-
* Time Complexity: O(1)
|
|
849
|
-
* Space Complexity: O(1)
|
|
850
|
-
*/
|
|
851
|
-
|
|
852
|
-
/**
|
|
853
|
-
* Time Complexity: O(1)
|
|
854
|
-
* Space Complexity: O(1)
|
|
855
|
-
*
|
|
856
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
857
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
858
|
-
* list.
|
|
859
|
-
*/
|
|
860
|
-
pollFirst(): E | undefined {
|
|
861
|
-
return this.shift();
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
/**
|
|
865
|
-
* Time Complexity: O(1)
|
|
866
|
-
* Space Complexity: O(1)
|
|
867
|
-
*/
|
|
868
|
-
|
|
869
|
-
/**
|
|
870
|
-
* Time Complexity: O(1)
|
|
871
|
-
* Space Complexity: O(1)
|
|
872
|
-
*
|
|
873
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
874
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
875
|
-
* doubly linked list.
|
|
876
|
-
*/
|
|
877
|
-
addFirst(value: E): void {
|
|
878
|
-
this.unshift(value);
|
|
879
|
-
}
|
|
880
|
-
|
|
881
803
|
/**
|
|
882
804
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
883
805
|
*/
|
|
@@ -93,6 +93,24 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
93
93
|
return this._tail;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/**
|
|
97
|
+
* The above function returns the value of the first element in a linked list, or undefined if the
|
|
98
|
+
* list is empty.
|
|
99
|
+
* @returns The value of the first node in the linked list, or undefined if the linked list is empty.
|
|
100
|
+
*/
|
|
101
|
+
get first(): E | undefined {
|
|
102
|
+
return this.head?.value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The function returns the value of the last element in a linked list, or undefined if the list is
|
|
107
|
+
* empty.
|
|
108
|
+
* @returns The value of the last node in the linked list, or undefined if the linked list is empty.
|
|
109
|
+
*/
|
|
110
|
+
get last(): E | undefined {
|
|
111
|
+
return this.tail?.value;
|
|
112
|
+
}
|
|
113
|
+
|
|
96
114
|
protected _size: number = 0;
|
|
97
115
|
|
|
98
116
|
/**
|
|
@@ -130,20 +148,19 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
130
148
|
/**
|
|
131
149
|
* Time Complexity: O(1)
|
|
132
150
|
* Space Complexity: O(1)
|
|
133
|
-
* Constant time, as it involves basic pointer adjustments.
|
|
134
|
-
* Constant space, as it only creates a new node.
|
|
135
151
|
*/
|
|
136
152
|
|
|
137
153
|
/**
|
|
138
154
|
* Time Complexity: O(1)
|
|
139
155
|
* Space Complexity: O(1)
|
|
140
156
|
*
|
|
141
|
-
* The
|
|
142
|
-
* @param {E}
|
|
143
|
-
*
|
|
157
|
+
* The push function adds a new element to the end of a singly linked list.
|
|
158
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
159
|
+
* add to the linked list.
|
|
160
|
+
* @returns The `push` method is returning a boolean value, `true`.
|
|
144
161
|
*/
|
|
145
|
-
push(
|
|
146
|
-
const newNode = new SinglyLinkedListNode(
|
|
162
|
+
push(element: E): boolean {
|
|
163
|
+
const newNode = new SinglyLinkedListNode(element);
|
|
147
164
|
if (!this.head) {
|
|
148
165
|
this._head = newNode;
|
|
149
166
|
this._tail = newNode;
|
|
@@ -155,23 +172,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
155
172
|
return true;
|
|
156
173
|
}
|
|
157
174
|
|
|
158
|
-
/**
|
|
159
|
-
* Time Complexity: O(1)
|
|
160
|
-
* Space Complexity: O(1)
|
|
161
|
-
*/
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Time Complexity: O(1)
|
|
165
|
-
* Space Complexity: O(1)
|
|
166
|
-
*
|
|
167
|
-
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
168
|
-
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
169
|
-
* any type (E) as specified in the generic type declaration of the class or function.
|
|
170
|
-
*/
|
|
171
|
-
addLast(value: E): boolean {
|
|
172
|
-
return this.push(value);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
175
|
/**
|
|
176
176
|
* Time Complexity: O(n)
|
|
177
177
|
* Space Complexity: O(1)
|
|
@@ -182,10 +182,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
182
182
|
* Time Complexity: O(n)
|
|
183
183
|
* Space Complexity: O(1)
|
|
184
184
|
*
|
|
185
|
-
* The `pop
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
* the linked list is empty, it returns `undefined`.
|
|
185
|
+
* The `pop` function removes and returns the value of the last element in a linked list.
|
|
186
|
+
* @returns The method is returning the value of the element that is being popped from the end of the
|
|
187
|
+
* list.
|
|
189
188
|
*/
|
|
190
189
|
pop(): E | undefined {
|
|
191
190
|
if (!this.head) return undefined;
|
|
@@ -208,24 +207,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
208
207
|
return value;
|
|
209
208
|
}
|
|
210
209
|
|
|
211
|
-
/**
|
|
212
|
-
* Time Complexity: O(n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
214
|
-
*/
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Time Complexity: O(n)
|
|
218
|
-
* Space Complexity: O(1)
|
|
219
|
-
*
|
|
220
|
-
* The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
221
|
-
* pointers accordingly.
|
|
222
|
-
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
223
|
-
* the linked list is empty, it returns `undefined`.
|
|
224
|
-
*/
|
|
225
|
-
pollLast(): E | undefined {
|
|
226
|
-
return this.pop();
|
|
227
|
-
}
|
|
228
|
-
|
|
229
210
|
/**
|
|
230
211
|
* Time Complexity: O(1)
|
|
231
212
|
* Space Complexity: O(1)
|
|
@@ -235,8 +216,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
235
216
|
* Time Complexity: O(1)
|
|
236
217
|
* Space Complexity: O(1)
|
|
237
218
|
*
|
|
238
|
-
* The `shift()` function removes and returns the value of the first
|
|
239
|
-
* @returns The value of the
|
|
219
|
+
* The `shift()` function removes and returns the value of the first element in a linked list.
|
|
220
|
+
* @returns The value of the removed node.
|
|
240
221
|
*/
|
|
241
222
|
shift(): E | undefined {
|
|
242
223
|
if (!this.head) return undefined;
|
|
@@ -255,28 +236,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
255
236
|
* Time Complexity: O(1)
|
|
256
237
|
* Space Complexity: O(1)
|
|
257
238
|
*
|
|
258
|
-
* The
|
|
259
|
-
* @
|
|
239
|
+
* The unshift function adds a new element to the beginning of a singly linked list.
|
|
240
|
+
* @param {E} element - The "element" parameter represents the value of the element that you want to
|
|
241
|
+
* add to the beginning of the singly linked list.
|
|
242
|
+
* @returns The `unshift` method is returning a boolean value, `true`.
|
|
260
243
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Time Complexity: O(1)
|
|
267
|
-
* Space Complexity: O(1)
|
|
268
|
-
*/
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Time Complexity: O(1)
|
|
272
|
-
* Space Complexity: O(1)
|
|
273
|
-
*
|
|
274
|
-
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
275
|
-
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
276
|
-
* linked list.
|
|
277
|
-
*/
|
|
278
|
-
unshift(value: E): boolean {
|
|
279
|
-
const newNode = new SinglyLinkedListNode(value);
|
|
244
|
+
unshift(element: E): boolean {
|
|
245
|
+
const newNode = new SinglyLinkedListNode(element);
|
|
280
246
|
if (!this.head) {
|
|
281
247
|
this._head = newNode;
|
|
282
248
|
this._tail = newNode;
|
|
@@ -288,27 +254,9 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
288
254
|
return true;
|
|
289
255
|
}
|
|
290
256
|
|
|
291
|
-
/**
|
|
292
|
-
* Time Complexity: O(1)
|
|
293
|
-
* Space Complexity: O(1)
|
|
294
|
-
*/
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Time Complexity: O(1)
|
|
298
|
-
* Space Complexity: O(1)
|
|
299
|
-
*
|
|
300
|
-
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
301
|
-
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
302
|
-
* linked list.
|
|
303
|
-
*/
|
|
304
|
-
addFirst(value: E): boolean {
|
|
305
|
-
return this.unshift(value);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
257
|
/**
|
|
309
258
|
* Time Complexity: O(n)
|
|
310
259
|
* Space Complexity: O(1)
|
|
311
|
-
* Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
312
260
|
*/
|
|
313
261
|
|
|
314
262
|
/**
|
|
@@ -811,73 +811,6 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
811
811
|
return newDeque;
|
|
812
812
|
}
|
|
813
813
|
|
|
814
|
-
/**
|
|
815
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
816
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
817
|
-
*/
|
|
818
|
-
|
|
819
|
-
/**
|
|
820
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
821
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
822
|
-
*
|
|
823
|
-
* The addLast function adds an element to the end of an array.
|
|
824
|
-
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
825
|
-
* data structure.
|
|
826
|
-
*/
|
|
827
|
-
addLast(element: E): boolean {
|
|
828
|
-
return this.push(element);
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
/**
|
|
832
|
-
* Time Complexity: O(1)
|
|
833
|
-
* Space Complexity: O(1)
|
|
834
|
-
*/
|
|
835
|
-
|
|
836
|
-
/**
|
|
837
|
-
* Time Complexity: O(1)
|
|
838
|
-
* Space Complexity: O(1)
|
|
839
|
-
*
|
|
840
|
-
* The function "pollLast" removes and returns the last element of an array.
|
|
841
|
-
* @returns The last element of the array is being returned.
|
|
842
|
-
*/
|
|
843
|
-
pollLast(): E | undefined {
|
|
844
|
-
return this.pop();
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
/**
|
|
848
|
-
* Time Complexity: O(1)
|
|
849
|
-
* Space Complexity: O(1)
|
|
850
|
-
* /
|
|
851
|
-
|
|
852
|
-
/**
|
|
853
|
-
* Time Complexity: O(1)
|
|
854
|
-
* Space Complexity: O(1)
|
|
855
|
-
*
|
|
856
|
-
* The "addFirst" function adds an element to the beginning of an array.
|
|
857
|
-
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
858
|
-
* beginning of the data structure.
|
|
859
|
-
*/
|
|
860
|
-
addFirst(element: E): boolean {
|
|
861
|
-
return this.unshift(element);
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
/**
|
|
865
|
-
* Time Complexity: O(1)
|
|
866
|
-
* Space Complexity: O(1)
|
|
867
|
-
* /
|
|
868
|
-
|
|
869
|
-
/**
|
|
870
|
-
* Time Complexity: O(1)
|
|
871
|
-
* Space Complexity: O(1)
|
|
872
|
-
*
|
|
873
|
-
* The function "pollFirst" removes and returns the first element of an array.
|
|
874
|
-
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
875
|
-
* from the beginning. If the array is empty, it will return `undefined`.
|
|
876
|
-
*/
|
|
877
|
-
pollFirst(): E | undefined {
|
|
878
|
-
return this.shift();
|
|
879
|
-
}
|
|
880
|
-
|
|
881
814
|
/**
|
|
882
815
|
* Time Complexity: O(n)
|
|
883
816
|
* Space Complexity: O(1)
|
|
@@ -178,72 +178,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
178
178
|
return spliced.length === 1;
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
/**
|
|
182
|
-
* Time Complexity: O(1)
|
|
183
|
-
* Space Complexity: O(1)
|
|
184
|
-
*/
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Time Complexity: O(1)
|
|
188
|
-
* Space Complexity: O(1)
|
|
189
|
-
*
|
|
190
|
-
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
191
|
-
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
192
|
-
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
193
|
-
*/
|
|
194
|
-
peek(): E | undefined {
|
|
195
|
-
return this.first;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Time Complexity: O(1)
|
|
200
|
-
* Space Complexity: O(1)
|
|
201
|
-
*/
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Time Complexity: O(1)
|
|
205
|
-
* Space Complexity: O(1)
|
|
206
|
-
*
|
|
207
|
-
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
208
|
-
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
209
|
-
* array is empty, it returns `undefined`.
|
|
210
|
-
*/
|
|
211
|
-
peekLast(): E | undefined {
|
|
212
|
-
return this.last;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Time Complexity: O(1)
|
|
217
|
-
* Space Complexity: O(1)
|
|
218
|
-
*/
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Time Complexity: O(1)
|
|
222
|
-
* Space Complexity: O(1)
|
|
223
|
-
*
|
|
224
|
-
* The enqueue function adds a value to the end of a queue.
|
|
225
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
226
|
-
*/
|
|
227
|
-
enqueue(value: E): boolean {
|
|
228
|
-
return this.push(value);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Time Complexity: O(1)
|
|
233
|
-
* Space Complexity: O(1)
|
|
234
|
-
*/
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Time Complexity: O(1)
|
|
238
|
-
* Space Complexity: O(1)
|
|
239
|
-
*
|
|
240
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
241
|
-
* @returns The method is returning a value of type E or undefined.
|
|
242
|
-
*/
|
|
243
|
-
dequeue(): E | undefined {
|
|
244
|
-
return this.shift();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
181
|
/**
|
|
248
182
|
* Time Complexity: O(1)
|
|
249
183
|
* Space Complexity: O(1)
|
|
@@ -411,38 +345,6 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
411
345
|
* 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.
|
|
412
346
|
*/
|
|
413
347
|
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
414
|
-
/**
|
|
415
|
-
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
416
|
-
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
417
|
-
*/
|
|
418
|
-
get first(): E | undefined {
|
|
419
|
-
return this.head?.value;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* The enqueue function adds a value to the end of an array.
|
|
424
|
-
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
425
|
-
*/
|
|
426
|
-
enqueue(value: E): boolean {
|
|
427
|
-
return this.push(value);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
432
|
-
* @returns The method is returning the element at the front of the queue, or undefined if the queue is empty.
|
|
433
|
-
*/
|
|
434
|
-
dequeue(): E | undefined {
|
|
435
|
-
return this.shift();
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
440
|
-
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
441
|
-
*/
|
|
442
|
-
peek(): E | undefined {
|
|
443
|
-
return this.first;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
348
|
/**
|
|
447
349
|
* Time Complexity: O(n)
|
|
448
350
|
* Space Complexity: O(n)
|