linked-list-typed 1.39.0 → 1.39.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/binary-tree/avl-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/map-graph.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/data-structures/matrix/matrix2d.js +3 -7
- package/dist/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/data-structures/matrix/vector2d.js +0 -1
- package/dist/data-structures/queue/deque.d.ts +20 -20
- package/dist/data-structures/queue/deque.js +22 -22
- package/dist/data-structures/queue/queue.d.ts +3 -3
- package/dist/data-structures/queue/queue.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/helpers.d.ts +1 -4
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +76 -90
- package/src/data-structures/binary-tree/bst.ts +9 -16
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +0 -2
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/helpers.ts +1 -7
|
@@ -124,11 +124,11 @@ class DoublyLinkedList {
|
|
|
124
124
|
return removedNode.val;
|
|
125
125
|
}
|
|
126
126
|
/**
|
|
127
|
-
* The `
|
|
127
|
+
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
128
128
|
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
129
129
|
* list is empty, it returns null.
|
|
130
130
|
*/
|
|
131
|
-
|
|
131
|
+
popLast() {
|
|
132
132
|
return this.pop();
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
@@ -152,11 +152,11 @@ class DoublyLinkedList {
|
|
|
152
152
|
return removedNode.val;
|
|
153
153
|
}
|
|
154
154
|
/**
|
|
155
|
-
* The `
|
|
155
|
+
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
156
156
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
157
157
|
* list.
|
|
158
158
|
*/
|
|
159
|
-
|
|
159
|
+
popFirst() {
|
|
160
160
|
return this.shift();
|
|
161
161
|
}
|
|
162
162
|
/**
|
|
@@ -186,18 +186,18 @@ class DoublyLinkedList {
|
|
|
186
186
|
this.unshift(val);
|
|
187
187
|
}
|
|
188
188
|
/**
|
|
189
|
-
* The `
|
|
190
|
-
* @returns The method `
|
|
189
|
+
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
190
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
191
191
|
*/
|
|
192
|
-
|
|
192
|
+
getFirst() {
|
|
193
193
|
var _a;
|
|
194
194
|
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
|
|
195
195
|
}
|
|
196
196
|
/**
|
|
197
|
-
* The `
|
|
198
|
-
* @returns The method `
|
|
197
|
+
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
198
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
199
199
|
*/
|
|
200
|
-
|
|
200
|
+
getLast() {
|
|
201
201
|
var _a;
|
|
202
202
|
return (_a = this.tail) === null || _a === void 0 ? void 0 : _a.val;
|
|
203
203
|
}
|
|
@@ -241,7 +241,7 @@ class DoublyLinkedList {
|
|
|
241
241
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
242
242
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
243
243
|
*/
|
|
244
|
-
|
|
244
|
+
getNode(val) {
|
|
245
245
|
let current = this.head;
|
|
246
246
|
while (current) {
|
|
247
247
|
if (current.val === val) {
|
|
@@ -281,6 +281,40 @@ class DoublyLinkedList {
|
|
|
281
281
|
this._length++;
|
|
282
282
|
return true;
|
|
283
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
286
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
287
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
288
|
+
* itself.
|
|
289
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
290
|
+
* list.
|
|
291
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
292
|
+
* insertion fails.
|
|
293
|
+
*/
|
|
294
|
+
insertBefore(existingValueOrNode, newValue) {
|
|
295
|
+
let existingNode;
|
|
296
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
297
|
+
existingNode = existingValueOrNode;
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
301
|
+
}
|
|
302
|
+
if (existingNode) {
|
|
303
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
304
|
+
newNode.prev = existingNode.prev;
|
|
305
|
+
if (existingNode.prev) {
|
|
306
|
+
existingNode.prev.next = newNode;
|
|
307
|
+
}
|
|
308
|
+
newNode.next = existingNode;
|
|
309
|
+
existingNode.prev = newNode;
|
|
310
|
+
if (existingNode === this.head) {
|
|
311
|
+
this.head = newNode;
|
|
312
|
+
}
|
|
313
|
+
this._length++;
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
284
318
|
/**
|
|
285
319
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
286
320
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -316,7 +350,7 @@ class DoublyLinkedList {
|
|
|
316
350
|
node = valOrNode;
|
|
317
351
|
}
|
|
318
352
|
else {
|
|
319
|
-
node = this.
|
|
353
|
+
node = this.getNode(valOrNode);
|
|
320
354
|
}
|
|
321
355
|
if (node) {
|
|
322
356
|
if (node === this.head) {
|
|
@@ -401,14 +435,14 @@ class DoublyLinkedList {
|
|
|
401
435
|
return -1;
|
|
402
436
|
}
|
|
403
437
|
/**
|
|
404
|
-
* The `
|
|
438
|
+
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
405
439
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
406
440
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
407
441
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
408
|
-
* @returns The method `
|
|
442
|
+
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
409
443
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
410
444
|
*/
|
|
411
|
-
|
|
445
|
+
findBackward(callback) {
|
|
412
446
|
let current = this.tail;
|
|
413
447
|
while (current) {
|
|
414
448
|
if (callback(current.val)) {
|
|
@@ -419,10 +453,10 @@ class DoublyLinkedList {
|
|
|
419
453
|
return null;
|
|
420
454
|
}
|
|
421
455
|
/**
|
|
422
|
-
* The `
|
|
423
|
-
* @returns The `
|
|
456
|
+
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
457
|
+
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
424
458
|
*/
|
|
425
|
-
|
|
459
|
+
toArrayBackward() {
|
|
426
460
|
const array = [];
|
|
427
461
|
let current = this.tail;
|
|
428
462
|
while (current) {
|
|
@@ -527,7 +561,7 @@ class DoublyLinkedList {
|
|
|
527
561
|
existingNode = existingValueOrNode;
|
|
528
562
|
}
|
|
529
563
|
else {
|
|
530
|
-
existingNode = this.
|
|
564
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
531
565
|
}
|
|
532
566
|
if (existingNode) {
|
|
533
567
|
const newNode = new DoublyLinkedListNode(newValue);
|
|
@@ -546,38 +580,14 @@ class DoublyLinkedList {
|
|
|
546
580
|
return false;
|
|
547
581
|
}
|
|
548
582
|
/**
|
|
549
|
-
* The
|
|
550
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
551
|
-
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
552
|
-
* itself.
|
|
553
|
-
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
554
|
-
* list.
|
|
555
|
-
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
556
|
-
* insertion fails.
|
|
583
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
557
584
|
*/
|
|
558
|
-
|
|
559
|
-
let
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
else {
|
|
564
|
-
existingNode = this.findNode(existingValueOrNode);
|
|
565
|
-
}
|
|
566
|
-
if (existingNode) {
|
|
567
|
-
const newNode = new DoublyLinkedListNode(newValue);
|
|
568
|
-
newNode.prev = existingNode.prev;
|
|
569
|
-
if (existingNode.prev) {
|
|
570
|
-
existingNode.prev.next = newNode;
|
|
571
|
-
}
|
|
572
|
-
newNode.next = existingNode;
|
|
573
|
-
existingNode.prev = newNode;
|
|
574
|
-
if (existingNode === this.head) {
|
|
575
|
-
this.head = newNode;
|
|
576
|
-
}
|
|
577
|
-
this._length++;
|
|
578
|
-
return true;
|
|
585
|
+
*[Symbol.iterator]() {
|
|
586
|
+
let current = this.head;
|
|
587
|
+
while (current) {
|
|
588
|
+
yield current.val;
|
|
589
|
+
current = current.next;
|
|
579
590
|
}
|
|
580
|
-
return false;
|
|
581
591
|
}
|
|
582
592
|
}
|
|
583
593
|
exports.DoublyLinkedList = DoublyLinkedList;
|
|
@@ -39,13 +39,18 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
39
39
|
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
40
40
|
*/
|
|
41
41
|
static fromArray<E>(data: E[]): SinglyLinkedList<E>;
|
|
42
|
-
getLength(): number;
|
|
43
42
|
/**
|
|
44
|
-
* The `push` function adds a new node with the given
|
|
45
|
-
* @param {E}
|
|
43
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
44
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
46
45
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
47
46
|
*/
|
|
48
|
-
push(
|
|
47
|
+
push(val: E): void;
|
|
48
|
+
/**
|
|
49
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
50
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
51
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
52
|
+
*/
|
|
53
|
+
addLast(val: E): void;
|
|
49
54
|
/**
|
|
50
55
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
51
56
|
* pointers accordingly.
|
|
@@ -53,17 +58,35 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
53
58
|
* the linked list is empty, it returns `null`.
|
|
54
59
|
*/
|
|
55
60
|
pop(): E | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
63
|
+
* pointers accordingly.
|
|
64
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
65
|
+
* the linked list is empty, it returns `null`.
|
|
66
|
+
*/
|
|
67
|
+
popLast(): E | undefined;
|
|
56
68
|
/**
|
|
57
69
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
58
70
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
59
71
|
*/
|
|
60
72
|
shift(): E | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
75
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
76
|
+
*/
|
|
77
|
+
popFirst(): E | undefined;
|
|
61
78
|
/**
|
|
62
79
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
63
80
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
64
81
|
* linked list.
|
|
65
82
|
*/
|
|
66
83
|
unshift(val: E): void;
|
|
84
|
+
/**
|
|
85
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
86
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
87
|
+
* linked list.
|
|
88
|
+
*/
|
|
89
|
+
addFirst(val: E): void;
|
|
67
90
|
/**
|
|
68
91
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
69
92
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -148,7 +171,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
148
171
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
149
172
|
* the specified value is found, the function returns `null`.
|
|
150
173
|
*/
|
|
151
|
-
|
|
174
|
+
getNode(value: E): SinglyLinkedListNode<E> | null;
|
|
152
175
|
/**
|
|
153
176
|
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
154
177
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
@@ -158,13 +181,58 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
158
181
|
* inserted before the existing value, and `false` otherwise.
|
|
159
182
|
*/
|
|
160
183
|
insertBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
161
|
-
|
|
162
|
-
|
|
184
|
+
/**
|
|
185
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
186
|
+
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
187
|
+
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
188
|
+
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
189
|
+
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
190
|
+
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
191
|
+
*/
|
|
192
|
+
insertAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
163
193
|
/**
|
|
164
194
|
* The function counts the number of occurrences of a given value in a linked list.
|
|
165
195
|
* @param {E} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
166
196
|
* @returns The count of occurrences of the given value in the linked list.
|
|
167
197
|
*/
|
|
168
198
|
countOccurrences(value: E): number;
|
|
199
|
+
/**
|
|
200
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
201
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
202
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
203
|
+
* current node in the linked list.
|
|
204
|
+
*/
|
|
205
|
+
forEach(callback: (val: E, index: number) => void): void;
|
|
206
|
+
/**
|
|
207
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
208
|
+
* SinglyLinkedList with the transformed values.
|
|
209
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
210
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
211
|
+
* SinglyLinkedList).
|
|
212
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
213
|
+
*/
|
|
214
|
+
map<U>(callback: (val: E) => U): SinglyLinkedList<U>;
|
|
215
|
+
/**
|
|
216
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
217
|
+
* elements that satisfy the given callback function.
|
|
218
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
219
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
220
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
221
|
+
*/
|
|
222
|
+
filter(callback: (val: E) => boolean): SinglyLinkedList<E>;
|
|
223
|
+
/**
|
|
224
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
225
|
+
* single value.
|
|
226
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
227
|
+
* used to perform a specific operation on each element of the linked list.
|
|
228
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
229
|
+
* point for the reduction operation.
|
|
230
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
231
|
+
* elements in the linked list.
|
|
232
|
+
*/
|
|
233
|
+
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
|
|
234
|
+
/**
|
|
235
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
236
|
+
*/
|
|
169
237
|
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
170
238
|
}
|
|
@@ -69,16 +69,13 @@ class SinglyLinkedList {
|
|
|
69
69
|
}
|
|
70
70
|
return singlyLinkedList;
|
|
71
71
|
}
|
|
72
|
-
getLength() {
|
|
73
|
-
return this._length;
|
|
74
|
-
}
|
|
75
72
|
/**
|
|
76
|
-
* The `push` function adds a new node with the given
|
|
77
|
-
* @param {E}
|
|
73
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
74
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
78
75
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
79
76
|
*/
|
|
80
|
-
push(
|
|
81
|
-
const newNode = new SinglyLinkedListNode(
|
|
77
|
+
push(val) {
|
|
78
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
82
79
|
if (!this.head) {
|
|
83
80
|
this.head = newNode;
|
|
84
81
|
this.tail = newNode;
|
|
@@ -89,6 +86,14 @@ class SinglyLinkedList {
|
|
|
89
86
|
}
|
|
90
87
|
this._length++;
|
|
91
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
91
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
92
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
93
|
+
*/
|
|
94
|
+
addLast(val) {
|
|
95
|
+
this.push(val);
|
|
96
|
+
}
|
|
92
97
|
/**
|
|
93
98
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
94
99
|
* pointers accordingly.
|
|
@@ -115,6 +120,15 @@ class SinglyLinkedList {
|
|
|
115
120
|
this._length--;
|
|
116
121
|
return val;
|
|
117
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
125
|
+
* pointers accordingly.
|
|
126
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
127
|
+
* the linked list is empty, it returns `null`.
|
|
128
|
+
*/
|
|
129
|
+
popLast() {
|
|
130
|
+
return this.pop();
|
|
131
|
+
}
|
|
118
132
|
/**
|
|
119
133
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
120
134
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -127,6 +141,13 @@ class SinglyLinkedList {
|
|
|
127
141
|
this._length--;
|
|
128
142
|
return removedNode.val;
|
|
129
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
146
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
147
|
+
*/
|
|
148
|
+
popFirst() {
|
|
149
|
+
return this.shift();
|
|
150
|
+
}
|
|
130
151
|
/**
|
|
131
152
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
132
153
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
@@ -144,6 +165,14 @@ class SinglyLinkedList {
|
|
|
144
165
|
}
|
|
145
166
|
this._length++;
|
|
146
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
170
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
171
|
+
* linked list.
|
|
172
|
+
*/
|
|
173
|
+
addFirst(val) {
|
|
174
|
+
this.unshift(val);
|
|
175
|
+
}
|
|
147
176
|
/**
|
|
148
177
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
149
178
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -350,7 +379,7 @@ class SinglyLinkedList {
|
|
|
350
379
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
351
380
|
* the specified value is found, the function returns `null`.
|
|
352
381
|
*/
|
|
353
|
-
|
|
382
|
+
getNode(value) {
|
|
354
383
|
let current = this.head;
|
|
355
384
|
while (current) {
|
|
356
385
|
if (current.val === value) {
|
|
@@ -409,7 +438,7 @@ class SinglyLinkedList {
|
|
|
409
438
|
existingNode = existingValueOrNode;
|
|
410
439
|
}
|
|
411
440
|
else {
|
|
412
|
-
existingNode = this.
|
|
441
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
413
442
|
}
|
|
414
443
|
if (existingNode) {
|
|
415
444
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
@@ -439,6 +468,78 @@ class SinglyLinkedList {
|
|
|
439
468
|
}
|
|
440
469
|
return count;
|
|
441
470
|
}
|
|
471
|
+
/**
|
|
472
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
473
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
474
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
475
|
+
* current node in the linked list.
|
|
476
|
+
*/
|
|
477
|
+
forEach(callback) {
|
|
478
|
+
let current = this.head;
|
|
479
|
+
let index = 0;
|
|
480
|
+
while (current) {
|
|
481
|
+
callback(current.val, index);
|
|
482
|
+
current = current.next;
|
|
483
|
+
index++;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
488
|
+
* SinglyLinkedList with the transformed values.
|
|
489
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
490
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
491
|
+
* SinglyLinkedList).
|
|
492
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
493
|
+
*/
|
|
494
|
+
map(callback) {
|
|
495
|
+
const mappedList = new SinglyLinkedList();
|
|
496
|
+
let current = this.head;
|
|
497
|
+
while (current) {
|
|
498
|
+
mappedList.push(callback(current.val));
|
|
499
|
+
current = current.next;
|
|
500
|
+
}
|
|
501
|
+
return mappedList;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
505
|
+
* elements that satisfy the given callback function.
|
|
506
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
507
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
508
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
509
|
+
*/
|
|
510
|
+
filter(callback) {
|
|
511
|
+
const filteredList = new SinglyLinkedList();
|
|
512
|
+
let current = this.head;
|
|
513
|
+
while (current) {
|
|
514
|
+
if (callback(current.val)) {
|
|
515
|
+
filteredList.push(current.val);
|
|
516
|
+
}
|
|
517
|
+
current = current.next;
|
|
518
|
+
}
|
|
519
|
+
return filteredList;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
523
|
+
* single value.
|
|
524
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
525
|
+
* used to perform a specific operation on each element of the linked list.
|
|
526
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
527
|
+
* point for the reduction operation.
|
|
528
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
529
|
+
* elements in the linked list.
|
|
530
|
+
*/
|
|
531
|
+
reduce(callback, initialValue) {
|
|
532
|
+
let accumulator = initialValue;
|
|
533
|
+
let current = this.head;
|
|
534
|
+
while (current) {
|
|
535
|
+
accumulator = callback(accumulator, current.val);
|
|
536
|
+
current = current.next;
|
|
537
|
+
}
|
|
538
|
+
return accumulator;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
542
|
+
*/
|
|
442
543
|
*[Symbol.iterator]() {
|
|
443
544
|
let current = this.head;
|
|
444
545
|
while (current) {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import Vector2D from './vector2d';
|
|
8
|
+
import { Vector2D } from './vector2d';
|
|
9
9
|
export declare class Matrix2D {
|
|
10
10
|
private readonly _matrix;
|
|
11
11
|
/**
|
|
@@ -105,4 +105,3 @@ export declare class Matrix2D {
|
|
|
105
105
|
*/
|
|
106
106
|
toVector(): Vector2D;
|
|
107
107
|
}
|
|
108
|
-
export default Matrix2D;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Matrix2D = void 0;
|
|
7
4
|
/**
|
|
@@ -11,7 +8,7 @@ exports.Matrix2D = void 0;
|
|
|
11
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
12
9
|
* @license MIT License
|
|
13
10
|
*/
|
|
14
|
-
const vector2d_1 =
|
|
11
|
+
const vector2d_1 = require("./vector2d");
|
|
15
12
|
class Matrix2D {
|
|
16
13
|
/**
|
|
17
14
|
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
@@ -23,7 +20,7 @@ class Matrix2D {
|
|
|
23
20
|
if (typeof value === 'undefined') {
|
|
24
21
|
this._matrix = Matrix2D.identity;
|
|
25
22
|
}
|
|
26
|
-
else if (value instanceof vector2d_1.
|
|
23
|
+
else if (value instanceof vector2d_1.Vector2D) {
|
|
27
24
|
this._matrix = Matrix2D.identity;
|
|
28
25
|
this._matrix[0][0] = value.x;
|
|
29
26
|
this._matrix[1][0] = value.y;
|
|
@@ -196,8 +193,7 @@ class Matrix2D {
|
|
|
196
193
|
* the first column of the matrix.
|
|
197
194
|
*/
|
|
198
195
|
toVector() {
|
|
199
|
-
return new vector2d_1.
|
|
196
|
+
return new vector2d_1.Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
200
197
|
}
|
|
201
198
|
}
|
|
202
199
|
exports.Matrix2D = Matrix2D;
|
|
203
|
-
exports.default = Matrix2D;
|
|
@@ -37,25 +37,25 @@ export declare class ObjectDeque<E = number> {
|
|
|
37
37
|
*/
|
|
38
38
|
addLast(value: E): void;
|
|
39
39
|
/**
|
|
40
|
-
* The function `
|
|
40
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
41
41
|
* @returns The value of the first element in the data structure.
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
popFirst(): E | undefined;
|
|
44
44
|
/**
|
|
45
|
-
* The `
|
|
45
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
46
46
|
* @returns The element at the first position of the `_nodes` array.
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
getFirst(): E | undefined;
|
|
49
49
|
/**
|
|
50
|
-
* The `
|
|
50
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
51
51
|
* @returns The value that was removed from the data structure.
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
popLast(): E | undefined;
|
|
54
54
|
/**
|
|
55
|
-
* The `
|
|
55
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
56
56
|
* @returns The last element in the array "_nodes" is being returned.
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
getLast(): E | undefined;
|
|
59
59
|
/**
|
|
60
60
|
* The get function returns the element at the specified index in an array-like data structure.
|
|
61
61
|
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
@@ -87,16 +87,16 @@ export declare class ArrayDeque<E> {
|
|
|
87
87
|
*/
|
|
88
88
|
addLast(value: E): number;
|
|
89
89
|
/**
|
|
90
|
-
* The function "
|
|
91
|
-
* @returns The method `
|
|
90
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
91
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
92
92
|
*/
|
|
93
|
-
|
|
93
|
+
popLast(): E | null;
|
|
94
94
|
/**
|
|
95
|
-
* The `
|
|
96
|
-
* @returns The `
|
|
95
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
96
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
97
97
|
* empty.
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
popFirst(): E | null;
|
|
100
100
|
/**
|
|
101
101
|
* O(n) time complexity of adding at the beginning and the end
|
|
102
102
|
*/
|
|
@@ -108,16 +108,16 @@ export declare class ArrayDeque<E> {
|
|
|
108
108
|
*/
|
|
109
109
|
addFirst(value: E): number;
|
|
110
110
|
/**
|
|
111
|
-
* The `
|
|
112
|
-
* @returns The function `
|
|
111
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
112
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
113
113
|
* empty, it will return `null`.
|
|
114
114
|
*/
|
|
115
|
-
|
|
115
|
+
getFirst(): E | null;
|
|
116
116
|
/**
|
|
117
|
-
* The `
|
|
118
|
-
* @returns The method `
|
|
117
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
118
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
119
119
|
*/
|
|
120
|
-
|
|
120
|
+
getLast(): E | null;
|
|
121
121
|
/**
|
|
122
122
|
* O(1) time complexity of obtaining the value
|
|
123
123
|
*/
|