data-structure-typed 0.8.18 → 1.3.0
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/LICENSE +21 -0
- package/README.md +690 -2
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -18
- package/dist/data-structures/binary-tree/avl-tree.js +110 -37
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
- package/dist/data-structures/binary-tree/binary-tree.js +27 -979
- package/dist/data-structures/binary-tree/bst.d.ts +118 -28
- package/dist/data-structures/binary-tree/bst.js +162 -124
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
- package/dist/data-structures/binary-tree/segment-tree.js +80 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
- package/dist/data-structures/graph/abstract-graph.js +365 -92
- package/dist/data-structures/graph/directed-graph.d.ts +175 -26
- package/dist/data-structures/graph/directed-graph.js +249 -95
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -8
- package/dist/data-structures/graph/undirected-graph.js +154 -44
- package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
- package/dist/data-structures/hash/coordinate-map.js +44 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +34 -0
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -51
- package/dist/data-structures/heap/heap.js +106 -63
- package/dist/data-structures/heap/max-heap.d.ts +13 -4
- package/dist/data-structures/heap/max-heap.js +10 -2
- package/dist/data-structures/heap/min-heap.d.ts +14 -4
- package/dist/data-structures/heap/min-heap.js +11 -2
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -319
- package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
- package/dist/data-structures/matrix/matrix2d.js +91 -8
- package/dist/data-structures/matrix/navigator.d.ts +37 -16
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +156 -29
- package/dist/data-structures/matrix/vector2d.js +184 -55
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
- package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
- package/dist/data-structures/priority-queue/priority-queue.js +219 -75
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +151 -7
- package/dist/data-structures/queue/queue.d.ts +68 -42
- package/dist/data-structures/queue/queue.js +95 -51
- package/dist/data-structures/stack/stack.d.ts +30 -36
- package/dist/data-structures/stack/stack.js +31 -37
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/{types/utils.js → data-structures/tree/tree.js} +26 -19
- package/dist/data-structures/trie/trie.d.ts +39 -6
- package/dist/data-structures/trie/trie.js +81 -12
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-binary-tree.js +2 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/abstract-graph.js +2 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/avl-tree.js +2 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/bst.js +2 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/dist/interfaces/directed-graph.js +2 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/index.js +31 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/rb-tree.js +2 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +13 -7
- package/dist/types/data-structures/index.js +31 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +19 -0
- package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
- package/package.json +106 -55
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.d.ts +0 -122
- package/dist/utils.js +0 -569
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -232
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1088
- package/src/data-structures/binary-tree/bst.ts +0 -404
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -164
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -789
- package/src/data-structures/graph/directed-graph.ts +0 -322
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -154
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -136
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -22
- package/src/data-structures/heap/min-heap.ts +0 -24
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -99
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/priority-queue.ts +0 -208
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -123
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -104
- package/src/data-structures/trampoline.ts +0 -91
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -153
- package/src/index.ts +0 -1
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +0 -158
- package/src/utils.ts +0 -605
- package/tsconfig.json +0 -53
- /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
- /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
- /package/dist/{types/types → utils}/index.d.ts +0 -0
|
@@ -2,659 +2,440 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
9
10
|
*/
|
|
10
11
|
class SinglyLinkedListNode {
|
|
11
|
-
constructor(
|
|
12
|
-
/** Data stored on the node */
|
|
13
|
-
val,
|
|
14
|
-
/** The previous node in the list */
|
|
15
|
-
prev,
|
|
16
|
-
/** The next link in the list */
|
|
17
|
-
next,
|
|
18
|
-
/** The list this node belongs to */
|
|
19
|
-
list) {
|
|
20
|
-
this.val = val;
|
|
21
|
-
this.prev = prev;
|
|
22
|
-
this.next = next;
|
|
23
|
-
this.list = list;
|
|
24
|
-
}
|
|
25
12
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* ```
|
|
13
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
14
|
+
* @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
|
|
15
|
+
* will be stored in the node of a linked list.
|
|
30
16
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
constructor(val) {
|
|
18
|
+
this._val = val;
|
|
19
|
+
this._next = null;
|
|
33
20
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
* ```ts
|
|
37
|
-
* new LinkedList(1, 2, 3).head.index; // 0
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
get index() {
|
|
41
|
-
if (!this.list) {
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
return this.list.findIndex((value) => value === this.value);
|
|
21
|
+
get val() {
|
|
22
|
+
return this._val;
|
|
45
23
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* ```ts
|
|
49
|
-
* new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3
|
|
50
|
-
* ```
|
|
51
|
-
* @param val Data to save in the node
|
|
52
|
-
*/
|
|
53
|
-
insertBefore(val) {
|
|
54
|
-
return this.list !== null
|
|
55
|
-
? this.list.insertBefore(this, val)
|
|
56
|
-
: new SinglyLinkedList(val, this.val);
|
|
24
|
+
set val(value) {
|
|
25
|
+
this._val = value;
|
|
57
26
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
* ```ts
|
|
61
|
-
* new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3
|
|
62
|
-
* ```
|
|
63
|
-
* @param val Data to be saved in the node
|
|
64
|
-
*/
|
|
65
|
-
insertAfter(val) {
|
|
66
|
-
return this.list !== null
|
|
67
|
-
? this.list.insertAfter(this, val)
|
|
68
|
-
: new SinglyLinkedList(this.val, val);
|
|
27
|
+
get next() {
|
|
28
|
+
return this._next;
|
|
69
29
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
* ```ts
|
|
73
|
-
* new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
remove() {
|
|
77
|
-
if (this.list === null) {
|
|
78
|
-
throw new ReferenceError('Node does not belong to any list');
|
|
79
|
-
}
|
|
80
|
-
return this.list.removeNode(this);
|
|
30
|
+
set next(value) {
|
|
31
|
+
this._next = value;
|
|
81
32
|
}
|
|
82
33
|
}
|
|
83
34
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
84
|
-
/**
|
|
85
|
-
* A doubly linked list
|
|
86
|
-
* ```ts
|
|
87
|
-
* const list = new LinkedList(1, 2, 3);
|
|
88
|
-
* const listFromArray = LinkedList.from([1, 2, 3]);
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
35
|
class SinglyLinkedList {
|
|
92
36
|
/**
|
|
93
|
-
* The
|
|
37
|
+
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
94
38
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
39
|
+
constructor() {
|
|
40
|
+
this._head = null;
|
|
41
|
+
this._tail = null;
|
|
42
|
+
this._length = 0;
|
|
97
43
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
* ```javascript
|
|
101
|
-
* const array = [1, 2, 3];
|
|
102
|
-
* const list = LinkedList.from(array);
|
|
103
|
-
* ```
|
|
104
|
-
* @param iterable Any iterable datatype like Array or Map
|
|
105
|
-
*/
|
|
106
|
-
static from(iterable) {
|
|
107
|
-
return new SinglyLinkedList(...iterable);
|
|
108
|
-
}
|
|
109
|
-
constructor(...args) {
|
|
110
|
-
this.head = null;
|
|
111
|
-
this.tail = null;
|
|
112
|
-
this.size = 0;
|
|
113
|
-
for (let i = 0; i < arguments.length; i++) {
|
|
114
|
-
this.append(args[i]);
|
|
115
|
-
}
|
|
44
|
+
get head() {
|
|
45
|
+
return this._head;
|
|
116
46
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
* ```ts
|
|
120
|
-
* new LinkedList(1, 2, 3).get(0); // 1
|
|
121
|
-
* ```
|
|
122
|
-
* @param index to retrieve val at
|
|
123
|
-
*/
|
|
124
|
-
get(index) {
|
|
125
|
-
const node = this.getNode(index);
|
|
126
|
-
return node !== undefined ? node.val : undefined;
|
|
47
|
+
set head(value) {
|
|
48
|
+
this._head = value;
|
|
127
49
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
* ```ts
|
|
131
|
-
* new LinkedList(1, 2, 3).getNode(0);
|
|
132
|
-
* // { prev: null, val: 1, next: SinglyLinkedListNode }
|
|
133
|
-
* ```
|
|
134
|
-
*/
|
|
135
|
-
getNode(index) {
|
|
136
|
-
if (this.head === null || index < 0 || index >= this.length) {
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
const asc = index < this.length / 2;
|
|
140
|
-
const stopAt = asc ? index : this.length - index - 1;
|
|
141
|
-
const nextNode = asc ? 'next' : 'prev';
|
|
142
|
-
let currentNode = asc ? this.head : this.tail;
|
|
143
|
-
// TODO after no-non-null-assertion not ensure the logic
|
|
144
|
-
for (let currentIndex = 0; currentIndex < stopAt; currentIndex++) {
|
|
145
|
-
if (currentNode) {
|
|
146
|
-
currentNode = currentNode[nextNode];
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return currentNode || undefined;
|
|
50
|
+
get tail() {
|
|
51
|
+
return this._tail;
|
|
150
52
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
* satisfies the testing function
|
|
154
|
-
* ```ts
|
|
155
|
-
* new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
|
|
156
|
-
* // { node: SinglyLinkedListNode, index: 0 }
|
|
157
|
-
* ```
|
|
158
|
-
* @param f A function to be applied to the val of each node
|
|
159
|
-
*/
|
|
160
|
-
findNodeIndex(f) {
|
|
161
|
-
let currentIndex = 0;
|
|
162
|
-
let currentNode = this.head;
|
|
163
|
-
while (currentNode) {
|
|
164
|
-
if (f(currentNode.val, currentIndex, this)) {
|
|
165
|
-
return {
|
|
166
|
-
index: currentIndex,
|
|
167
|
-
node: currentNode,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
currentNode = currentNode.next;
|
|
171
|
-
currentIndex += 1;
|
|
172
|
-
}
|
|
173
|
-
return undefined;
|
|
53
|
+
set tail(value) {
|
|
54
|
+
this._tail = value;
|
|
174
55
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
* satisfies the provided testing function. Otherwise undefined is returned.
|
|
178
|
-
* ```ts
|
|
179
|
-
* new LinkedList(1, 2, 3).findNode(val => val === 1);
|
|
180
|
-
* // { prev: null, val: 1, next: SinglyLinkedListNode }
|
|
181
|
-
* ```
|
|
182
|
-
* @param f Function to test val against
|
|
183
|
-
*/
|
|
184
|
-
findNode(f) {
|
|
185
|
-
const nodeIndex = this.findNodeIndex(f);
|
|
186
|
-
return nodeIndex !== undefined ? nodeIndex.node : undefined;
|
|
56
|
+
get length() {
|
|
57
|
+
return this._length;
|
|
187
58
|
}
|
|
188
59
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
* ```
|
|
194
|
-
* @param f Function to test val against
|
|
60
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
61
|
+
* array.
|
|
62
|
+
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
|
63
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
195
64
|
*/
|
|
196
|
-
|
|
197
|
-
const
|
|
198
|
-
|
|
65
|
+
static fromArray(data) {
|
|
66
|
+
const singlyLinkedList = new SinglyLinkedList();
|
|
67
|
+
for (const item of data) {
|
|
68
|
+
singlyLinkedList.push(item);
|
|
69
|
+
}
|
|
70
|
+
return singlyLinkedList;
|
|
199
71
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
* satisfies the provided testing function. Ohterwise -1 is returned.
|
|
203
|
-
* ```ts
|
|
204
|
-
* new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
|
|
205
|
-
* ```
|
|
206
|
-
* @param f Function to test val against
|
|
207
|
-
*/
|
|
208
|
-
findIndex(f) {
|
|
209
|
-
const nodeIndex = this.findNodeIndex(f);
|
|
210
|
-
return nodeIndex !== undefined ? nodeIndex.index : -1;
|
|
72
|
+
getLength() {
|
|
73
|
+
return this._length;
|
|
211
74
|
}
|
|
212
75
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* ```ts
|
|
217
|
-
* new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
|
|
218
|
-
* ```
|
|
219
|
-
* @param args Data to be stored in the node, takes any number of arguments
|
|
76
|
+
* The `push` function adds a new node with the given data to the end of a singly linked list.
|
|
77
|
+
* @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
|
|
78
|
+
* any type (T) as specified in the generic type declaration of the class or function.
|
|
220
79
|
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
if (this.tail !== null) {
|
|
228
|
-
this.tail.next = node;
|
|
229
|
-
}
|
|
230
|
-
this.tail = node;
|
|
231
|
-
this.size += 1;
|
|
80
|
+
push(data) {
|
|
81
|
+
const newNode = new SinglyLinkedListNode(data);
|
|
82
|
+
if (!this.head) {
|
|
83
|
+
this.head = newNode;
|
|
84
|
+
this.tail = newNode;
|
|
232
85
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
* new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4
|
|
239
|
-
* ```
|
|
240
|
-
* @param args Data to be stored, takes any number of arguments
|
|
241
|
-
*/
|
|
242
|
-
push(...args) {
|
|
243
|
-
this.append(...args);
|
|
244
|
-
return this.length;
|
|
86
|
+
else {
|
|
87
|
+
this.tail.next = newNode;
|
|
88
|
+
this.tail = newNode;
|
|
89
|
+
}
|
|
90
|
+
this._length++;
|
|
245
91
|
}
|
|
246
92
|
/**
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* ```
|
|
252
|
-
* @param args Data to be stored in the node, accepts any number of arguments
|
|
93
|
+
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
94
|
+
* pointers accordingly.
|
|
95
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
96
|
+
* the linked list is empty, it returns `null`.
|
|
253
97
|
*/
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
98
|
+
pop() {
|
|
99
|
+
if (!this.head)
|
|
100
|
+
return undefined;
|
|
101
|
+
if (this.head === this.tail) {
|
|
102
|
+
const val = this.head.val;
|
|
103
|
+
this.head = null;
|
|
104
|
+
this.tail = null;
|
|
105
|
+
this._length--;
|
|
106
|
+
return val;
|
|
107
|
+
}
|
|
108
|
+
let current = this.head;
|
|
109
|
+
while (current.next !== this.tail) {
|
|
110
|
+
current = current.next;
|
|
266
111
|
}
|
|
267
|
-
|
|
112
|
+
const val = this.tail.val;
|
|
113
|
+
current.next = null;
|
|
114
|
+
this.tail = current;
|
|
115
|
+
this._length--;
|
|
116
|
+
return val;
|
|
268
117
|
}
|
|
269
118
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* or 0, it will be prepended.
|
|
273
|
-
* ```ts
|
|
274
|
-
* new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
|
|
275
|
-
* ```
|
|
276
|
-
* @param index The index to insert the new node at
|
|
277
|
-
* @param val Data to be stored on the new node
|
|
119
|
+
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
120
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
278
121
|
*/
|
|
279
|
-
|
|
280
|
-
if (this.head
|
|
281
|
-
return
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
let currentNode = this.head;
|
|
287
|
-
let currentIndex = 0;
|
|
288
|
-
while (currentIndex < index - 1 && currentNode.next !== null) {
|
|
289
|
-
currentIndex += 1;
|
|
290
|
-
currentNode = currentNode.next;
|
|
291
|
-
}
|
|
292
|
-
currentNode.insertAfter(val);
|
|
293
|
-
return this;
|
|
122
|
+
shift() {
|
|
123
|
+
if (!this.head)
|
|
124
|
+
return undefined;
|
|
125
|
+
const removedNode = this.head;
|
|
126
|
+
this.head = this.head.next;
|
|
127
|
+
this._length--;
|
|
128
|
+
return removedNode.val;
|
|
294
129
|
}
|
|
295
130
|
/**
|
|
296
|
-
*
|
|
297
|
-
* node
|
|
298
|
-
*
|
|
299
|
-
* const list = new LinkedList(1, 2, 3);
|
|
300
|
-
* list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
|
|
301
|
-
* ```
|
|
302
|
-
* @param node The node to be removed
|
|
131
|
+
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
132
|
+
* @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
133
|
+
* linked list.
|
|
303
134
|
*/
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
node.prev.next = node.next;
|
|
135
|
+
unshift(val) {
|
|
136
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
137
|
+
if (!this.head) {
|
|
138
|
+
this.head = newNode;
|
|
139
|
+
this.tail = newNode;
|
|
310
140
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (this.head === node) {
|
|
315
|
-
this.head = node.next;
|
|
316
|
-
}
|
|
317
|
-
if (this.tail === node) {
|
|
318
|
-
this.tail = node.prev;
|
|
141
|
+
else {
|
|
142
|
+
newNode.next = this.head;
|
|
143
|
+
this.head = newNode;
|
|
319
144
|
}
|
|
320
|
-
this.
|
|
321
|
-
node.next = null;
|
|
322
|
-
node.prev = null;
|
|
323
|
-
node.list = null;
|
|
324
|
-
return node;
|
|
145
|
+
this._length++;
|
|
325
146
|
}
|
|
326
147
|
/**
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
148
|
+
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
149
|
+
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
150
|
+
* retrieve from the list.
|
|
151
|
+
* @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
|
|
152
|
+
* `null` if the index is out of bounds.
|
|
332
153
|
*/
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
154
|
+
getAt(index) {
|
|
155
|
+
if (index < 0 || index >= this.length)
|
|
156
|
+
return null;
|
|
157
|
+
let current = this.head;
|
|
158
|
+
for (let i = 0; i < index; i++) {
|
|
159
|
+
current = current.next;
|
|
160
|
+
}
|
|
161
|
+
return current.val;
|
|
336
162
|
}
|
|
337
163
|
/**
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* @param referenceNode The node reference
|
|
344
|
-
* @param val Data to save in the node
|
|
164
|
+
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
165
|
+
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
166
|
+
* retrieve from the linked list. It indicates the zero-based index of the node we want to access.
|
|
167
|
+
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
|
|
168
|
+
* specified index exists, or `null` if the index is out of bounds.
|
|
345
169
|
*/
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
if (referenceNode.prev !== null) {
|
|
352
|
-
referenceNode.prev.next = node;
|
|
170
|
+
getNodeAt(index) {
|
|
171
|
+
let current = this.head;
|
|
172
|
+
for (let i = 0; i < index; i++) {
|
|
173
|
+
current = current.next;
|
|
353
174
|
}
|
|
354
|
-
|
|
355
|
-
this.size += 1;
|
|
356
|
-
return this;
|
|
175
|
+
return current;
|
|
357
176
|
}
|
|
358
177
|
/**
|
|
359
|
-
*
|
|
360
|
-
* @param
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
178
|
+
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
179
|
+
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
180
|
+
* data structure. It is of type number.
|
|
181
|
+
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
182
|
+
* bounds.
|
|
364
183
|
*/
|
|
365
|
-
|
|
366
|
-
if (
|
|
367
|
-
return
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
184
|
+
deleteAt(index) {
|
|
185
|
+
if (index < 0 || index >= this.length)
|
|
186
|
+
return undefined;
|
|
187
|
+
if (index === 0)
|
|
188
|
+
return this.shift();
|
|
189
|
+
if (index === this.length - 1)
|
|
190
|
+
return this.pop();
|
|
191
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
192
|
+
const removedNode = prevNode.next;
|
|
193
|
+
prevNode.next = removedNode.next;
|
|
194
|
+
this._length--;
|
|
195
|
+
return removedNode.val;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The delete function removes a node with a specific value from a singly linked list.
|
|
199
|
+
* @param {T | SinglyLinkedListNode<T>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `T`
|
|
200
|
+
* or a `SinglyLinkedListNode<T>` object.
|
|
201
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
202
|
+
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
203
|
+
*/
|
|
204
|
+
delete(valueOrNode) {
|
|
205
|
+
let value;
|
|
206
|
+
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
207
|
+
value = valueOrNode.val;
|
|
371
208
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
if (current !== split) {
|
|
383
|
-
const temp = split.val;
|
|
384
|
-
split.val = current.val;
|
|
385
|
-
current.val = temp;
|
|
209
|
+
else {
|
|
210
|
+
value = valueOrNode;
|
|
211
|
+
}
|
|
212
|
+
let current = this.head, prev = null;
|
|
213
|
+
while (current) {
|
|
214
|
+
if (current.val === value) {
|
|
215
|
+
if (prev === null) {
|
|
216
|
+
this.head = current.next;
|
|
217
|
+
if (current === this.tail) {
|
|
218
|
+
this.tail = null;
|
|
386
219
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
prev.next = current.next;
|
|
223
|
+
if (current === this.tail) {
|
|
224
|
+
this.tail = prev;
|
|
390
225
|
}
|
|
391
226
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
end.val = split.val;
|
|
395
|
-
split.val = pivotData;
|
|
396
|
-
if (start.next === end.prev) {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
if (split.prev && split !== start) {
|
|
400
|
-
quicksort(start, split.prev);
|
|
401
|
-
}
|
|
402
|
-
if (split.next && split !== end) {
|
|
403
|
-
quicksort(split.next, end);
|
|
227
|
+
this._length--;
|
|
228
|
+
return true;
|
|
404
229
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
230
|
+
prev = current;
|
|
231
|
+
current = current.next;
|
|
232
|
+
}
|
|
233
|
+
return false;
|
|
408
234
|
}
|
|
409
235
|
/**
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* @
|
|
416
|
-
*
|
|
236
|
+
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
237
|
+
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
238
|
+
* linked list. It is of type number.
|
|
239
|
+
* @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
|
|
240
|
+
* specified index.
|
|
241
|
+
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
242
|
+
* if the index is out of bounds.
|
|
417
243
|
*/
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
244
|
+
insertAt(index, val) {
|
|
245
|
+
if (index < 0 || index > this.length)
|
|
246
|
+
return false;
|
|
247
|
+
if (index === 0) {
|
|
248
|
+
this.unshift(val);
|
|
249
|
+
return true;
|
|
422
250
|
}
|
|
423
|
-
if (
|
|
424
|
-
|
|
251
|
+
if (index === this.length) {
|
|
252
|
+
this.push(val);
|
|
253
|
+
return true;
|
|
425
254
|
}
|
|
426
|
-
|
|
427
|
-
this.
|
|
428
|
-
|
|
255
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
256
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
257
|
+
newNode.next = prevNode.next;
|
|
258
|
+
prevNode.next = newNode;
|
|
259
|
+
this._length++;
|
|
260
|
+
return true;
|
|
429
261
|
}
|
|
430
262
|
/**
|
|
431
|
-
*
|
|
432
|
-
* or
|
|
433
|
-
*
|
|
434
|
-
* new LinkedList(1, 2, 3).shift(); // 1
|
|
435
|
-
* ```
|
|
263
|
+
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
264
|
+
* whether it is empty or not.
|
|
265
|
+
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
436
266
|
*/
|
|
437
|
-
|
|
438
|
-
return this.
|
|
267
|
+
isEmpty() {
|
|
268
|
+
return this.length === 0;
|
|
439
269
|
}
|
|
440
270
|
/**
|
|
441
|
-
*
|
|
442
|
-
* or undefined if the list was empty
|
|
443
|
-
* ```ts
|
|
444
|
-
* new LinkedList(1, 2, 3).pop(); // 3
|
|
445
|
-
* ```
|
|
446
|
-
*/
|
|
447
|
-
pop() {
|
|
448
|
-
return this.removeFromAnyEnd(this.tail);
|
|
449
|
-
}
|
|
450
|
-
/**
|
|
451
|
-
* Merge the current list with another. Both lists will be
|
|
452
|
-
* equal after merging.
|
|
453
|
-
* ```ts
|
|
454
|
-
* const list = new LinkedList(1, 2);
|
|
455
|
-
* const otherList = new LinkedList(3);
|
|
456
|
-
* list.merge(otherList);
|
|
457
|
-
* (list === otherList); // true
|
|
458
|
-
* ```
|
|
459
|
-
* @param list The list to be merged
|
|
460
|
-
*/
|
|
461
|
-
merge(list) {
|
|
462
|
-
if (this.tail !== null) {
|
|
463
|
-
this.tail.next = list.head;
|
|
464
|
-
}
|
|
465
|
-
if (list.head !== null) {
|
|
466
|
-
list.head.prev = this.tail;
|
|
467
|
-
}
|
|
468
|
-
this.head = this.head || list.head;
|
|
469
|
-
this.tail = list.tail || this.tail;
|
|
470
|
-
this.size += list.size;
|
|
471
|
-
list.size = this.size;
|
|
472
|
-
list.head = this.head;
|
|
473
|
-
list.tail = this.tail;
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Removes all nodes from a list
|
|
477
|
-
*
|
|
478
|
-
* ```ts
|
|
479
|
-
* list.clear();
|
|
480
|
-
* ```
|
|
271
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
481
272
|
*/
|
|
482
273
|
clear() {
|
|
483
|
-
this.
|
|
484
|
-
this.
|
|
485
|
-
this.
|
|
486
|
-
return this;
|
|
274
|
+
this._head = null;
|
|
275
|
+
this._tail = null;
|
|
276
|
+
this._length = 0;
|
|
487
277
|
}
|
|
488
278
|
/**
|
|
489
|
-
* The
|
|
490
|
-
*
|
|
491
|
-
* from start to end (end not included).
|
|
492
|
-
* The original list will not be modified.
|
|
493
|
-
* ```ts
|
|
494
|
-
* const list = new LinkedList(1, 2, 3, 4, 5);
|
|
495
|
-
* const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
|
|
496
|
-
* ```
|
|
497
|
-
* @param start Start index
|
|
498
|
-
* @param end End index, optional
|
|
279
|
+
* The `toArray` function converts a linked list into an array.
|
|
280
|
+
* @returns The `toArray()` method is returning an array of type `T[]`.
|
|
499
281
|
*/
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
}
|
|
507
|
-
if (finish === undefined || finish < start) {
|
|
508
|
-
finish = this.length;
|
|
509
|
-
}
|
|
510
|
-
let head = this.getNode(start);
|
|
511
|
-
for (let i = 0; i < finish - start && head !== null && head !== undefined; i++) {
|
|
512
|
-
list.append(head.val);
|
|
513
|
-
head = head.next;
|
|
282
|
+
toArray() {
|
|
283
|
+
const array = [];
|
|
284
|
+
let current = this.head;
|
|
285
|
+
while (current) {
|
|
286
|
+
array.push(current.val);
|
|
287
|
+
current = current.next;
|
|
514
288
|
}
|
|
515
|
-
return
|
|
289
|
+
return array;
|
|
516
290
|
}
|
|
517
291
|
/**
|
|
518
|
-
* The reverse
|
|
519
|
-
*
|
|
520
|
-
* ```ts
|
|
521
|
-
* new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
|
|
522
|
-
* ```
|
|
292
|
+
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
293
|
+
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
523
294
|
*/
|
|
524
295
|
reverse() {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
296
|
+
if (!this.head || this.head === this.tail)
|
|
297
|
+
return;
|
|
298
|
+
let prev = null;
|
|
299
|
+
let current = this.head;
|
|
300
|
+
let next = null;
|
|
301
|
+
while (current) {
|
|
302
|
+
next = current.next;
|
|
303
|
+
current.next = prev;
|
|
304
|
+
prev = current;
|
|
305
|
+
current = next;
|
|
306
|
+
}
|
|
307
|
+
[this.head, this.tail] = [this.tail, this.head];
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
311
|
+
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
312
|
+
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
|
|
313
|
+
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
314
|
+
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
315
|
+
*/
|
|
316
|
+
find(callback) {
|
|
317
|
+
let current = this.head;
|
|
318
|
+
while (current) {
|
|
319
|
+
if (callback(current.val)) {
|
|
320
|
+
return current.val;
|
|
321
|
+
}
|
|
322
|
+
current = current.next;
|
|
531
323
|
}
|
|
532
|
-
|
|
533
|
-
this.tail = this.head;
|
|
534
|
-
this.head = tail;
|
|
535
|
-
return this;
|
|
324
|
+
return null;
|
|
536
325
|
}
|
|
537
326
|
/**
|
|
538
|
-
* The
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
* @param f Function to execute for each element, taking up to three arguments.
|
|
543
|
-
* @param reverse Indicates if the list should be walked in reverse order, default is false
|
|
327
|
+
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
328
|
+
* @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
329
|
+
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
|
330
|
+
* value is not found, it returns -1.
|
|
544
331
|
*/
|
|
545
|
-
|
|
546
|
-
let
|
|
547
|
-
let
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
332
|
+
indexOf(value) {
|
|
333
|
+
let index = 0;
|
|
334
|
+
let current = this.head;
|
|
335
|
+
while (current) {
|
|
336
|
+
if (current.val === value) {
|
|
337
|
+
return index;
|
|
338
|
+
}
|
|
339
|
+
index++;
|
|
340
|
+
current = current.next;
|
|
554
341
|
}
|
|
342
|
+
return -1;
|
|
555
343
|
}
|
|
556
344
|
/**
|
|
557
|
-
* The
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
* @param f Function that produces an node of the new list, taking up to three arguments
|
|
563
|
-
* @param reverse Indicates if the list should be mapped in reverse order, default is false
|
|
564
|
-
*/
|
|
565
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
566
|
-
map(f, reverse = false) {
|
|
567
|
-
const list = new SinglyLinkedList();
|
|
568
|
-
this.forEach((val, index) => list.append(f(val, index, this)), reverse);
|
|
569
|
-
return list;
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* The filter() method creates a new list with all nodes
|
|
573
|
-
* that pass the test implemented by the provided function.
|
|
574
|
-
* ```ts
|
|
575
|
-
* new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
|
|
576
|
-
* ```
|
|
577
|
-
* @param f Function to test each node val in the list. Return true to keep the node
|
|
578
|
-
* @param reverse Indicates if the list should be filtered in reverse order, default is false
|
|
345
|
+
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
346
|
+
* null.
|
|
347
|
+
* @param {T} value - The value parameter is the value that we want to search for in the linked list.
|
|
348
|
+
* @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
|
|
349
|
+
* the specified value is found, the function returns `null`.
|
|
579
350
|
*/
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
list.append(val);
|
|
351
|
+
findNode(value) {
|
|
352
|
+
let current = this.head;
|
|
353
|
+
while (current) {
|
|
354
|
+
if (current.val === value) {
|
|
355
|
+
return current;
|
|
586
356
|
}
|
|
587
|
-
|
|
588
|
-
|
|
357
|
+
current = current.next;
|
|
358
|
+
}
|
|
359
|
+
return null;
|
|
589
360
|
}
|
|
590
361
|
/**
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
* new
|
|
594
|
-
*
|
|
595
|
-
* @
|
|
596
|
-
*
|
|
597
|
-
* @returns The final state of the accumulator
|
|
362
|
+
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
363
|
+
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node that you want to insert the
|
|
364
|
+
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
365
|
+
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
366
|
+
* @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
367
|
+
* inserted before the existing value, and `false` otherwise.
|
|
598
368
|
*/
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
if (start !== undefined) {
|
|
606
|
-
result = start;
|
|
607
|
-
}
|
|
608
|
-
else if (currentElement) {
|
|
609
|
-
result = currentElement.val;
|
|
610
|
-
currentElement = currentElement[nextNode];
|
|
369
|
+
insertBefore(existingValueOrNode, newValue) {
|
|
370
|
+
if (!this.head)
|
|
371
|
+
return false;
|
|
372
|
+
let existingValue;
|
|
373
|
+
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
374
|
+
existingValue = existingValueOrNode.val;
|
|
611
375
|
}
|
|
612
376
|
else {
|
|
613
|
-
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
377
|
+
existingValue = existingValueOrNode;
|
|
378
|
+
}
|
|
379
|
+
if (this.head.val === existingValue) {
|
|
380
|
+
this.unshift(newValue);
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
let current = this.head;
|
|
384
|
+
while (current.next) {
|
|
385
|
+
if (current.next.val === existingValue) {
|
|
386
|
+
const newNode = new SinglyLinkedListNode(newValue);
|
|
387
|
+
newNode.next = current.next;
|
|
388
|
+
current.next = newNode;
|
|
389
|
+
this._length++;
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
current = current.next;
|
|
619
393
|
}
|
|
620
|
-
return
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Convert the linked list to an array
|
|
624
|
-
* ```ts
|
|
625
|
-
* new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
|
|
626
|
-
* ```
|
|
627
|
-
*/
|
|
628
|
-
toArray() {
|
|
629
|
-
return [...this];
|
|
394
|
+
return false;
|
|
630
395
|
}
|
|
631
396
|
/**
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
* new
|
|
635
|
-
*
|
|
636
|
-
* @
|
|
397
|
+
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
398
|
+
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node in the linked list after which
|
|
399
|
+
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
400
|
+
* @param {T} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
401
|
+
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
402
|
+
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
637
403
|
*/
|
|
638
|
-
|
|
639
|
-
|
|
404
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
405
|
+
let existingNode;
|
|
406
|
+
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
407
|
+
existingNode = existingValueOrNode;
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
existingNode = this.findNode(existingValueOrNode);
|
|
411
|
+
}
|
|
412
|
+
if (existingNode) {
|
|
413
|
+
const newNode = new SinglyLinkedListNode(newValue);
|
|
414
|
+
newNode.next = existingNode.next;
|
|
415
|
+
existingNode.next = newNode;
|
|
416
|
+
if (existingNode === this.tail) {
|
|
417
|
+
this.tail = newNode;
|
|
418
|
+
}
|
|
419
|
+
this._length++;
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
return false;
|
|
640
423
|
}
|
|
641
424
|
/**
|
|
642
|
-
* The
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
* for (const val of list) { log(val); } // 1 2 3
|
|
646
|
-
* ```
|
|
425
|
+
* The function counts the number of occurrences of a given value in a linked list.
|
|
426
|
+
* @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
427
|
+
* @returns The count of occurrences of the given value in the linked list.
|
|
647
428
|
*/
|
|
648
|
-
|
|
649
|
-
let
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
429
|
+
countOccurrences(value) {
|
|
430
|
+
let count = 0;
|
|
431
|
+
let current = this.head;
|
|
432
|
+
while (current) {
|
|
433
|
+
if (current.val === value) {
|
|
434
|
+
count++;
|
|
435
|
+
}
|
|
436
|
+
current = current.next;
|
|
653
437
|
}
|
|
654
|
-
|
|
655
|
-
/** Private helper function to reduce duplication of pop() and shift() methods */
|
|
656
|
-
removeFromAnyEnd(node) {
|
|
657
|
-
return node !== null ? this.removeNode(node).val : undefined;
|
|
438
|
+
return count;
|
|
658
439
|
}
|
|
659
440
|
}
|
|
660
441
|
exports.SinglyLinkedList = SinglyLinkedList;
|