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