data-structure-typed 1.15.2 → 1.16.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/README.md +24 -24
- package/dist/data-structures/heap/heap.d.ts +30 -15
- package/dist/data-structures/heap/heap.js +60 -15
- package/dist/data-structures/heap/max-heap.d.ts +3 -3
- package/dist/data-structures/heap/min-heap.d.ts +3 -3
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
- package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
- package/dist/data-structures/priority-queue/priority-queue.js +28 -22
- 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 +9 -0
- package/dist/data-structures/tree/tree.js +52 -0
- package/dist/data-structures/types/heap.d.ts +0 -4
- package/dist/utils/types/utils.d.ts +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2192 -0
- package/docs/classes/AVLTreeNode.html +574 -0
- package/docs/classes/AaTree.html +150 -0
- package/docs/classes/AbstractEdge.html +269 -0
- package/docs/classes/AbstractGraph.html +927 -0
- package/docs/classes/AbstractVertex.html +215 -0
- package/docs/classes/ArrayDeque.html +417 -0
- package/docs/classes/BST.html +2038 -0
- package/docs/classes/BSTNode.html +570 -0
- package/docs/classes/BTree.html +150 -0
- package/docs/classes/BinaryIndexedTree.html +289 -0
- package/docs/classes/BinaryTree.html +1827 -0
- package/docs/classes/BinaryTreeNode.html +533 -0
- package/docs/classes/Character.html +198 -0
- package/docs/classes/CoordinateMap.html +469 -0
- package/docs/classes/CoordinateSet.html +430 -0
- package/docs/classes/Deque.html +767 -0
- package/docs/classes/DirectedEdge.html +354 -0
- package/docs/classes/DirectedGraph.html +1243 -0
- package/docs/classes/DirectedVertex.html +226 -0
- package/docs/classes/DoublyLinkedList.html +733 -0
- package/docs/classes/DoublyLinkedListNode.html +258 -0
- package/docs/classes/Heap.html +482 -0
- package/docs/classes/HeapItem.html +233 -0
- package/docs/classes/Matrix2D.html +480 -0
- package/docs/classes/MatrixNTI2D.html +218 -0
- package/docs/classes/MaxHeap.html +500 -0
- package/docs/classes/MaxPriorityQueue.html +809 -0
- package/docs/classes/MinHeap.html +501 -0
- package/docs/classes/MinPriorityQueue.html +811 -0
- package/docs/classes/Navigator.html +291 -0
- package/docs/classes/ObjectDeque.html +423 -0
- package/docs/classes/PriorityQueue.html +733 -0
- package/docs/classes/Queue.html +370 -0
- package/docs/classes/RBTree.html +150 -0
- package/docs/classes/SegmentTree.html +345 -0
- package/docs/classes/SegmentTreeNode.html +418 -0
- package/docs/classes/SinglyLinkedList.html +1105 -0
- package/docs/classes/SinglyLinkedListNode.html +375 -0
- package/docs/classes/SplayTree.html +150 -0
- package/docs/classes/Stack.html +346 -0
- package/docs/classes/TreeMultiSet.html +2036 -0
- package/docs/classes/TreeNode.html +236 -0
- package/docs/classes/Trie.html +350 -0
- package/docs/classes/TrieNode.html +258 -0
- package/docs/classes/TwoThreeTree.html +150 -0
- package/docs/classes/UndirectedEdge.html +313 -0
- package/docs/classes/UndirectedGraph.html +1080 -0
- package/docs/classes/UndirectedVertex.html +226 -0
- package/docs/classes/Vector2D.html +783 -0
- package/docs/enums/CP.html +159 -0
- package/docs/enums/FamilyPosition.html +159 -0
- package/docs/enums/LoopType.html +160 -0
- package/docs/index.html +494 -0
- package/docs/interfaces/AVLTreeDeleted.html +161 -0
- package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
- package/docs/interfaces/HeapOptions.html +167 -0
- package/docs/interfaces/IDirectedGraph.html +243 -0
- package/docs/interfaces/IGraph.html +427 -0
- package/docs/interfaces/NavigatorParams.html +197 -0
- package/docs/interfaces/PriorityQueueOptions.html +168 -0
- package/docs/modules.html +218 -0
- package/docs/types/BSTComparator.html +140 -0
- package/docs/types/BSTDeletedResult.html +137 -0
- package/docs/types/BinaryTreeDeleted.html +137 -0
- package/docs/types/BinaryTreeNodeId.html +125 -0
- package/docs/types/BinaryTreeNodePropertyName.html +125 -0
- package/docs/types/DFSOrderPattern.html +125 -0
- package/docs/types/DijkstraResult.html +145 -0
- package/docs/types/Direction.html +125 -0
- package/docs/types/DoublyLinkedListGetBy.html +125 -0
- package/docs/types/NodeOrPropertyName.html +125 -0
- package/docs/types/PriorityQueueComparator.html +145 -0
- package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
- package/docs/types/ResultByProperty.html +130 -0
- package/docs/types/ResultsByProperty.html +130 -0
- package/docs/types/SegmentTreeNodeVal.html +125 -0
- package/docs/types/SpecifyOptional.html +132 -0
- package/docs/types/Thunk.html +133 -0
- package/docs/types/ToThunkFn.html +133 -0
- package/docs/types/TopologicalStatus.html +125 -0
- package/docs/types/TreeMultiSetDeletedResult.html +137 -0
- package/docs/types/TrlAsyncFn.html +138 -0
- package/docs/types/TrlFn.html +138 -0
- package/docs/types/Turning.html +125 -0
- package/docs/types/VertexId.html +125 -0
- package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
- package/notes/note.md +23 -0
- package/package.json +1 -1
- package/.idea/data-structure-typed.iml +0 -14
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -293
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
- package/src/data-structures/binary-tree/bst.ts +0 -497
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- 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 -267
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -958
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -429
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -259
- package/src/data-structures/hash/coordinate-map.ts +0 -74
- package/src/data-structures/hash/coordinate-set.ts +0 -63
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -162
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -13
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
- 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 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/priority-queue.ts +0 -349
- package/src/data-structures/queue/deque.ts +0 -251
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/index.ts +0 -13
- package/src/data-structures/types/navigator.ts +0 -13
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -1
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -2
- package/src/utils/types/index.ts +0 -1
- package/src/utils/types/utils.ts +0 -4
- package/src/utils/utils.ts +0 -78
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
- package/tests/unit/data-structures/graph/index.ts +0 -3
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
|
@@ -1,757 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/* The SinglyLinkedListNode class represents a node in a singly linked list and provides methods for inserting, removing,
|
|
11
|
-
and accessing nodes. */
|
|
12
|
-
export class SinglyLinkedListNode<NodeVal = any> {
|
|
13
|
-
constructor(val: NodeVal, prev?: SinglyLinkedListNode<NodeVal> | null, next?: SinglyLinkedListNode<NodeVal> | null, list?: SinglyLinkedList<NodeVal> | null) {
|
|
14
|
-
this._val = val;
|
|
15
|
-
this._prev = prev || null;
|
|
16
|
-
this._next = next || null;
|
|
17
|
-
this._list = list || null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
protected _val: NodeVal;
|
|
21
|
-
|
|
22
|
-
get val(): NodeVal {
|
|
23
|
-
return this._val;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
set val(value: NodeVal) {
|
|
27
|
-
this._val = value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
protected _prev: SinglyLinkedListNode<NodeVal> | null;
|
|
31
|
-
|
|
32
|
-
get prev(): SinglyLinkedListNode<NodeVal> | null {
|
|
33
|
-
return this._prev;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
set prev(value: SinglyLinkedListNode<NodeVal> | null) {
|
|
37
|
-
this._prev = value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
protected _next: SinglyLinkedListNode<NodeVal> | null
|
|
41
|
-
|
|
42
|
-
get next(): SinglyLinkedListNode<NodeVal> | null {
|
|
43
|
-
return this._next;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
set next(value: SinglyLinkedListNode<NodeVal> | null) {
|
|
47
|
-
this._next = value;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
protected _list: SinglyLinkedList<NodeVal> | null
|
|
51
|
-
|
|
52
|
-
get list(): SinglyLinkedList<NodeVal> | null {
|
|
53
|
-
return this._list;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
set list(value: SinglyLinkedList<NodeVal> | null) {
|
|
57
|
-
this._list = value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
get index() {
|
|
61
|
-
if (!this.list) {
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
return this.list.findIndex((value) => value === this.val);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* The `insertBefore` function inserts a new node with the given value before the current node in a singly linked list.
|
|
69
|
-
* @param {NodeVal} val - The parameter "val" is of type "NodeVal". It represents the value of the node that you want
|
|
70
|
-
* to insert before the current node.
|
|
71
|
-
* @returns The method is returning a SinglyLinkedList<NodeVal>.
|
|
72
|
-
*/
|
|
73
|
-
insertBefore(val: NodeVal): SinglyLinkedList<NodeVal> {
|
|
74
|
-
return this.list !== null
|
|
75
|
-
? this.list.insertBefore(this, val)
|
|
76
|
-
: new SinglyLinkedList(val, this.val);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* The function inserts a new node with the given value after the current node in a singly linked list.
|
|
81
|
-
* @param {NodeVal} val - The parameter `val` is the value of the node that you want to insert after the current node.
|
|
82
|
-
* @returns The method is returning a SinglyLinkedList<NodeVal>.
|
|
83
|
-
*/
|
|
84
|
-
insertAfter(val: NodeVal): SinglyLinkedList<NodeVal> {
|
|
85
|
-
return this.list !== null
|
|
86
|
-
? this.list.insertAfter(this, val)
|
|
87
|
-
: new SinglyLinkedList(this.val, val);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* The `remove()` function removes a node from a singly linked list.
|
|
92
|
-
* @returns The remove() method is returning a SinglyLinkedListNode<NodeVal> object.
|
|
93
|
-
*/
|
|
94
|
-
remove(): SinglyLinkedListNode<NodeVal> {
|
|
95
|
-
if (this.list === null) {
|
|
96
|
-
throw new ReferenceError('Node does not belong to any list');
|
|
97
|
-
}
|
|
98
|
-
return this.list.removeNode(this);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export class SinglyLinkedList<NodeVal = any> {
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* The constructor initializes a linked list with the given arguments as nodes.
|
|
106
|
-
* @param {NodeVal[]} args - args is a rest parameter that allows the constructor to accept an arbitrary number of
|
|
107
|
-
* arguments of type NodeVal.
|
|
108
|
-
*/
|
|
109
|
-
constructor(...args: NodeVal[]) {
|
|
110
|
-
this._head = null;
|
|
111
|
-
this._tail = null;
|
|
112
|
-
this._size = 0;
|
|
113
|
-
|
|
114
|
-
for (let i = 0; i < arguments.length; i++) {
|
|
115
|
-
this.append(args[i]);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
protected _head: SinglyLinkedListNode<NodeVal> | null;
|
|
120
|
-
|
|
121
|
-
get head(): SinglyLinkedListNode<NodeVal> | null {
|
|
122
|
-
return this._head;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
set head(value: SinglyLinkedListNode<NodeVal> | null) {
|
|
126
|
-
this._head = value;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
protected _tail: SinglyLinkedListNode<NodeVal> | null;
|
|
130
|
-
|
|
131
|
-
get tail(): SinglyLinkedListNode<NodeVal> | null {
|
|
132
|
-
return this._tail;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
set tail(value: SinglyLinkedListNode<NodeVal> | null) {
|
|
136
|
-
this._tail = value;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
protected _size: number;
|
|
140
|
-
|
|
141
|
-
get size(): number {
|
|
142
|
-
return this._size;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
set size(value: number) {
|
|
146
|
-
this._size = value;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* The `from` function in TypeScript creates a new SinglyLinkedList instance from an iterable object.
|
|
151
|
-
* @param iterable - The `iterable` parameter is an object that can be iterated over, such as an array or a string. It
|
|
152
|
-
* contains a collection of elements of type `T`.
|
|
153
|
-
* @returns The method is returning a new instance of the SinglyLinkedList class.
|
|
154
|
-
*/
|
|
155
|
-
static from<T>(iterable: Iterable<T>): SinglyLinkedList<T> {
|
|
156
|
-
return new SinglyLinkedList(...iterable);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* The `get` function returns the value of a node at a given index in a data structure.
|
|
161
|
-
* @param {number} index - The index parameter is a number that represents the position of the node in the data
|
|
162
|
-
* structure.
|
|
163
|
-
* @returns The method is returning the value of the node at the specified index if the node exists, otherwise it
|
|
164
|
-
* returns undefined.
|
|
165
|
-
*/
|
|
166
|
-
get(index: number): NodeVal | undefined {
|
|
167
|
-
const node = this.getNode(index);
|
|
168
|
-
return node !== undefined ? node.val : undefined;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* The function `getNode` returns the node at a given index in a singly linked list.
|
|
173
|
-
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
174
|
-
* retrieve from the linked list.
|
|
175
|
-
* @returns a SinglyLinkedListNode<NodeVal> object or undefined.
|
|
176
|
-
*/
|
|
177
|
-
getNode(index: number): SinglyLinkedListNode<NodeVal> | undefined {
|
|
178
|
-
if (this.head === null || index < 0 || index >= this.size) {
|
|
179
|
-
return undefined;
|
|
180
|
-
}
|
|
181
|
-
const asc = index < this.size / 2;
|
|
182
|
-
const stopAt = asc ? index : this.size - index - 1;
|
|
183
|
-
const nextNode = asc ? 'next' : 'prev';
|
|
184
|
-
let currentNode = asc ? this.head : this.tail;
|
|
185
|
-
// TODO after no-non-null-assertion not ensure the logic
|
|
186
|
-
for (let currentIndex = 0; currentIndex < stopAt; currentIndex++) {
|
|
187
|
-
if (currentNode) {
|
|
188
|
-
currentNode = currentNode[nextNode];
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return currentNode || undefined;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* The function `findNodeIndex` searches for a node in a singly linked list that satisfies a given condition and
|
|
196
|
-
* returns its index and the node itself.
|
|
197
|
-
* @param callbackFn - The callbackFn parameter is a function that takes three arguments: data, index, and list. It is
|
|
198
|
-
* used to determine whether a node in the singly linked list matches a certain condition. The function should return a
|
|
199
|
-
* boolean value indicating whether the condition is met for the given node.
|
|
200
|
-
* @returns The function `findNodeIndex` returns an object with two properties: `node` and `index`. The `node` property
|
|
201
|
-
* contains the node that matches the condition specified in the `callbackFn` function, and the `index` property
|
|
202
|
-
* contains the index of that node in the linked list. If no node matches the condition, the function returns
|
|
203
|
-
* `undefined`.
|
|
204
|
-
*/
|
|
205
|
-
findNodeIndex(callbackFn: (
|
|
206
|
-
data: NodeVal,
|
|
207
|
-
index: number,
|
|
208
|
-
list: SinglyLinkedList<NodeVal>,
|
|
209
|
-
) => boolean): ({
|
|
210
|
-
node: SinglyLinkedListNode<NodeVal>,
|
|
211
|
-
index: number,
|
|
212
|
-
}) | undefined {
|
|
213
|
-
let currentIndex = 0;
|
|
214
|
-
let currentNode = this.head;
|
|
215
|
-
while (currentNode) {
|
|
216
|
-
if (callbackFn(currentNode.val, currentIndex, this)) {
|
|
217
|
-
return {
|
|
218
|
-
index: currentIndex,
|
|
219
|
-
node: currentNode,
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
currentNode = currentNode.next;
|
|
223
|
-
currentIndex += 1;
|
|
224
|
-
}
|
|
225
|
-
return undefined;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* The findNode function searches for a node in a singly linked list based on a given callback function.
|
|
230
|
-
* @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
|
|
231
|
-
* value indicating whether the current node matches the desired criteria.
|
|
232
|
-
* @returns The function `findNode` returns a `SinglyLinkedListNode<NodeVal>` if a node satisfying the condition
|
|
233
|
-
* specified by the `callbackFn` is found in the linked list. If no such node is found, it returns `undefined`.
|
|
234
|
-
*/
|
|
235
|
-
findNode(callbackFn: (
|
|
236
|
-
data: NodeVal,
|
|
237
|
-
index: number,
|
|
238
|
-
list: SinglyLinkedList<NodeVal>,
|
|
239
|
-
) => boolean): SinglyLinkedListNode<NodeVal> | undefined {
|
|
240
|
-
const nodeIndex = this.findNodeIndex(callbackFn);
|
|
241
|
-
return nodeIndex !== undefined ? nodeIndex.node : undefined;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* The `find` function in TypeScript searches for a node in a singly linked list based on a given callback function and
|
|
246
|
-
* returns the value of the found node.
|
|
247
|
-
* @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
|
|
248
|
-
* value indicating whether the condition is met for a particular node in the linked list.
|
|
249
|
-
* @returns The method `find` returns the `NodeVal` value of the first node in the linked list that satisfies the
|
|
250
|
-
* condition specified by the `callbackFn` function. If no node satisfies the condition, it returns `undefined`.
|
|
251
|
-
*/
|
|
252
|
-
find(callbackFn: (
|
|
253
|
-
data: NodeVal,
|
|
254
|
-
index: number,
|
|
255
|
-
list: SinglyLinkedList<NodeVal>,
|
|
256
|
-
) => boolean): NodeVal | undefined {
|
|
257
|
-
const nodeIndex = this.findNodeIndex(callbackFn);
|
|
258
|
-
return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* The findIndex function returns the index of the first node in a singly linked list that satisfies a given condition,
|
|
263
|
-
* or -1 if no such node is found.
|
|
264
|
-
* @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
|
|
265
|
-
* value indicating whether the condition is met for a particular node in the singly linked list.
|
|
266
|
-
* @returns The method `findIndex` returns a number.
|
|
267
|
-
*/
|
|
268
|
-
findIndex(callbackFn: (
|
|
269
|
-
data: NodeVal,
|
|
270
|
-
index: number,
|
|
271
|
-
list: SinglyLinkedList<NodeVal>,
|
|
272
|
-
) => boolean): number {
|
|
273
|
-
const nodeIndex = this.findNodeIndex(callbackFn);
|
|
274
|
-
return nodeIndex !== undefined ? nodeIndex.index : -1;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/* The above code is a comment in TypeScript. It is using the triple hash symbol ( */
|
|
278
|
-
append(...args: NodeVal[]): SinglyLinkedList<NodeVal> {
|
|
279
|
-
for (const val of args) {
|
|
280
|
-
const node = new SinglyLinkedListNode(val, this.tail, null, this);
|
|
281
|
-
if (this.head === null) {
|
|
282
|
-
this.head = node;
|
|
283
|
-
}
|
|
284
|
-
if (this.tail !== null) {
|
|
285
|
-
this.tail.next = node;
|
|
286
|
-
}
|
|
287
|
-
this.tail = node;
|
|
288
|
-
this.size += 1;
|
|
289
|
-
}
|
|
290
|
-
return this;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* The push function appends multiple NodeVal objects to a data structure and returns the new size of the data
|
|
295
|
-
* structure.
|
|
296
|
-
* @param {NodeVal[]} args - args is a rest parameter of type NodeVal[]. It allows the function to accept any number
|
|
297
|
-
* of arguments of type NodeVal.
|
|
298
|
-
* @returns The size of the data structure after the nodes are appended.
|
|
299
|
-
*/
|
|
300
|
-
push(...args: NodeVal[]): number {
|
|
301
|
-
this.append(...args);
|
|
302
|
-
return this.size;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* The `prepend` function adds new nodes to the beginning of a singly linked list.
|
|
307
|
-
* @param {NodeVal[]} args - An array of NodeVal objects.
|
|
308
|
-
* @returns The `prepend` method is returning the updated `SinglyLinkedList` object.
|
|
309
|
-
*/
|
|
310
|
-
prepend(...args: NodeVal[]): SinglyLinkedList<NodeVal> {
|
|
311
|
-
const reverseArgs = Array.from(args).reverse();
|
|
312
|
-
for (const val of reverseArgs) {
|
|
313
|
-
const node = new SinglyLinkedListNode(val, null, this.head, this);
|
|
314
|
-
if (this.tail === null) {
|
|
315
|
-
this.tail = node;
|
|
316
|
-
}
|
|
317
|
-
if (this.head !== null) {
|
|
318
|
-
this.head.prev = node;
|
|
319
|
-
}
|
|
320
|
-
this.head = node;
|
|
321
|
-
this.size += 1;
|
|
322
|
-
}
|
|
323
|
-
return this;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
328
|
-
* @param {number} index - The index parameter is a number that represents the position at which the new node should be
|
|
329
|
-
* inserted in the linked list.
|
|
330
|
-
* @param {NodeVal} val - The `val` parameter represents the value of the node that you want to insert into the linked
|
|
331
|
-
* list.
|
|
332
|
-
* @returns The method `insertAt` returns the updated `SinglyLinkedList` object.
|
|
333
|
-
*/
|
|
334
|
-
insertAt(index: number, val: NodeVal): SinglyLinkedList<NodeVal> {
|
|
335
|
-
if (this.head === null) {
|
|
336
|
-
return this.append(val);
|
|
337
|
-
}
|
|
338
|
-
if (index <= 0) {
|
|
339
|
-
return this.prepend(val);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
let currentNode = this.head;
|
|
343
|
-
let currentIndex = 0;
|
|
344
|
-
while (currentIndex < index - 1 && currentNode.next !== null) {
|
|
345
|
-
currentIndex += 1;
|
|
346
|
-
currentNode = currentNode.next;
|
|
347
|
-
}
|
|
348
|
-
currentNode.insertAfter(val);
|
|
349
|
-
return this;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* The removeNode function removes a node from a singly linked list and updates the head, tail, and size properties
|
|
354
|
-
* accordingly.
|
|
355
|
-
* @param node - The `node` parameter is of type `SinglyLinkedListNode<NodeVal>`, which represents a node in a singly
|
|
356
|
-
* linked list.
|
|
357
|
-
* @returns the removed node.
|
|
358
|
-
*/
|
|
359
|
-
removeNode(node: SinglyLinkedListNode<NodeVal>): SinglyLinkedListNode<NodeVal> {
|
|
360
|
-
if (node.list !== this) {
|
|
361
|
-
throw new ReferenceError('Node does not belong to this list');
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
if (node.prev !== null) {
|
|
365
|
-
node.prev.next = node.next;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (node.next !== null) {
|
|
369
|
-
node.next.prev = node.prev;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
if (this.head === node) {
|
|
373
|
-
this.head = node.next;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (this.tail === node) {
|
|
377
|
-
this.tail = node.prev;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
this.size -= 1;
|
|
381
|
-
node.next = null;
|
|
382
|
-
node.prev = null;
|
|
383
|
-
node.list = null;
|
|
384
|
-
return node;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* The `removeAt` function removes a node at a specified index from a singly linked list.
|
|
389
|
-
* @param {number} index - The index parameter is a number that represents the position of the node to be removed in
|
|
390
|
-
* the singly linked list.
|
|
391
|
-
* @returns The method `removeAt` returns a `SinglyLinkedListNode<NodeVal>` if the node at the specified index is
|
|
392
|
-
* found and removed successfully. If the node is not found, it returns `undefined`.
|
|
393
|
-
*/
|
|
394
|
-
removeAt(index: number): SinglyLinkedListNode<NodeVal> | undefined {
|
|
395
|
-
const node = this.getNode(index);
|
|
396
|
-
return node !== undefined ? this.removeNode(node) : undefined;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* The `insertBefore` function inserts a new node with a given value before a specified reference node in a singly
|
|
401
|
-
* linked list.
|
|
402
|
-
* @param referenceNode - The referenceNode parameter is the node in the linked list before which the new node will be
|
|
403
|
-
* inserted.
|
|
404
|
-
* @param {NodeVal} val - The value of the new node that will be inserted before the reference node.
|
|
405
|
-
* @returns The method is returning the updated SinglyLinkedList object.
|
|
406
|
-
*/
|
|
407
|
-
insertBefore(
|
|
408
|
-
referenceNode: SinglyLinkedListNode<NodeVal>,
|
|
409
|
-
val: NodeVal,
|
|
410
|
-
): SinglyLinkedList<NodeVal> {
|
|
411
|
-
const node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
|
|
412
|
-
if (referenceNode.prev === null) {
|
|
413
|
-
this.head = node;
|
|
414
|
-
}
|
|
415
|
-
if (referenceNode.prev !== null) {
|
|
416
|
-
referenceNode.prev.next = node;
|
|
417
|
-
}
|
|
418
|
-
referenceNode.prev = node;
|
|
419
|
-
this.size += 1;
|
|
420
|
-
return this;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* The `sort` function uses the quicksort algorithm to sort the elements of a singly linked list based on a provided
|
|
425
|
-
* comparison function.
|
|
426
|
-
* @param start - The `start` parameter is the starting node of the sublist that needs to be sorted.
|
|
427
|
-
* @param end - The `end` parameter is a reference to the last node in the linked list. It is used as the pivot element
|
|
428
|
-
* for the quicksort algorithm.
|
|
429
|
-
* @returns The `sort` method is returning the sorted `SinglyLinkedList` object.
|
|
430
|
-
*/
|
|
431
|
-
sort(compare: (a: NodeVal, b: NodeVal) => boolean): SinglyLinkedList<NodeVal> {
|
|
432
|
-
if (this.head === null || this.tail === null) {
|
|
433
|
-
return this;
|
|
434
|
-
}
|
|
435
|
-
if (this.size < 2) {
|
|
436
|
-
return this;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
const quicksort = (
|
|
440
|
-
start: SinglyLinkedListNode<NodeVal>,
|
|
441
|
-
end: SinglyLinkedListNode<NodeVal>,
|
|
442
|
-
) => {
|
|
443
|
-
if (start === end) {
|
|
444
|
-
return;
|
|
445
|
-
}
|
|
446
|
-
const pivotData = end.val;
|
|
447
|
-
let current: SinglyLinkedListNode | null = start;
|
|
448
|
-
let split: SinglyLinkedListNode = start;
|
|
449
|
-
while (current && current !== end) {
|
|
450
|
-
const sort = compare(current.val, pivotData);
|
|
451
|
-
if (sort) {
|
|
452
|
-
if (current !== split) {
|
|
453
|
-
const temp = split.val;
|
|
454
|
-
split.val = current.val;
|
|
455
|
-
current.val = temp;
|
|
456
|
-
}
|
|
457
|
-
// TODO after no-non-null-assertion not ensure the logic
|
|
458
|
-
if (split.next) {
|
|
459
|
-
split = split.next;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
current = current.next;
|
|
463
|
-
}
|
|
464
|
-
end.val = split.val;
|
|
465
|
-
split.val = pivotData;
|
|
466
|
-
|
|
467
|
-
if (start.next === end.prev) {
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
if (split.prev && split !== start) {
|
|
472
|
-
quicksort(start, split.prev);
|
|
473
|
-
}
|
|
474
|
-
if (split.next && split !== end) {
|
|
475
|
-
quicksort(split.next, end);
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
|
|
479
|
-
quicksort(this.head, this.tail);
|
|
480
|
-
return this;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* The `insertAfter` function inserts a new node with a given value after a specified reference node in a singly linked
|
|
485
|
-
* list.
|
|
486
|
-
* @param referenceNode - The referenceNode parameter is the node after which the new node will be inserted.
|
|
487
|
-
* @param {NodeVal} val - The value of the new node that will be inserted after the reference node.
|
|
488
|
-
* @returns The `insertAfter` method is returning the updated `SinglyLinkedList` object.
|
|
489
|
-
*/
|
|
490
|
-
insertAfter(
|
|
491
|
-
referenceNode: SinglyLinkedListNode<NodeVal>,
|
|
492
|
-
val: NodeVal,
|
|
493
|
-
): SinglyLinkedList<NodeVal> {
|
|
494
|
-
const node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
|
|
495
|
-
if (referenceNode.next === null) {
|
|
496
|
-
this.tail = node;
|
|
497
|
-
}
|
|
498
|
-
if (referenceNode.next !== null) {
|
|
499
|
-
referenceNode.next.prev = node;
|
|
500
|
-
}
|
|
501
|
-
referenceNode.next = node;
|
|
502
|
-
this.size += 1;
|
|
503
|
-
return this;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
/**
|
|
507
|
-
* The `shift()` function removes and returns the first element from a linked list.
|
|
508
|
-
* @returns The `shift()` method is returning a value of type `NodeVal` or `undefined`.
|
|
509
|
-
*/
|
|
510
|
-
shift(): NodeVal | undefined {
|
|
511
|
-
return this.removeFromAnyEnd(this.head);
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* The `pop()` function removes and returns the last element from a linked list.
|
|
516
|
-
* @returns The `pop()` method is returning a value of type `NodeVal` or `undefined`.
|
|
517
|
-
*/
|
|
518
|
-
pop(): NodeVal | undefined {
|
|
519
|
-
return this.removeFromAnyEnd(this.tail);
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
/**
|
|
523
|
-
* The merge function merges two singly linked lists by updating the next and prev pointers, as well as the head, tail,
|
|
524
|
-
* and size properties.
|
|
525
|
-
* @param list - The parameter "list" is a SinglyLinkedList object that contains nodes with data of type NodeVal.
|
|
526
|
-
*/
|
|
527
|
-
merge(list: SinglyLinkedList<NodeVal>): void {
|
|
528
|
-
if (this.tail !== null) {
|
|
529
|
-
this.tail.next = list.head;
|
|
530
|
-
}
|
|
531
|
-
if (list.head !== null) {
|
|
532
|
-
list.head.prev = this.tail;
|
|
533
|
-
}
|
|
534
|
-
this.head = this.head || list.head;
|
|
535
|
-
this.tail = list.tail || this.tail;
|
|
536
|
-
this.size += list.size;
|
|
537
|
-
list.size = this.size;
|
|
538
|
-
list.head = this.head;
|
|
539
|
-
list.tail = this.tail;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
/**
|
|
543
|
-
* The clear() function resets the linked list by setting the head and tail to null and the size to 0.
|
|
544
|
-
* @returns The "this" object is being returned.
|
|
545
|
-
*/
|
|
546
|
-
clear() {
|
|
547
|
-
this.head = null;
|
|
548
|
-
this.tail = null;
|
|
549
|
-
this.size = 0;
|
|
550
|
-
return this;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* The `slice` function returns a new SinglyLinkedList containing a portion of the original list, starting from the
|
|
555
|
-
* specified index and ending at the optional end index.
|
|
556
|
-
* @param {number} start - The `start` parameter is a number that represents the index at which to start slicing the
|
|
557
|
-
* linked list.
|
|
558
|
-
* @param {number} [end] - The `end` parameter is an optional number that specifies the index at which to end the
|
|
559
|
-
* slicing. If no value is provided for `end`, or if the provided value is less than the `start` index, the slicing
|
|
560
|
-
* will continue until the end of the list.
|
|
561
|
-
* @returns a new SinglyLinkedList containing the sliced elements from the original list.
|
|
562
|
-
*/
|
|
563
|
-
slice(start: number, end?: number): SinglyLinkedList<NodeVal | {}> {
|
|
564
|
-
const list = new SinglyLinkedList();
|
|
565
|
-
let finish = end;
|
|
566
|
-
|
|
567
|
-
if (this.head === null || this.tail === null) {
|
|
568
|
-
return list;
|
|
569
|
-
}
|
|
570
|
-
if (finish === undefined || finish < start) {
|
|
571
|
-
finish = this.size;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
let head: SinglyLinkedListNode<NodeVal> | null | undefined = this.getNode(start);
|
|
575
|
-
for (let i = 0; i < finish - start && head !== null && head !== undefined; i++) {
|
|
576
|
-
list.append(head.val);
|
|
577
|
-
head = head.next;
|
|
578
|
-
}
|
|
579
|
-
return list;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
/**
|
|
583
|
-
* The reverse() function reverses the order of nodes in a singly linked list.
|
|
584
|
-
* @returns The reverse() method is returning the reversed SinglyLinkedList.
|
|
585
|
-
*/
|
|
586
|
-
reverse(): SinglyLinkedList<NodeVal> {
|
|
587
|
-
let currentNode = this.head;
|
|
588
|
-
while (currentNode) {
|
|
589
|
-
const next = currentNode.next;
|
|
590
|
-
currentNode.next = currentNode.prev;
|
|
591
|
-
currentNode.prev = next;
|
|
592
|
-
currentNode = currentNode.prev;
|
|
593
|
-
}
|
|
594
|
-
const tail = this.tail;
|
|
595
|
-
this.tail = this.head;
|
|
596
|
-
this.head = tail;
|
|
597
|
-
return this;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* The `forEach` function iterates over a singly linked list and applies a callback function to each node, either in
|
|
602
|
-
* forward or reverse order.
|
|
603
|
-
* @param callbackFn - A callback function that will be called for each element in the linked list. It takes three
|
|
604
|
-
* parameters:
|
|
605
|
-
* @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
|
|
606
|
-
* to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
|
|
607
|
-
* (default), the iteration will start from the head and move towards the tail.
|
|
608
|
-
*/
|
|
609
|
-
forEach(callbackFn: (
|
|
610
|
-
data: any,
|
|
611
|
-
index: number,
|
|
612
|
-
list: SinglyLinkedList<NodeVal>,
|
|
613
|
-
) => any, reverse = false): void {
|
|
614
|
-
let currentIndex = reverse ? this.size - 1 : 0;
|
|
615
|
-
let currentNode = reverse ? this.tail : this.head;
|
|
616
|
-
const modifier = reverse ? -1 : 1;
|
|
617
|
-
const nextNode = reverse ? 'prev' : 'next';
|
|
618
|
-
while (currentNode) {
|
|
619
|
-
callbackFn(currentNode.val, currentIndex, this);
|
|
620
|
-
currentNode = currentNode[nextNode];
|
|
621
|
-
currentIndex += modifier;
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
/**
|
|
626
|
-
* The map function takes a callback function and applies it to each element in the linked list, returning a new linked
|
|
627
|
-
* list with the results.
|
|
628
|
-
* @param callbackFn - A callback function that will be applied to each element in the linked list. It takes three
|
|
629
|
-
* parameters:
|
|
630
|
-
* @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the mapping should be
|
|
631
|
-
* done in reverse order or not. If `reverse` is set to `true`, the mapping will be done in reverse order. If `reverse`
|
|
632
|
-
* is set to `false` or not provided, the mapping will be
|
|
633
|
-
* @returns The `map` function is returning a new `SinglyLinkedList` object.
|
|
634
|
-
*/
|
|
635
|
-
map(callbackFn: (
|
|
636
|
-
data: any,
|
|
637
|
-
index: number,
|
|
638
|
-
list: SinglyLinkedList<NodeVal>,
|
|
639
|
-
) => any, reverse = false): SinglyLinkedList<NodeVal | {}> {
|
|
640
|
-
const list = new SinglyLinkedList();
|
|
641
|
-
this.forEach((val, index) => list.append(callbackFn(val, index, this)), reverse);
|
|
642
|
-
return list;
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* The `filter` function filters the elements of a singly linked list based on a given callback function.
|
|
647
|
-
* @param callbackFn - A callback function that takes three parameters: data, index, and list. It should return a
|
|
648
|
-
* boolean value indicating whether the current element should be included in the filtered list or not.
|
|
649
|
-
* @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the filtered list should
|
|
650
|
-
* be reversed or not. If `reverse` is set to `true`, the filtered list will be in reverse order. If `reverse` is set
|
|
651
|
-
* to `false` or not provided, the filtered list will be in
|
|
652
|
-
* @returns The `filter` method is returning a new `SinglyLinkedList` object.
|
|
653
|
-
*/
|
|
654
|
-
filter(callbackFn: (
|
|
655
|
-
data: NodeVal,
|
|
656
|
-
index: number,
|
|
657
|
-
list: SinglyLinkedList<NodeVal>,
|
|
658
|
-
) => boolean, reverse = false): SinglyLinkedList<NodeVal | {}> {
|
|
659
|
-
const list = new SinglyLinkedList();
|
|
660
|
-
this.forEach((val, index) => {
|
|
661
|
-
if (callbackFn(val, index, this)) {
|
|
662
|
-
list.append(val);
|
|
663
|
-
}
|
|
664
|
-
}, reverse);
|
|
665
|
-
return list;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
/**
|
|
669
|
-
* The `reduce` function iterates over a singly linked list and applies a callback function to each element,
|
|
670
|
-
* accumulating a single value.
|
|
671
|
-
* @param callbackFn - A callback function that will be called for each element in the linked list. It takes four
|
|
672
|
-
* parameters:
|
|
673
|
-
* @param {any} [start] - The `start` parameter is an optional initial value for the accumulator. If provided, the
|
|
674
|
-
* `reduce` function will start accumulating from this value. If not provided, the `reduce` function will use the value
|
|
675
|
-
* of the first element in the linked list as the initial value.
|
|
676
|
-
* @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
|
|
677
|
-
* to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
|
|
678
|
-
* (default), the iteration will start from the head and move towards the tail.
|
|
679
|
-
* @returns The `reduce` method returns the accumulated value after applying the callback function to each element in
|
|
680
|
-
* the linked list.
|
|
681
|
-
*/
|
|
682
|
-
reduce(
|
|
683
|
-
callbackFn: (
|
|
684
|
-
accumulator: any,
|
|
685
|
-
currentNode: NodeVal,
|
|
686
|
-
index: number,
|
|
687
|
-
list: SinglyLinkedList<NodeVal>,
|
|
688
|
-
) => any,
|
|
689
|
-
start?: any,
|
|
690
|
-
reverse = false,
|
|
691
|
-
): any {
|
|
692
|
-
let currentIndex = reverse ? this.size - 1 : 0;
|
|
693
|
-
const modifier = reverse ? -1 : 1;
|
|
694
|
-
const nextNode = reverse ? 'prev' : 'next';
|
|
695
|
-
let currentElement = reverse ? this.tail : this.head;
|
|
696
|
-
let result;
|
|
697
|
-
|
|
698
|
-
if (start !== undefined) {
|
|
699
|
-
result = start;
|
|
700
|
-
} else if (currentElement) {
|
|
701
|
-
result = currentElement.val;
|
|
702
|
-
currentElement = currentElement[nextNode];
|
|
703
|
-
} else {
|
|
704
|
-
throw new TypeError('Reduce of empty LinkedList with no initial value');
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
while (currentElement) {
|
|
708
|
-
result = callbackFn(result, currentElement.val, currentIndex, this);
|
|
709
|
-
currentIndex += modifier;
|
|
710
|
-
currentElement = currentElement[nextNode];
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
return result;
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
/**
|
|
717
|
-
* The toArray() function converts a NodeVal object into an array of NodeVal objects.
|
|
718
|
-
* @returns An array of NodeVal objects.
|
|
719
|
-
*/
|
|
720
|
-
toArray(): NodeVal[] {
|
|
721
|
-
return [...this];
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
/**
|
|
725
|
-
* The `toString` function takes an optional separator and returns a string representation of an array, with each
|
|
726
|
-
* element separated by the specified separator.
|
|
727
|
-
* @param [separator= ] - The separator parameter is a string that specifies the character(s) to be used as a separator
|
|
728
|
-
* between each element in the array when converting it to a string. By default, the separator is set to a space
|
|
729
|
-
* character (' ').
|
|
730
|
-
* @returns The toString method is being returned as a string.
|
|
731
|
-
*/
|
|
732
|
-
toString(separator = ' '): string {
|
|
733
|
-
return this.reduce((s, val) => `${s}${separator}${val}`);
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
/**
|
|
737
|
-
* The function is an iterator that returns the values of each node in a linked list.
|
|
738
|
-
*/
|
|
739
|
-
public* [Symbol.iterator](): IterableIterator<NodeVal> {
|
|
740
|
-
let element = this.head;
|
|
741
|
-
|
|
742
|
-
while (element !== null) {
|
|
743
|
-
yield element.val;
|
|
744
|
-
element = element.next;
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
/**
|
|
749
|
-
* The function removes a node from either end of a singly linked list and returns its value.
|
|
750
|
-
* @param {SinglyLinkedListNode<NodeVal> | null} node - The `node` parameter is a reference to a node in a singly
|
|
751
|
-
* linked list. It can be either a `SinglyLinkedListNode` object or `null`.
|
|
752
|
-
* @returns The value of the removed node if the node is not null, otherwise undefined.
|
|
753
|
-
*/
|
|
754
|
-
protected removeFromAnyEnd(node: SinglyLinkedListNode<NodeVal> | null) {
|
|
755
|
-
return node !== null ? this.removeNode(node).val : undefined;
|
|
756
|
-
}
|
|
757
|
-
}
|