data-structure-typed 0.8.18 → 1.12.9
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/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/.idea/modules.xml +1 -1
- package/README.md +298 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
- package/dist/data-structures/binary-tree/avl-tree.js +150 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
- package/dist/data-structures/binary-tree/binary-tree.js +747 -369
- package/dist/data-structures/binary-tree/bst.d.ts +20 -8
- package/dist/data-structures/binary-tree/bst.js +164 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
- package/dist/data-structures/binary-tree/segment-tree.js +95 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
- package/dist/data-structures/graph/abstract-graph.js +712 -323
- package/dist/data-structures/graph/directed-graph.d.ts +114 -12
- package/dist/data-structures/graph/directed-graph.js +372 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
- package/dist/data-structures/graph/undirected-graph.js +233 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
- package/dist/data-structures/hash/coordinate-map.js +70 -20
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +58 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +26 -37
- package/dist/data-structures/heap/heap.js +56 -60
- package/dist/data-structures/heap/max-heap.d.ts +8 -2
- package/dist/data-structures/heap/max-heap.js +32 -9
- package/dist/data-structures/heap/min-heap.d.ts +9 -2
- package/dist/data-structures/heap/min-heap.js +33 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
- package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +19 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
- package/dist/data-structures/matrix/matrix2d.js +158 -61
- package/dist/data-structures/matrix/navigator.d.ts +34 -16
- package/dist/data-structures/matrix/navigator.js +65 -18
- package/dist/data-structures/matrix/vector2d.d.ts +153 -29
- package/dist/data-structures/matrix/vector2d.js +249 -102
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
- package/dist/data-structures/priority-queue/priority-queue.js +285 -116
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +151 -56
- package/dist/data-structures/queue/queue.d.ts +34 -37
- package/dist/data-structures/queue/queue.js +59 -61
- package/dist/data-structures/stack/stack.d.ts +29 -35
- package/dist/data-structures/stack/stack.js +51 -56
- package/dist/data-structures/trie/trie.d.ts +36 -6
- package/dist/data-structures/trie/trie.js +256 -83
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/utils/trampoline.d.ts +14 -0
- package/dist/utils/trampoline.js +130 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{types → utils}/types/utils.d.ts +15 -1
- package/dist/{types → utils/types}/utils.js +21 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
- package/dist/utils/utils.js +651 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -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 +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +33 -47
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +58 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
- package/src/data-structures/binary-tree/binary-tree.ts +460 -145
- package/src/data-structures/binary-tree/bst.ts +31 -25
- 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/segment-tree.ts +25 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +224 -108
- 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 +132 -26
- package/src/data-structures/graph/undirected-graph.ts +78 -11
- package/src/data-structures/hash/coordinate-map.ts +33 -1
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +27 -41
- package/src/data-structures/heap/max-heap.ts +8 -2
- package/src/data-structures/heap/min-heap.ts +9 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
- package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +90 -10
- package/src/data-structures/matrix/navigator.ts +34 -14
- package/src/data-structures/matrix/vector2d.ts +187 -63
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/priority-queue.ts +200 -78
- package/src/data-structures/queue/deque.ts +71 -2
- package/src/data-structures/queue/queue.ts +37 -40
- package/src/data-structures/stack/stack.ts +32 -38
- package/src/data-structures/trie/trie.ts +83 -14
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/trampoline.ts +51 -0
- package/src/utils/types/index.ts +1 -0
- package/src/{types → utils/types}/utils.ts +27 -5
- package/src/{utils.ts → utils/utils.ts} +41 -131
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- package/tsconfig.json +9 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/data-structures/trampoline.js +0 -52
- 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/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/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.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/index.d.ts +0 -9
- 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/linked-list/skip-linked-list.d.ts +0 -1
- 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/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/data-structures/trampoline.ts +0 -91
- package/src/types/index.ts +0 -1
- /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
- /package/dist/{types → utils}/types/index.d.ts +0 -0
- /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
|
@@ -1,4 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
7
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
8
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
9
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
10
|
+
function step(op) {
|
|
11
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
12
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
13
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
14
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
15
|
+
switch (op[0]) {
|
|
16
|
+
case 0: case 1: t = op; break;
|
|
17
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
18
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
19
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
20
|
+
default:
|
|
21
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
22
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
23
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
24
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
25
|
+
if (t[2]) _.ops.pop();
|
|
26
|
+
_.trys.pop(); continue;
|
|
27
|
+
}
|
|
28
|
+
op = body.call(thisArg, _);
|
|
29
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
30
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
34
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
35
|
+
if (!m) return o;
|
|
36
|
+
var i = m.call(o), r, ar = [], e;
|
|
37
|
+
try {
|
|
38
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
39
|
+
}
|
|
40
|
+
catch (error) { e = { error: error }; }
|
|
41
|
+
finally {
|
|
42
|
+
try {
|
|
43
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
44
|
+
}
|
|
45
|
+
finally { if (e) throw e.error; }
|
|
46
|
+
}
|
|
47
|
+
return ar;
|
|
48
|
+
};
|
|
49
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
50
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
51
|
+
if (ar || !(i in from)) {
|
|
52
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
53
|
+
ar[i] = from[i];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
57
|
+
};
|
|
58
|
+
var __values = (this && this.__values) || function(o) {
|
|
59
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
60
|
+
if (m) return m.call(o);
|
|
61
|
+
if (o && typeof o.length === "number") return {
|
|
62
|
+
next: function () {
|
|
63
|
+
if (o && i >= o.length) o = void 0;
|
|
64
|
+
return { value: o && o[i++], done: !o };
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
68
|
+
};
|
|
2
69
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
70
|
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
4
71
|
/**
|
|
@@ -7,8 +74,8 @@ exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
|
7
74
|
* const node = new SinglyLinkedListNode(1, null, null, null);
|
|
8
75
|
* ```
|
|
9
76
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
77
|
+
var SinglyLinkedListNode = /** @class */ (function () {
|
|
78
|
+
function SinglyLinkedListNode(
|
|
12
79
|
/** Data stored on the node */
|
|
13
80
|
val,
|
|
14
81
|
/** The previous node in the list */
|
|
@@ -22,27 +89,36 @@ class SinglyLinkedListNode {
|
|
|
22
89
|
this.next = next;
|
|
23
90
|
this.list = list;
|
|
24
91
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
92
|
+
Object.defineProperty(SinglyLinkedListNode.prototype, "value", {
|
|
93
|
+
/**
|
|
94
|
+
* Alias to .val
|
|
95
|
+
* ```ts
|
|
96
|
+
* new LinkedList(1, 2, 3).head.value; // 1
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
get: function () {
|
|
100
|
+
return this.val;
|
|
101
|
+
},
|
|
102
|
+
enumerable: false,
|
|
103
|
+
configurable: true
|
|
104
|
+
});
|
|
105
|
+
Object.defineProperty(SinglyLinkedListNode.prototype, "index", {
|
|
106
|
+
/**
|
|
107
|
+
* Get the index of this node
|
|
108
|
+
* ```ts
|
|
109
|
+
* new LinkedList(1, 2, 3).head.index; // 0
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
get: function () {
|
|
113
|
+
var _this = this;
|
|
114
|
+
if (!this.list) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
return this.list.findIndex(function (value) { return value === _this.value; });
|
|
118
|
+
},
|
|
119
|
+
enumerable: false,
|
|
120
|
+
configurable: true
|
|
121
|
+
});
|
|
46
122
|
/**
|
|
47
123
|
* Insert a new node before this one
|
|
48
124
|
* ```ts
|
|
@@ -50,11 +126,11 @@ class SinglyLinkedListNode {
|
|
|
50
126
|
* ```
|
|
51
127
|
* @param val Data to save in the node
|
|
52
128
|
*/
|
|
53
|
-
insertBefore(val) {
|
|
129
|
+
SinglyLinkedListNode.prototype.insertBefore = function (val) {
|
|
54
130
|
return this.list !== null
|
|
55
131
|
? this.list.insertBefore(this, val)
|
|
56
132
|
: new SinglyLinkedList(val, this.val);
|
|
57
|
-
}
|
|
133
|
+
};
|
|
58
134
|
/**
|
|
59
135
|
* Insert new val after this node
|
|
60
136
|
* ```ts
|
|
@@ -62,24 +138,25 @@ class SinglyLinkedListNode {
|
|
|
62
138
|
* ```
|
|
63
139
|
* @param val Data to be saved in the node
|
|
64
140
|
*/
|
|
65
|
-
insertAfter(val) {
|
|
141
|
+
SinglyLinkedListNode.prototype.insertAfter = function (val) {
|
|
66
142
|
return this.list !== null
|
|
67
143
|
? this.list.insertAfter(this, val)
|
|
68
144
|
: new SinglyLinkedList(this.val, val);
|
|
69
|
-
}
|
|
145
|
+
};
|
|
70
146
|
/**
|
|
71
147
|
* Remove this node
|
|
72
148
|
* ```ts
|
|
73
149
|
* new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
|
|
74
150
|
* ```
|
|
75
151
|
*/
|
|
76
|
-
remove() {
|
|
152
|
+
SinglyLinkedListNode.prototype.remove = function () {
|
|
77
153
|
if (this.list === null) {
|
|
78
154
|
throw new ReferenceError('Node does not belong to any list');
|
|
79
155
|
}
|
|
80
156
|
return this.list.removeNode(this);
|
|
81
|
-
}
|
|
82
|
-
|
|
157
|
+
};
|
|
158
|
+
return SinglyLinkedListNode;
|
|
159
|
+
}());
|
|
83
160
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
84
161
|
/**
|
|
85
162
|
* A doubly linked list
|
|
@@ -88,13 +165,29 @@ exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
|
88
165
|
* const listFromArray = LinkedList.from([1, 2, 3]);
|
|
89
166
|
* ```
|
|
90
167
|
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
168
|
+
var SinglyLinkedList = /** @class */ (function () {
|
|
169
|
+
function SinglyLinkedList() {
|
|
170
|
+
var args = [];
|
|
171
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
172
|
+
args[_i] = arguments[_i];
|
|
173
|
+
}
|
|
174
|
+
this.head = null;
|
|
175
|
+
this.tail = null;
|
|
176
|
+
this.size = 0;
|
|
177
|
+
for (var i = 0; i < arguments.length; i++) {
|
|
178
|
+
this.append(args[i]);
|
|
179
|
+
}
|
|
97
180
|
}
|
|
181
|
+
Object.defineProperty(SinglyLinkedList.prototype, "length", {
|
|
182
|
+
/**
|
|
183
|
+
* The length of the list
|
|
184
|
+
*/
|
|
185
|
+
get: function () {
|
|
186
|
+
return this.size;
|
|
187
|
+
},
|
|
188
|
+
enumerable: false,
|
|
189
|
+
configurable: true
|
|
190
|
+
});
|
|
98
191
|
/**
|
|
99
192
|
* Convert any iterable to a new linked list
|
|
100
193
|
* ```javascript
|
|
@@ -103,17 +196,9 @@ class SinglyLinkedList {
|
|
|
103
196
|
* ```
|
|
104
197
|
* @param iterable Any iterable datatype like Array or Map
|
|
105
198
|
*/
|
|
106
|
-
|
|
107
|
-
return new SinglyLinkedList(
|
|
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
|
-
}
|
|
116
|
-
}
|
|
199
|
+
SinglyLinkedList.from = function (iterable) {
|
|
200
|
+
return new (SinglyLinkedList.bind.apply(SinglyLinkedList, __spreadArray([void 0], __read(iterable), false)))();
|
|
201
|
+
};
|
|
117
202
|
/**
|
|
118
203
|
* Get the node val at a specified index, zero based
|
|
119
204
|
* ```ts
|
|
@@ -121,10 +206,10 @@ class SinglyLinkedList {
|
|
|
121
206
|
* ```
|
|
122
207
|
* @param index to retrieve val at
|
|
123
208
|
*/
|
|
124
|
-
get(index) {
|
|
125
|
-
|
|
209
|
+
SinglyLinkedList.prototype.get = function (index) {
|
|
210
|
+
var node = this.getNode(index);
|
|
126
211
|
return node !== undefined ? node.val : undefined;
|
|
127
|
-
}
|
|
212
|
+
};
|
|
128
213
|
/**
|
|
129
214
|
* Get the node at index, zero based
|
|
130
215
|
* ```ts
|
|
@@ -132,22 +217,22 @@ class SinglyLinkedList {
|
|
|
132
217
|
* // { prev: null, val: 1, next: SinglyLinkedListNode }
|
|
133
218
|
* ```
|
|
134
219
|
*/
|
|
135
|
-
getNode(index) {
|
|
220
|
+
SinglyLinkedList.prototype.getNode = function (index) {
|
|
136
221
|
if (this.head === null || index < 0 || index >= this.length) {
|
|
137
222
|
return undefined;
|
|
138
223
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
224
|
+
var asc = index < this.length / 2;
|
|
225
|
+
var stopAt = asc ? index : this.length - index - 1;
|
|
226
|
+
var nextNode = asc ? 'next' : 'prev';
|
|
227
|
+
var currentNode = asc ? this.head : this.tail;
|
|
143
228
|
// TODO after no-non-null-assertion not ensure the logic
|
|
144
|
-
for (
|
|
229
|
+
for (var currentIndex = 0; currentIndex < stopAt; currentIndex++) {
|
|
145
230
|
if (currentNode) {
|
|
146
231
|
currentNode = currentNode[nextNode];
|
|
147
232
|
}
|
|
148
233
|
}
|
|
149
234
|
return currentNode || undefined;
|
|
150
|
-
}
|
|
235
|
+
};
|
|
151
236
|
/**
|
|
152
237
|
* Return the first node and its index in the list that
|
|
153
238
|
* satisfies the testing function
|
|
@@ -157,9 +242,9 @@ class SinglyLinkedList {
|
|
|
157
242
|
* ```
|
|
158
243
|
* @param f A function to be applied to the val of each node
|
|
159
244
|
*/
|
|
160
|
-
findNodeIndex(f) {
|
|
161
|
-
|
|
162
|
-
|
|
245
|
+
SinglyLinkedList.prototype.findNodeIndex = function (f) {
|
|
246
|
+
var currentIndex = 0;
|
|
247
|
+
var currentNode = this.head;
|
|
163
248
|
while (currentNode) {
|
|
164
249
|
if (f(currentNode.val, currentIndex, this)) {
|
|
165
250
|
return {
|
|
@@ -171,7 +256,7 @@ class SinglyLinkedList {
|
|
|
171
256
|
currentIndex += 1;
|
|
172
257
|
}
|
|
173
258
|
return undefined;
|
|
174
|
-
}
|
|
259
|
+
};
|
|
175
260
|
/**
|
|
176
261
|
* Returns the first node in the list that
|
|
177
262
|
* satisfies the provided testing function. Otherwise undefined is returned.
|
|
@@ -181,10 +266,10 @@ class SinglyLinkedList {
|
|
|
181
266
|
* ```
|
|
182
267
|
* @param f Function to test val against
|
|
183
268
|
*/
|
|
184
|
-
findNode(f) {
|
|
185
|
-
|
|
269
|
+
SinglyLinkedList.prototype.findNode = function (f) {
|
|
270
|
+
var nodeIndex = this.findNodeIndex(f);
|
|
186
271
|
return nodeIndex !== undefined ? nodeIndex.node : undefined;
|
|
187
|
-
}
|
|
272
|
+
};
|
|
188
273
|
/**
|
|
189
274
|
* Returns the value of the first element in the list that
|
|
190
275
|
* satisfies the provided testing function. Otherwise undefined is returned.
|
|
@@ -193,10 +278,10 @@ class SinglyLinkedList {
|
|
|
193
278
|
* ```
|
|
194
279
|
* @param f Function to test val against
|
|
195
280
|
*/
|
|
196
|
-
find(f) {
|
|
197
|
-
|
|
281
|
+
SinglyLinkedList.prototype.find = function (f) {
|
|
282
|
+
var nodeIndex = this.findNodeIndex(f);
|
|
198
283
|
return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
|
|
199
|
-
}
|
|
284
|
+
};
|
|
200
285
|
/**
|
|
201
286
|
* Returns the index of the first node in the list that
|
|
202
287
|
* satisfies the provided testing function. Ohterwise -1 is returned.
|
|
@@ -205,10 +290,10 @@ class SinglyLinkedList {
|
|
|
205
290
|
* ```
|
|
206
291
|
* @param f Function to test val against
|
|
207
292
|
*/
|
|
208
|
-
findIndex(f) {
|
|
209
|
-
|
|
293
|
+
SinglyLinkedList.prototype.findIndex = function (f) {
|
|
294
|
+
var nodeIndex = this.findNodeIndex(f);
|
|
210
295
|
return nodeIndex !== undefined ? nodeIndex.index : -1;
|
|
211
|
-
}
|
|
296
|
+
};
|
|
212
297
|
/**
|
|
213
298
|
* Append one or any number of nodes to the end of the list.
|
|
214
299
|
* This modifies the list in place and returns the list itself
|
|
@@ -218,20 +303,35 @@ class SinglyLinkedList {
|
|
|
218
303
|
* ```
|
|
219
304
|
* @param args Data to be stored in the node, takes any number of arguments
|
|
220
305
|
*/
|
|
221
|
-
append(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
306
|
+
SinglyLinkedList.prototype.append = function () {
|
|
307
|
+
var e_1, _a;
|
|
308
|
+
var args = [];
|
|
309
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
310
|
+
args[_i] = arguments[_i];
|
|
311
|
+
}
|
|
312
|
+
try {
|
|
313
|
+
for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
|
|
314
|
+
var val = args_1_1.value;
|
|
315
|
+
var node = new SinglyLinkedListNode(val, this.tail, null, this);
|
|
316
|
+
if (this.head === null) {
|
|
317
|
+
this.head = node;
|
|
318
|
+
}
|
|
319
|
+
if (this.tail !== null) {
|
|
320
|
+
this.tail.next = node;
|
|
321
|
+
}
|
|
322
|
+
this.tail = node;
|
|
323
|
+
this.size += 1;
|
|
226
324
|
}
|
|
227
|
-
|
|
228
|
-
|
|
325
|
+
}
|
|
326
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
327
|
+
finally {
|
|
328
|
+
try {
|
|
329
|
+
if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
|
|
229
330
|
}
|
|
230
|
-
|
|
231
|
-
this.size += 1;
|
|
331
|
+
finally { if (e_1) throw e_1.error; }
|
|
232
332
|
}
|
|
233
333
|
return this;
|
|
234
|
-
}
|
|
334
|
+
};
|
|
235
335
|
/**
|
|
236
336
|
* Synonym for append
|
|
237
337
|
* ```ts
|
|
@@ -239,10 +339,14 @@ class SinglyLinkedList {
|
|
|
239
339
|
* ```
|
|
240
340
|
* @param args Data to be stored, takes any number of arguments
|
|
241
341
|
*/
|
|
242
|
-
push(
|
|
243
|
-
|
|
342
|
+
SinglyLinkedList.prototype.push = function () {
|
|
343
|
+
var args = [];
|
|
344
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
345
|
+
args[_i] = arguments[_i];
|
|
346
|
+
}
|
|
347
|
+
this.append.apply(this, __spreadArray([], __read(args), false));
|
|
244
348
|
return this.length;
|
|
245
|
-
}
|
|
349
|
+
};
|
|
246
350
|
/**
|
|
247
351
|
* Prepend any number of val arguments to the list. The
|
|
248
352
|
* argument list is prepended as a block to reduce confusion:
|
|
@@ -251,21 +355,36 @@ class SinglyLinkedList {
|
|
|
251
355
|
* ```
|
|
252
356
|
* @param args Data to be stored in the node, accepts any number of arguments
|
|
253
357
|
*/
|
|
254
|
-
prepend(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
358
|
+
SinglyLinkedList.prototype.prepend = function () {
|
|
359
|
+
var e_2, _a;
|
|
360
|
+
var args = [];
|
|
361
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
362
|
+
args[_i] = arguments[_i];
|
|
363
|
+
}
|
|
364
|
+
var reverseArgs = Array.from(args).reverse();
|
|
365
|
+
try {
|
|
366
|
+
for (var reverseArgs_1 = __values(reverseArgs), reverseArgs_1_1 = reverseArgs_1.next(); !reverseArgs_1_1.done; reverseArgs_1_1 = reverseArgs_1.next()) {
|
|
367
|
+
var val = reverseArgs_1_1.value;
|
|
368
|
+
var node = new SinglyLinkedListNode(val, null, this.head, this);
|
|
369
|
+
if (this.tail === null) {
|
|
370
|
+
this.tail = node;
|
|
371
|
+
}
|
|
372
|
+
if (this.head !== null) {
|
|
373
|
+
this.head.prev = node;
|
|
374
|
+
}
|
|
375
|
+
this.head = node;
|
|
376
|
+
this.size += 1;
|
|
260
377
|
}
|
|
261
|
-
|
|
262
|
-
|
|
378
|
+
}
|
|
379
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
380
|
+
finally {
|
|
381
|
+
try {
|
|
382
|
+
if (reverseArgs_1_1 && !reverseArgs_1_1.done && (_a = reverseArgs_1.return)) _a.call(reverseArgs_1);
|
|
263
383
|
}
|
|
264
|
-
|
|
265
|
-
this.size += 1;
|
|
384
|
+
finally { if (e_2) throw e_2.error; }
|
|
266
385
|
}
|
|
267
386
|
return this;
|
|
268
|
-
}
|
|
387
|
+
};
|
|
269
388
|
/**
|
|
270
389
|
* Insert a new node at a given index position. If index is
|
|
271
390
|
* out of bounds, the node is appended, if index is negative
|
|
@@ -276,22 +395,22 @@ class SinglyLinkedList {
|
|
|
276
395
|
* @param index The index to insert the new node at
|
|
277
396
|
* @param val Data to be stored on the new node
|
|
278
397
|
*/
|
|
279
|
-
insertAt(index, val) {
|
|
398
|
+
SinglyLinkedList.prototype.insertAt = function (index, val) {
|
|
280
399
|
if (this.head === null) {
|
|
281
400
|
return this.append(val);
|
|
282
401
|
}
|
|
283
402
|
if (index <= 0) {
|
|
284
403
|
return this.prepend(val);
|
|
285
404
|
}
|
|
286
|
-
|
|
287
|
-
|
|
405
|
+
var currentNode = this.head;
|
|
406
|
+
var currentIndex = 0;
|
|
288
407
|
while (currentIndex < index - 1 && currentNode.next !== null) {
|
|
289
408
|
currentIndex += 1;
|
|
290
409
|
currentNode = currentNode.next;
|
|
291
410
|
}
|
|
292
411
|
currentNode.insertAfter(val);
|
|
293
412
|
return this;
|
|
294
|
-
}
|
|
413
|
+
};
|
|
295
414
|
/**
|
|
296
415
|
* Remove the specified node from the list and return the removed
|
|
297
416
|
* node afterwards.
|
|
@@ -301,7 +420,7 @@ class SinglyLinkedList {
|
|
|
301
420
|
* ```
|
|
302
421
|
* @param node The node to be removed
|
|
303
422
|
*/
|
|
304
|
-
removeNode(node) {
|
|
423
|
+
SinglyLinkedList.prototype.removeNode = function (node) {
|
|
305
424
|
if (node.list !== this) {
|
|
306
425
|
throw new ReferenceError('Node does not belong to this list');
|
|
307
426
|
}
|
|
@@ -322,7 +441,7 @@ class SinglyLinkedList {
|
|
|
322
441
|
node.prev = null;
|
|
323
442
|
node.list = null;
|
|
324
443
|
return node;
|
|
325
|
-
}
|
|
444
|
+
};
|
|
326
445
|
/**
|
|
327
446
|
* Remove the node at the specified index
|
|
328
447
|
* ```ts
|
|
@@ -330,10 +449,10 @@ class SinglyLinkedList {
|
|
|
330
449
|
* ```
|
|
331
450
|
* @param index Index at which to remove
|
|
332
451
|
*/
|
|
333
|
-
removeAt(index) {
|
|
334
|
-
|
|
452
|
+
SinglyLinkedList.prototype.removeAt = function (index) {
|
|
453
|
+
var node = this.getNode(index);
|
|
335
454
|
return node !== undefined ? this.removeNode(node) : undefined;
|
|
336
|
-
}
|
|
455
|
+
};
|
|
337
456
|
/**
|
|
338
457
|
* Insert a new node before the reference node
|
|
339
458
|
* ```ts
|
|
@@ -343,8 +462,8 @@ class SinglyLinkedList {
|
|
|
343
462
|
* @param referenceNode The node reference
|
|
344
463
|
* @param val Data to save in the node
|
|
345
464
|
*/
|
|
346
|
-
insertBefore(referenceNode, val) {
|
|
347
|
-
|
|
465
|
+
SinglyLinkedList.prototype.insertBefore = function (referenceNode, val) {
|
|
466
|
+
var node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
|
|
348
467
|
if (referenceNode.prev === null) {
|
|
349
468
|
this.head = node;
|
|
350
469
|
}
|
|
@@ -354,7 +473,7 @@ class SinglyLinkedList {
|
|
|
354
473
|
referenceNode.prev = node;
|
|
355
474
|
this.size += 1;
|
|
356
475
|
return this;
|
|
357
|
-
}
|
|
476
|
+
};
|
|
358
477
|
/**
|
|
359
478
|
* Sorts the linked list using the provided compare function
|
|
360
479
|
* @param compare A function used to compare the val of two nodes. It should return
|
|
@@ -362,25 +481,25 @@ class SinglyLinkedList {
|
|
|
362
481
|
* (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1,
|
|
363
482
|
* the sort order will be ascending.
|
|
364
483
|
*/
|
|
365
|
-
sort(compare) {
|
|
484
|
+
SinglyLinkedList.prototype.sort = function (compare) {
|
|
366
485
|
if (this.head === null || this.tail === null) {
|
|
367
486
|
return this;
|
|
368
487
|
}
|
|
369
488
|
if (this.length < 2) {
|
|
370
489
|
return this;
|
|
371
490
|
}
|
|
372
|
-
|
|
491
|
+
var quicksort = function (start, end) {
|
|
373
492
|
if (start === end) {
|
|
374
493
|
return;
|
|
375
494
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
495
|
+
var pivotData = end.val;
|
|
496
|
+
var current = start;
|
|
497
|
+
var split = start;
|
|
379
498
|
while (current && current !== end) {
|
|
380
|
-
|
|
499
|
+
var sort = compare(current.val, pivotData);
|
|
381
500
|
if (sort) {
|
|
382
501
|
if (current !== split) {
|
|
383
|
-
|
|
502
|
+
var temp = split.val;
|
|
384
503
|
split.val = current.val;
|
|
385
504
|
current.val = temp;
|
|
386
505
|
}
|
|
@@ -405,7 +524,7 @@ class SinglyLinkedList {
|
|
|
405
524
|
};
|
|
406
525
|
quicksort(this.head, this.tail);
|
|
407
526
|
return this;
|
|
408
|
-
}
|
|
527
|
+
};
|
|
409
528
|
/**
|
|
410
529
|
* Insert a new node after this one
|
|
411
530
|
* ```ts
|
|
@@ -415,8 +534,8 @@ class SinglyLinkedList {
|
|
|
415
534
|
* @param referenceNode The reference node
|
|
416
535
|
* @param val Data to be saved in the node
|
|
417
536
|
*/
|
|
418
|
-
insertAfter(referenceNode, val) {
|
|
419
|
-
|
|
537
|
+
SinglyLinkedList.prototype.insertAfter = function (referenceNode, val) {
|
|
538
|
+
var node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
|
|
420
539
|
if (referenceNode.next === null) {
|
|
421
540
|
this.tail = node;
|
|
422
541
|
}
|
|
@@ -426,7 +545,7 @@ class SinglyLinkedList {
|
|
|
426
545
|
referenceNode.next = node;
|
|
427
546
|
this.size += 1;
|
|
428
547
|
return this;
|
|
429
|
-
}
|
|
548
|
+
};
|
|
430
549
|
/**
|
|
431
550
|
* Remove the first node from the list and return the val of the removed node
|
|
432
551
|
* or undefined
|
|
@@ -434,9 +553,9 @@ class SinglyLinkedList {
|
|
|
434
553
|
* new LinkedList(1, 2, 3).shift(); // 1
|
|
435
554
|
* ```
|
|
436
555
|
*/
|
|
437
|
-
shift() {
|
|
556
|
+
SinglyLinkedList.prototype.shift = function () {
|
|
438
557
|
return this.removeFromAnyEnd(this.head);
|
|
439
|
-
}
|
|
558
|
+
};
|
|
440
559
|
/**
|
|
441
560
|
* Remove the last node from the list and return the val of the removed node
|
|
442
561
|
* or undefined if the list was empty
|
|
@@ -444,9 +563,9 @@ class SinglyLinkedList {
|
|
|
444
563
|
* new LinkedList(1, 2, 3).pop(); // 3
|
|
445
564
|
* ```
|
|
446
565
|
*/
|
|
447
|
-
pop() {
|
|
566
|
+
SinglyLinkedList.prototype.pop = function () {
|
|
448
567
|
return this.removeFromAnyEnd(this.tail);
|
|
449
|
-
}
|
|
568
|
+
};
|
|
450
569
|
/**
|
|
451
570
|
* Merge the current list with another. Both lists will be
|
|
452
571
|
* equal after merging.
|
|
@@ -458,7 +577,7 @@ class SinglyLinkedList {
|
|
|
458
577
|
* ```
|
|
459
578
|
* @param list The list to be merged
|
|
460
579
|
*/
|
|
461
|
-
merge(list) {
|
|
580
|
+
SinglyLinkedList.prototype.merge = function (list) {
|
|
462
581
|
if (this.tail !== null) {
|
|
463
582
|
this.tail.next = list.head;
|
|
464
583
|
}
|
|
@@ -471,7 +590,7 @@ class SinglyLinkedList {
|
|
|
471
590
|
list.size = this.size;
|
|
472
591
|
list.head = this.head;
|
|
473
592
|
list.tail = this.tail;
|
|
474
|
-
}
|
|
593
|
+
};
|
|
475
594
|
/**
|
|
476
595
|
* Removes all nodes from a list
|
|
477
596
|
*
|
|
@@ -479,12 +598,12 @@ class SinglyLinkedList {
|
|
|
479
598
|
* list.clear();
|
|
480
599
|
* ```
|
|
481
600
|
*/
|
|
482
|
-
clear() {
|
|
601
|
+
SinglyLinkedList.prototype.clear = function () {
|
|
483
602
|
this.head = null;
|
|
484
603
|
this.tail = null;
|
|
485
604
|
this.size = 0;
|
|
486
605
|
return this;
|
|
487
|
-
}
|
|
606
|
+
};
|
|
488
607
|
/**
|
|
489
608
|
* The slice() method returns a shallow copy of a
|
|
490
609
|
* portion of a list into a new list object selected
|
|
@@ -498,22 +617,22 @@ class SinglyLinkedList {
|
|
|
498
617
|
* @param end End index, optional
|
|
499
618
|
*/
|
|
500
619
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
501
|
-
slice(start, end) {
|
|
502
|
-
|
|
503
|
-
|
|
620
|
+
SinglyLinkedList.prototype.slice = function (start, end) {
|
|
621
|
+
var list = new SinglyLinkedList();
|
|
622
|
+
var finish = end;
|
|
504
623
|
if (this.head === null || this.tail === null) {
|
|
505
624
|
return list;
|
|
506
625
|
}
|
|
507
626
|
if (finish === undefined || finish < start) {
|
|
508
627
|
finish = this.length;
|
|
509
628
|
}
|
|
510
|
-
|
|
511
|
-
for (
|
|
629
|
+
var head = this.getNode(start);
|
|
630
|
+
for (var i = 0; i < finish - start && head !== null && head !== undefined; i++) {
|
|
512
631
|
list.append(head.val);
|
|
513
632
|
head = head.next;
|
|
514
633
|
}
|
|
515
634
|
return list;
|
|
516
|
-
}
|
|
635
|
+
};
|
|
517
636
|
/**
|
|
518
637
|
* The reverse() function reverses the list in place and returns the list
|
|
519
638
|
* itself.
|
|
@@ -521,19 +640,19 @@ class SinglyLinkedList {
|
|
|
521
640
|
* new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
|
|
522
641
|
* ```
|
|
523
642
|
*/
|
|
524
|
-
reverse() {
|
|
525
|
-
|
|
643
|
+
SinglyLinkedList.prototype.reverse = function () {
|
|
644
|
+
var currentNode = this.head;
|
|
526
645
|
while (currentNode) {
|
|
527
|
-
|
|
646
|
+
var next = currentNode.next;
|
|
528
647
|
currentNode.next = currentNode.prev;
|
|
529
648
|
currentNode.prev = next;
|
|
530
649
|
currentNode = currentNode.prev;
|
|
531
650
|
}
|
|
532
|
-
|
|
651
|
+
var tail = this.tail;
|
|
533
652
|
this.tail = this.head;
|
|
534
653
|
this.head = tail;
|
|
535
654
|
return this;
|
|
536
|
-
}
|
|
655
|
+
};
|
|
537
656
|
/**
|
|
538
657
|
* The forEach() method executes a provided function once for each list node.
|
|
539
658
|
* ```ts
|
|
@@ -542,17 +661,18 @@ class SinglyLinkedList {
|
|
|
542
661
|
* @param f Function to execute for each element, taking up to three arguments.
|
|
543
662
|
* @param reverse Indicates if the list should be walked in reverse order, default is false
|
|
544
663
|
*/
|
|
545
|
-
forEach(f, reverse
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
664
|
+
SinglyLinkedList.prototype.forEach = function (f, reverse) {
|
|
665
|
+
if (reverse === void 0) { reverse = false; }
|
|
666
|
+
var currentIndex = reverse ? this.length - 1 : 0;
|
|
667
|
+
var currentNode = reverse ? this.tail : this.head;
|
|
668
|
+
var modifier = reverse ? -1 : 1;
|
|
669
|
+
var nextNode = reverse ? 'prev' : 'next';
|
|
550
670
|
while (currentNode) {
|
|
551
671
|
f(currentNode.val, currentIndex, this);
|
|
552
672
|
currentNode = currentNode[nextNode];
|
|
553
673
|
currentIndex += modifier;
|
|
554
674
|
}
|
|
555
|
-
}
|
|
675
|
+
};
|
|
556
676
|
/**
|
|
557
677
|
* The map() method creates a new list with the results of
|
|
558
678
|
* calling a provided function on every node in the calling list.
|
|
@@ -563,11 +683,13 @@ class SinglyLinkedList {
|
|
|
563
683
|
* @param reverse Indicates if the list should be mapped in reverse order, default is false
|
|
564
684
|
*/
|
|
565
685
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
566
|
-
map(f, reverse
|
|
567
|
-
|
|
568
|
-
|
|
686
|
+
SinglyLinkedList.prototype.map = function (f, reverse) {
|
|
687
|
+
var _this = this;
|
|
688
|
+
if (reverse === void 0) { reverse = false; }
|
|
689
|
+
var list = new SinglyLinkedList();
|
|
690
|
+
this.forEach(function (val, index) { return list.append(f(val, index, _this)); }, reverse);
|
|
569
691
|
return list;
|
|
570
|
-
}
|
|
692
|
+
};
|
|
571
693
|
/**
|
|
572
694
|
* The filter() method creates a new list with all nodes
|
|
573
695
|
* that pass the test implemented by the provided function.
|
|
@@ -578,15 +700,17 @@ class SinglyLinkedList {
|
|
|
578
700
|
* @param reverse Indicates if the list should be filtered in reverse order, default is false
|
|
579
701
|
*/
|
|
580
702
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
581
|
-
filter(f, reverse
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
703
|
+
SinglyLinkedList.prototype.filter = function (f, reverse) {
|
|
704
|
+
var _this = this;
|
|
705
|
+
if (reverse === void 0) { reverse = false; }
|
|
706
|
+
var list = new SinglyLinkedList();
|
|
707
|
+
this.forEach(function (val, index) {
|
|
708
|
+
if (f(val, index, _this)) {
|
|
585
709
|
list.append(val);
|
|
586
710
|
}
|
|
587
711
|
}, reverse);
|
|
588
712
|
return list;
|
|
589
|
-
}
|
|
713
|
+
};
|
|
590
714
|
/**
|
|
591
715
|
* Reduce over each node in the list
|
|
592
716
|
* ```ts
|
|
@@ -596,12 +720,13 @@ class SinglyLinkedList {
|
|
|
596
720
|
* @param start An initial value
|
|
597
721
|
* @returns The final state of the accumulator
|
|
598
722
|
*/
|
|
599
|
-
reduce(f, start, reverse
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
723
|
+
SinglyLinkedList.prototype.reduce = function (f, start, reverse) {
|
|
724
|
+
if (reverse === void 0) { reverse = false; }
|
|
725
|
+
var currentIndex = reverse ? this.length - 1 : 0;
|
|
726
|
+
var modifier = reverse ? -1 : 1;
|
|
727
|
+
var nextNode = reverse ? 'prev' : 'next';
|
|
728
|
+
var currentElement = reverse ? this.tail : this.head;
|
|
729
|
+
var result;
|
|
605
730
|
if (start !== undefined) {
|
|
606
731
|
result = start;
|
|
607
732
|
}
|
|
@@ -618,16 +743,16 @@ class SinglyLinkedList {
|
|
|
618
743
|
currentElement = currentElement[nextNode];
|
|
619
744
|
}
|
|
620
745
|
return result;
|
|
621
|
-
}
|
|
746
|
+
};
|
|
622
747
|
/**
|
|
623
748
|
* Convert the linked list to an array
|
|
624
749
|
* ```ts
|
|
625
750
|
* new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
|
|
626
751
|
* ```
|
|
627
752
|
*/
|
|
628
|
-
toArray() {
|
|
629
|
-
return [
|
|
630
|
-
}
|
|
753
|
+
SinglyLinkedList.prototype.toArray = function () {
|
|
754
|
+
return __spreadArray([], __read(this), false);
|
|
755
|
+
};
|
|
631
756
|
/**
|
|
632
757
|
* Convert a linked list to string
|
|
633
758
|
* ```ts
|
|
@@ -635,9 +760,10 @@ class SinglyLinkedList {
|
|
|
635
760
|
* ```
|
|
636
761
|
* @param separator Optional string to be placed in between val nodes, default is one space
|
|
637
762
|
*/
|
|
638
|
-
toString
|
|
639
|
-
|
|
640
|
-
|
|
763
|
+
SinglyLinkedList.prototype.toString = function (separator) {
|
|
764
|
+
if (separator === void 0) { separator = ' '; }
|
|
765
|
+
return this.reduce(function (s, val) { return "".concat(s).concat(separator).concat(val); });
|
|
766
|
+
};
|
|
641
767
|
/**
|
|
642
768
|
* The iterator implementation
|
|
643
769
|
* ```ts
|
|
@@ -645,16 +771,28 @@ class SinglyLinkedList {
|
|
|
645
771
|
* for (const val of list) { log(val); } // 1 2 3
|
|
646
772
|
* ```
|
|
647
773
|
*/
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
774
|
+
SinglyLinkedList.prototype[Symbol.iterator] = function () {
|
|
775
|
+
var element;
|
|
776
|
+
return __generator(this, function (_a) {
|
|
777
|
+
switch (_a.label) {
|
|
778
|
+
case 0:
|
|
779
|
+
element = this.head;
|
|
780
|
+
_a.label = 1;
|
|
781
|
+
case 1:
|
|
782
|
+
if (!(element !== null)) return [3 /*break*/, 3];
|
|
783
|
+
return [4 /*yield*/, element.val];
|
|
784
|
+
case 2:
|
|
785
|
+
_a.sent();
|
|
786
|
+
element = element.next;
|
|
787
|
+
return [3 /*break*/, 1];
|
|
788
|
+
case 3: return [2 /*return*/];
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
};
|
|
655
792
|
/** Private helper function to reduce duplication of pop() and shift() methods */
|
|
656
|
-
removeFromAnyEnd(node) {
|
|
793
|
+
SinglyLinkedList.prototype.removeFromAnyEnd = function (node) {
|
|
657
794
|
return node !== null ? this.removeNode(node).val : undefined;
|
|
658
|
-
}
|
|
659
|
-
|
|
795
|
+
};
|
|
796
|
+
return SinglyLinkedList;
|
|
797
|
+
}());
|
|
660
798
|
exports.SinglyLinkedList = SinglyLinkedList;
|