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