graph-typed 1.54.3 → 2.0.1
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-element-base.d.ts +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +16 -16
- package/dist/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/data-structures/hash/hash-map.js +46 -0
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +131 -40
- package/dist/data-structures/queue/queue.js +181 -50
- package/dist/data-structures/stack/stack.d.ts +124 -11
- package/dist/data-structures/stack/stack.js +121 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +196 -63
- package/src/data-structures/stack/stack.ts +124 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
|
@@ -1,73 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
4
|
-
const
|
|
5
|
-
class DoublyLinkedListNode {
|
|
4
|
+
const linear_base_1 = require("../base/linear-base");
|
|
5
|
+
class DoublyLinkedListNode extends linear_base_1.LinkedListNode {
|
|
6
6
|
/**
|
|
7
7
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
8
8
|
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
9
9
|
* is defined as a generic type "E".
|
|
10
10
|
*/
|
|
11
11
|
constructor(value) {
|
|
12
|
+
super(value);
|
|
12
13
|
this._value = value;
|
|
13
14
|
this._next = undefined;
|
|
14
15
|
this._prev = undefined;
|
|
15
16
|
}
|
|
16
|
-
/**
|
|
17
|
-
* The function returns the value of a protected variable.
|
|
18
|
-
* @returns The value of the variable `_value` is being returned.
|
|
19
|
-
*/
|
|
20
|
-
get value() {
|
|
21
|
-
return this._value;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* The above function sets the value of a variable.
|
|
25
|
-
* @param {E} value - The parameter "value" is of type E, which means it can be any type.
|
|
26
|
-
*/
|
|
27
|
-
set value(value) {
|
|
28
|
-
this._value = value;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* The "next" function returns the next node in a doubly linked list.
|
|
32
|
-
* @returns The `next` property is being returned. It can be either a `DoublyLinkedListNode<E>`
|
|
33
|
-
* object or `undefined`.
|
|
34
|
-
*/
|
|
35
17
|
get next() {
|
|
36
18
|
return this._next;
|
|
37
19
|
}
|
|
38
|
-
/**
|
|
39
|
-
* The "next" property of a DoublyLinkedListNode is set to the provided value.
|
|
40
|
-
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
41
|
-
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
42
|
-
* `DoublyLinkedListNode` object or `undefined` as its value.
|
|
43
|
-
*/
|
|
44
20
|
set next(value) {
|
|
45
21
|
this._next = value;
|
|
46
22
|
}
|
|
47
|
-
/**
|
|
48
|
-
* The `prev` function returns the previous node in a doubly linked list.
|
|
49
|
-
* @returns The `prev` property of the `DoublyLinkedListNode` class is being returned. It can either
|
|
50
|
-
* be a `DoublyLinkedListNode` object or `undefined`.
|
|
51
|
-
*/
|
|
52
23
|
get prev() {
|
|
53
24
|
return this._prev;
|
|
54
25
|
}
|
|
55
|
-
/**
|
|
56
|
-
* The function sets the previous node of a doubly linked list node.
|
|
57
|
-
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
58
|
-
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
59
|
-
* `DoublyLinkedListNode` object or `undefined` as its value.
|
|
60
|
-
*/
|
|
61
26
|
set prev(value) {
|
|
62
27
|
this._prev = value;
|
|
63
28
|
}
|
|
64
29
|
}
|
|
65
30
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
66
31
|
/**
|
|
67
|
-
*1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
32
|
+
* 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
68
33
|
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
|
69
34
|
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
70
35
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
36
|
+
* Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
|
|
71
37
|
* @example
|
|
72
38
|
* // text editor operation history
|
|
73
39
|
* const actions = [
|
|
@@ -141,7 +107,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
141
107
|
* const initialNode = this.currentSong;
|
|
142
108
|
*
|
|
143
109
|
* // Loop through the playlist twice
|
|
144
|
-
* for (let i = 0; i < this.playlist.
|
|
110
|
+
* for (let i = 0; i < this.playlist.length * 2; i++) {
|
|
145
111
|
* playedSongs.push(this.currentSong!.value);
|
|
146
112
|
* this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed
|
|
147
113
|
* }
|
|
@@ -262,7 +228,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
262
228
|
* }
|
|
263
229
|
*
|
|
264
230
|
* // Check capacity
|
|
265
|
-
* if (this.list.
|
|
231
|
+
* if (this.list.length >= this.capacity) {
|
|
266
232
|
* // Delete the least recently used element (the tail of the linked list)
|
|
267
233
|
* const removedNode = this.list.tail;
|
|
268
234
|
* if (removedNode) {
|
|
@@ -307,9 +273,9 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
307
273
|
* this.map.clear();
|
|
308
274
|
* }
|
|
309
275
|
*
|
|
310
|
-
* // Get the current cache
|
|
311
|
-
* get
|
|
312
|
-
* return this.list.
|
|
276
|
+
* // Get the current cache length
|
|
277
|
+
* get length(): number {
|
|
278
|
+
* return this.list.length;
|
|
313
279
|
* }
|
|
314
280
|
*
|
|
315
281
|
* // Check if it is empty
|
|
@@ -368,7 +334,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
368
334
|
*
|
|
369
335
|
* console.log(cache.delete('a')); // true
|
|
370
336
|
* console.log(cache.get('a')); // undefined
|
|
371
|
-
* console.log(cache.
|
|
337
|
+
* console.log(cache.length); // 1
|
|
372
338
|
*
|
|
373
339
|
* // Should support clearing cache
|
|
374
340
|
* cache.clear();
|
|
@@ -376,7 +342,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
376
342
|
* cache.set('b', 2);
|
|
377
343
|
* cache.clear();
|
|
378
344
|
*
|
|
379
|
-
* console.log(cache.
|
|
345
|
+
* console.log(cache.length); // 0
|
|
380
346
|
* console.log(cache.isEmpty); // true
|
|
381
347
|
* @example
|
|
382
348
|
* // finding lyrics by timestamp in Coldplay's "Fix You"
|
|
@@ -495,7 +461,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
495
461
|
* scheduler.clear();
|
|
496
462
|
* console.log(scheduler.listProcesses()); // []
|
|
497
463
|
*/
|
|
498
|
-
class DoublyLinkedList extends
|
|
464
|
+
class DoublyLinkedList extends linear_base_1.LinearLinkedBase {
|
|
499
465
|
/**
|
|
500
466
|
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
501
467
|
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
@@ -510,30 +476,22 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
510
476
|
super(options);
|
|
511
477
|
this._head = undefined;
|
|
512
478
|
this._tail = undefined;
|
|
513
|
-
this.
|
|
479
|
+
this._length = 0;
|
|
480
|
+
if (options) {
|
|
481
|
+
const { maxLen } = options;
|
|
482
|
+
if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0)
|
|
483
|
+
this._maxLen = maxLen;
|
|
484
|
+
}
|
|
514
485
|
this.pushMany(elements);
|
|
515
486
|
}
|
|
516
|
-
/**
|
|
517
|
-
* The `head` function returns the first node of a doubly linked list.
|
|
518
|
-
* @returns The method `getHead()` returns either a `DoublyLinkedListNode<E>` object or `undefined`.
|
|
519
|
-
*/
|
|
520
487
|
get head() {
|
|
521
488
|
return this._head;
|
|
522
489
|
}
|
|
523
|
-
/**
|
|
524
|
-
* The `tail` function returns the last node of a doubly linked list.
|
|
525
|
-
* @returns The `get tail()` method is returning either a `DoublyLinkedListNode<E>` object or
|
|
526
|
-
* `undefined`.
|
|
527
|
-
*/
|
|
528
490
|
get tail() {
|
|
529
491
|
return this._tail;
|
|
530
492
|
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
* @returns The size of the object, which is a number.
|
|
534
|
-
*/
|
|
535
|
-
get size() {
|
|
536
|
-
return this._size;
|
|
493
|
+
get length() {
|
|
494
|
+
return this._length;
|
|
537
495
|
}
|
|
538
496
|
/**
|
|
539
497
|
* Time Complexity: O(1)
|
|
@@ -606,7 +564,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
606
564
|
this.tail.next = newNode;
|
|
607
565
|
this._tail = newNode;
|
|
608
566
|
}
|
|
609
|
-
this.
|
|
567
|
+
this._length++;
|
|
568
|
+
if (this._maxLen > 0 && this.length > this._maxLen)
|
|
569
|
+
this.shift();
|
|
610
570
|
return true;
|
|
611
571
|
}
|
|
612
572
|
/**
|
|
@@ -628,7 +588,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
628
588
|
this._tail = removedNode.prev;
|
|
629
589
|
this.tail.next = undefined;
|
|
630
590
|
}
|
|
631
|
-
this.
|
|
591
|
+
this._length--;
|
|
632
592
|
return removedNode.value;
|
|
633
593
|
}
|
|
634
594
|
/**
|
|
@@ -650,7 +610,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
650
610
|
this._head = removedNode.next;
|
|
651
611
|
this.head.prev = undefined;
|
|
652
612
|
}
|
|
653
|
-
this.
|
|
613
|
+
this._length--;
|
|
654
614
|
return removedNode.value;
|
|
655
615
|
}
|
|
656
616
|
/**
|
|
@@ -674,7 +634,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
674
634
|
this.head.prev = newNode;
|
|
675
635
|
this._head = newNode;
|
|
676
636
|
}
|
|
677
|
-
this.
|
|
637
|
+
this._length++;
|
|
638
|
+
if (this._maxLen > 0 && this._length > this._maxLen)
|
|
639
|
+
this.pop();
|
|
678
640
|
return true;
|
|
679
641
|
}
|
|
680
642
|
/**
|
|
@@ -737,7 +699,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
737
699
|
* or the linked list is empty, it will return undefined.
|
|
738
700
|
*/
|
|
739
701
|
at(index) {
|
|
740
|
-
if (index < 0 || index >= this.
|
|
702
|
+
if (index < 0 || index >= this._length)
|
|
741
703
|
return undefined;
|
|
742
704
|
let current = this.head;
|
|
743
705
|
for (let i = 0; i < index; i++) {
|
|
@@ -757,7 +719,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
757
719
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
758
720
|
*/
|
|
759
721
|
getNodeAt(index) {
|
|
760
|
-
if (index < 0 || index >= this.
|
|
722
|
+
if (index < 0 || index >= this._length)
|
|
761
723
|
return undefined;
|
|
762
724
|
let current = this.head;
|
|
763
725
|
for (let i = 0; i < index; i++) {
|
|
@@ -786,6 +748,8 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
786
748
|
getNode(elementNodeOrPredicate) {
|
|
787
749
|
if (elementNodeOrPredicate === undefined)
|
|
788
750
|
return;
|
|
751
|
+
if (this.isNode(elementNodeOrPredicate))
|
|
752
|
+
return elementNodeOrPredicate;
|
|
789
753
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
790
754
|
let current = this.head;
|
|
791
755
|
while (current) {
|
|
@@ -808,16 +772,16 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
808
772
|
* `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
809
773
|
* @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
|
|
810
774
|
* successfully added at the specified index, and `false` if the index is out of bounds (less than 0
|
|
811
|
-
* or greater than the
|
|
775
|
+
* or greater than the length of the list).
|
|
812
776
|
*/
|
|
813
777
|
addAt(index, newElementOrNode) {
|
|
814
|
-
if (index < 0 || index > this.
|
|
778
|
+
if (index < 0 || index > this._length)
|
|
815
779
|
return false;
|
|
816
780
|
if (index === 0) {
|
|
817
781
|
this.unshift(newElementOrNode);
|
|
818
782
|
return true;
|
|
819
783
|
}
|
|
820
|
-
if (index === this.
|
|
784
|
+
if (index === this._length) {
|
|
821
785
|
this.push(newElementOrNode);
|
|
822
786
|
return true;
|
|
823
787
|
}
|
|
@@ -828,7 +792,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
828
792
|
newNode.next = nextNode;
|
|
829
793
|
prevNode.next = newNode;
|
|
830
794
|
nextNode.prev = newNode;
|
|
831
|
-
this.
|
|
795
|
+
this._length++;
|
|
832
796
|
return true;
|
|
833
797
|
}
|
|
834
798
|
/**
|
|
@@ -861,7 +825,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
861
825
|
if (existingNode === this.head) {
|
|
862
826
|
this._head = newNode;
|
|
863
827
|
}
|
|
864
|
-
this.
|
|
828
|
+
this._length++;
|
|
865
829
|
return true;
|
|
866
830
|
}
|
|
867
831
|
return false;
|
|
@@ -897,7 +861,28 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
897
861
|
if (existingNode === this.tail) {
|
|
898
862
|
this._tail = newNode;
|
|
899
863
|
}
|
|
900
|
-
this.
|
|
864
|
+
this._length++;
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Time Complexity: O(n)
|
|
871
|
+
* Space Complexity: O(1)
|
|
872
|
+
*
|
|
873
|
+
* The function `setAt` updates the value at a specified index in a data structure if the index
|
|
874
|
+
* exists.
|
|
875
|
+
* @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
|
|
876
|
+
* data structure where you want to set a new value.
|
|
877
|
+
* @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
|
|
878
|
+
* want to set at the specified index in the data structure.
|
|
879
|
+
* @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
|
|
880
|
+
* is successfully updated, and `false` if the index is out of bounds.
|
|
881
|
+
*/
|
|
882
|
+
setAt(index, value) {
|
|
883
|
+
const node = this.getNodeAt(index);
|
|
884
|
+
if (node) {
|
|
885
|
+
node.value = value;
|
|
901
886
|
return true;
|
|
902
887
|
}
|
|
903
888
|
return false;
|
|
@@ -913,23 +898,26 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
913
898
|
* bounds.
|
|
914
899
|
*/
|
|
915
900
|
deleteAt(index) {
|
|
916
|
-
if (index < 0 || index >= this.
|
|
917
|
-
return
|
|
901
|
+
if (index < 0 || index >= this._length)
|
|
902
|
+
return;
|
|
903
|
+
let deleted;
|
|
918
904
|
if (index === 0) {
|
|
905
|
+
deleted = this.first;
|
|
919
906
|
this.shift();
|
|
920
|
-
return
|
|
907
|
+
return deleted;
|
|
921
908
|
}
|
|
922
|
-
if (index === this.
|
|
909
|
+
if (index === this._length - 1) {
|
|
910
|
+
deleted = this.last;
|
|
923
911
|
this.pop();
|
|
924
|
-
return
|
|
912
|
+
return deleted;
|
|
925
913
|
}
|
|
926
914
|
const removedNode = this.getNodeAt(index);
|
|
927
915
|
const prevNode = removedNode.prev;
|
|
928
916
|
const nextNode = removedNode.next;
|
|
929
917
|
prevNode.next = nextNode;
|
|
930
918
|
nextNode.prev = prevNode;
|
|
931
|
-
this.
|
|
932
|
-
return
|
|
919
|
+
this._length--;
|
|
920
|
+
return removedNode === null || removedNode === void 0 ? void 0 : removedNode.value;
|
|
933
921
|
}
|
|
934
922
|
/**
|
|
935
923
|
* Time Complexity: O(1) or O(n)
|
|
@@ -960,7 +948,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
960
948
|
prevNode.next = nextNode;
|
|
961
949
|
if (nextNode)
|
|
962
950
|
nextNode.prev = prevNode;
|
|
963
|
-
this.
|
|
951
|
+
this._length--;
|
|
964
952
|
}
|
|
965
953
|
return true;
|
|
966
954
|
}
|
|
@@ -970,46 +958,22 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
970
958
|
* Time Complexity: O(1)
|
|
971
959
|
* Space Complexity: O(1)
|
|
972
960
|
*
|
|
973
|
-
* The function checks if a variable has a
|
|
961
|
+
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
974
962
|
* @returns A boolean value is being returned.
|
|
975
963
|
*/
|
|
976
964
|
isEmpty() {
|
|
977
|
-
return this.
|
|
965
|
+
return this._length === 0;
|
|
978
966
|
}
|
|
979
967
|
/**
|
|
980
968
|
* Time Complexity: O(1)
|
|
981
969
|
* Space Complexity: O(1)
|
|
982
970
|
*
|
|
983
|
-
* The `clear` function resets the linked list by setting the head, tail, and
|
|
971
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
984
972
|
*/
|
|
985
973
|
clear() {
|
|
986
974
|
this._head = undefined;
|
|
987
975
|
this._tail = undefined;
|
|
988
|
-
this.
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* Time Complexity: O(n)
|
|
992
|
-
* Space Complexity: O(1)
|
|
993
|
-
*
|
|
994
|
-
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
|
|
995
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
996
|
-
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
|
|
997
|
-
* can be one of the following:
|
|
998
|
-
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
|
|
999
|
-
* matches the provided element, node, or predicate. If no match is found, it returns -1.
|
|
1000
|
-
*/
|
|
1001
|
-
indexOf(elementNodeOrPredicate) {
|
|
1002
|
-
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1003
|
-
let index = 0;
|
|
1004
|
-
let current = this.head;
|
|
1005
|
-
while (current) {
|
|
1006
|
-
if (predicate(current)) {
|
|
1007
|
-
return index;
|
|
1008
|
-
}
|
|
1009
|
-
index++;
|
|
1010
|
-
current = current.next;
|
|
1011
|
-
}
|
|
1012
|
-
return -1;
|
|
976
|
+
this._length = 0;
|
|
1013
977
|
}
|
|
1014
978
|
/**
|
|
1015
979
|
* Time Complexity: O(n)
|
|
@@ -1072,38 +1036,6 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1072
1036
|
}
|
|
1073
1037
|
return this;
|
|
1074
1038
|
}
|
|
1075
|
-
/**
|
|
1076
|
-
* Time Complexity: O(n)
|
|
1077
|
-
* Space Complexity: O(n)
|
|
1078
|
-
*
|
|
1079
|
-
* The `toArray` function converts a linked list into an array.
|
|
1080
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
1081
|
-
*/
|
|
1082
|
-
toArray() {
|
|
1083
|
-
const array = [];
|
|
1084
|
-
let current = this.head;
|
|
1085
|
-
while (current) {
|
|
1086
|
-
array.push(current.value);
|
|
1087
|
-
current = current.next;
|
|
1088
|
-
}
|
|
1089
|
-
return array;
|
|
1090
|
-
}
|
|
1091
|
-
/**
|
|
1092
|
-
* Time Complexity: O(n)
|
|
1093
|
-
* Space Complexity: O(n)
|
|
1094
|
-
*
|
|
1095
|
-
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
1096
|
-
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
1097
|
-
*/
|
|
1098
|
-
toReversedArray() {
|
|
1099
|
-
const array = [];
|
|
1100
|
-
let current = this.tail;
|
|
1101
|
-
while (current) {
|
|
1102
|
-
array.push(current.value);
|
|
1103
|
-
current = current.prev;
|
|
1104
|
-
}
|
|
1105
|
-
return array;
|
|
1106
|
-
}
|
|
1107
1039
|
/**
|
|
1108
1040
|
* Time Complexity: O(n)
|
|
1109
1041
|
* Space Complexity: O(n)
|
|
@@ -1114,7 +1046,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1114
1046
|
* is a copy of the original list.
|
|
1115
1047
|
*/
|
|
1116
1048
|
clone() {
|
|
1117
|
-
return new DoublyLinkedList(this);
|
|
1049
|
+
return new DoublyLinkedList(this, { toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
1118
1050
|
}
|
|
1119
1051
|
/**
|
|
1120
1052
|
* Time Complexity: O(n)
|
|
@@ -1134,7 +1066,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1134
1066
|
* elements that pass the filter condition specified by the `callback` function.
|
|
1135
1067
|
*/
|
|
1136
1068
|
filter(callback, thisArg) {
|
|
1137
|
-
const filteredList =
|
|
1069
|
+
const filteredList = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1138
1070
|
let index = 0;
|
|
1139
1071
|
for (const current of this) {
|
|
1140
1072
|
if (callback.call(thisArg, current, index, this)) {
|
|
@@ -1165,7 +1097,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1165
1097
|
* @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
|
|
1166
1098
|
*/
|
|
1167
1099
|
map(callback, toElementFn, thisArg) {
|
|
1168
|
-
const mappedList = new DoublyLinkedList([], { toElementFn });
|
|
1100
|
+
const mappedList = new DoublyLinkedList([], { toElementFn, maxLen: this._maxLen });
|
|
1169
1101
|
let index = 0;
|
|
1170
1102
|
for (const current of this) {
|
|
1171
1103
|
mappedList.push(callback.call(thisArg, current, index, this));
|
|
@@ -1206,6 +1138,35 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1206
1138
|
current = current.next;
|
|
1207
1139
|
}
|
|
1208
1140
|
}
|
|
1141
|
+
/**
|
|
1142
|
+
* The function returns an iterator that iterates over the elements of a data structure in reverse
|
|
1143
|
+
* order.
|
|
1144
|
+
*/
|
|
1145
|
+
*_getReverseIterator() {
|
|
1146
|
+
let current = this.tail;
|
|
1147
|
+
while (current) {
|
|
1148
|
+
yield current.value;
|
|
1149
|
+
current = current.prev;
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* The function returns an iterator that iterates over the nodes of a doubly linked list starting
|
|
1154
|
+
* from the head.
|
|
1155
|
+
*/
|
|
1156
|
+
*_getNodeIterator() {
|
|
1157
|
+
let current = this.head;
|
|
1158
|
+
while (current) {
|
|
1159
|
+
yield current;
|
|
1160
|
+
current = current.next;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
// protected *_getReverseNodeIterator(): IterableIterator<DoublyLinkedListNode<E>> {
|
|
1164
|
+
// const reversedArr = [...this._getNodeIterator()].reverse();
|
|
1165
|
+
//
|
|
1166
|
+
// for (const item of reversedArr) {
|
|
1167
|
+
// yield item;
|
|
1168
|
+
// }
|
|
1169
|
+
// }
|
|
1209
1170
|
/**
|
|
1210
1171
|
* The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
|
|
1211
1172
|
* as an argument and returns a boolean.
|
|
@@ -1248,5 +1209,29 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1248
1209
|
return elementNodeOrPredicate;
|
|
1249
1210
|
return (node) => node.value === elementNodeOrPredicate;
|
|
1250
1211
|
}
|
|
1212
|
+
/**
|
|
1213
|
+
* The function `_createInstance` returns a new instance of `DoublyLinkedList` with the specified
|
|
1214
|
+
* options.
|
|
1215
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
1216
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
1217
|
+
* configuration options when creating a new instance of the `DoublyLinkedList` class.
|
|
1218
|
+
* @returns An instance of the `DoublyLinkedList` class with an empty array and the provided options
|
|
1219
|
+
* is being returned, cast as the current class type.
|
|
1220
|
+
*/
|
|
1221
|
+
_createInstance(options) {
|
|
1222
|
+
return new DoublyLinkedList([], options);
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* The function `_getPrevNode` returns the previous node of a given node in a doubly linked list.
|
|
1226
|
+
* @param node - The parameter `node` in the `_getPrevNode` method is of type
|
|
1227
|
+
* `DoublyLinkedListNode<E>`, which represents a node in a doubly linked list containing an element
|
|
1228
|
+
* of type `E`.
|
|
1229
|
+
* @returns The `_getPrevNode` method is returning the previous node of the input `node` in a doubly
|
|
1230
|
+
* linked list. If the input node has a previous node, it will return that node. Otherwise, it will
|
|
1231
|
+
* return `undefined`.
|
|
1232
|
+
*/
|
|
1233
|
+
_getPrevNode(node) {
|
|
1234
|
+
return node.prev;
|
|
1235
|
+
}
|
|
1251
1236
|
}
|
|
1252
1237
|
exports.DoublyLinkedList = DoublyLinkedList;
|