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