data-structure-typed 1.19.3 → 1.19.4
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/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
- package/dist/data-structures/binary-tree/avl-tree.js +46 -90
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -31
- package/dist/data-structures/binary-tree/bst.js +96 -139
- package/dist/data-structures/binary-tree/rb-tree.js +32 -56
- package/dist/data-structures/binary-tree/segment-tree.js +78 -120
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.js +340 -574
- package/dist/data-structures/graph/directed-graph.js +146 -276
- package/dist/data-structures/graph/undirected-graph.js +87 -176
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/hash-table.js +2 -5
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/heap/heap.js +53 -77
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
- package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix2d.js +76 -93
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/vector2d.js +80 -101
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/queue.js +24 -25
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/utils/utils.js +22 -107
- package/dist/utils/validate-type.js +2 -2
- package/package.json +1 -1
- 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 +3 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
- package/src/data-structures/binary-tree/avl-tree.ts +297 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/src/data-structures/binary-tree/binary-tree.ts +40 -0
- package/src/data-structures/binary-tree/bst.ts +435 -0
- 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 +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +102 -0
- package/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +1032 -0
- 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 +472 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +270 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +3 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +3 -0
- package/src/data-structures/hash/tree-map.ts +3 -0
- package/src/data-structures/hash/tree-set.ts +3 -0
- package/src/data-structures/heap/heap.ts +183 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +34 -0
- package/src/data-structures/index.ts +15 -0
- package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
- package/src/data-structures/interfaces/abstract-graph.ts +40 -0
- package/src/data-structures/interfaces/avl-tree.ts +28 -0
- package/src/data-structures/interfaces/binary-tree.ts +8 -0
- package/src/data-structures/interfaces/bst.ts +32 -0
- package/src/data-structures/interfaces/directed-graph.ts +20 -0
- package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/heap.ts +1 -0
- package/src/data-structures/interfaces/index.ts +15 -0
- package/src/data-structures/interfaces/navigator.ts +1 -0
- package/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/src/data-structures/interfaces/rb-tree.ts +11 -0
- package/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/tree-multiset.ts +12 -0
- package/src/data-structures/interfaces/undirected-graph.ts +6 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +208 -0
- package/src/data-structures/matrix/navigator.ts +122 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/src/data-structures/queue/deque.ts +251 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +120 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +227 -0
- package/src/data-structures/types/abstract-binary-tree.ts +42 -0
- package/src/data-structures/types/abstract-graph.ts +5 -0
- package/src/data-structures/types/avl-tree.ts +5 -0
- package/src/data-structures/types/binary-tree.ts +9 -0
- package/src/data-structures/types/bst.ts +12 -0
- package/src/data-structures/types/directed-graph.ts +8 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +5 -0
- package/src/data-structures/types/helpers.ts +1 -0
- package/src/data-structures/types/index.ts +15 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/rb-tree.ts +8 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +8 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/types/index.ts +2 -0
- package/src/utils/types/utils.ts +6 -0
- package/src/utils/types/validate-type.ts +25 -0
- package/src/utils/utils.ts +78 -0
- package/src/utils/validate-type.ts +69 -0
- package/tsconfig.json +1 -1
|
@@ -1,31 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
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.");
|
|
12
|
-
};
|
|
13
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
-
if (!m) return o;
|
|
16
|
-
var i = m.call(o), r, ar = [], e;
|
|
17
|
-
try {
|
|
18
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
-
}
|
|
20
|
-
catch (error) { e = { error: error }; }
|
|
21
|
-
finally {
|
|
22
|
-
try {
|
|
23
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
-
}
|
|
25
|
-
finally { if (e) throw e.error; }
|
|
26
|
-
}
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
3
|
exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
31
4
|
/**
|
|
@@ -35,109 +8,77 @@ exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
|
35
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
36
9
|
* @license MIT License
|
|
37
10
|
*/
|
|
38
|
-
|
|
11
|
+
class SinglyLinkedListNode {
|
|
39
12
|
/**
|
|
40
13
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
41
14
|
* @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
|
|
42
15
|
* will be stored in the node of a linked list.
|
|
43
16
|
*/
|
|
44
|
-
|
|
17
|
+
constructor(val) {
|
|
45
18
|
this._val = val;
|
|
46
19
|
this._next = null;
|
|
47
20
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
},
|
|
62
|
-
set: function (value) {
|
|
63
|
-
this._next = value;
|
|
64
|
-
},
|
|
65
|
-
enumerable: false,
|
|
66
|
-
configurable: true
|
|
67
|
-
});
|
|
68
|
-
return SinglyLinkedListNode;
|
|
69
|
-
}());
|
|
21
|
+
get val() {
|
|
22
|
+
return this._val;
|
|
23
|
+
}
|
|
24
|
+
set val(value) {
|
|
25
|
+
this._val = value;
|
|
26
|
+
}
|
|
27
|
+
get next() {
|
|
28
|
+
return this._next;
|
|
29
|
+
}
|
|
30
|
+
set next(value) {
|
|
31
|
+
this._next = value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
70
34
|
exports.SinglyLinkedListNode = SinglyLinkedListNode;
|
|
71
|
-
|
|
35
|
+
class SinglyLinkedList {
|
|
72
36
|
/**
|
|
73
37
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
74
38
|
*/
|
|
75
|
-
|
|
39
|
+
constructor() {
|
|
76
40
|
this._head = null;
|
|
77
41
|
this._tail = null;
|
|
78
42
|
this._length = 0;
|
|
79
43
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
this._tail = value;
|
|
96
|
-
},
|
|
97
|
-
enumerable: false,
|
|
98
|
-
configurable: true
|
|
99
|
-
});
|
|
100
|
-
Object.defineProperty(SinglyLinkedList.prototype, "length", {
|
|
101
|
-
get: function () {
|
|
102
|
-
return this._length;
|
|
103
|
-
},
|
|
104
|
-
enumerable: false,
|
|
105
|
-
configurable: true
|
|
106
|
-
});
|
|
44
|
+
get head() {
|
|
45
|
+
return this._head;
|
|
46
|
+
}
|
|
47
|
+
set head(value) {
|
|
48
|
+
this._head = value;
|
|
49
|
+
}
|
|
50
|
+
get tail() {
|
|
51
|
+
return this._tail;
|
|
52
|
+
}
|
|
53
|
+
set tail(value) {
|
|
54
|
+
this._tail = value;
|
|
55
|
+
}
|
|
56
|
+
get length() {
|
|
57
|
+
return this._length;
|
|
58
|
+
}
|
|
107
59
|
/**
|
|
108
60
|
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
109
61
|
* array.
|
|
110
62
|
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
|
111
63
|
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
112
64
|
*/
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
|
|
118
|
-
var item = data_1_1.value;
|
|
119
|
-
singlyLinkedList.push(item);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
123
|
-
finally {
|
|
124
|
-
try {
|
|
125
|
-
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
|
|
126
|
-
}
|
|
127
|
-
finally { if (e_1) throw e_1.error; }
|
|
65
|
+
static fromArray(data) {
|
|
66
|
+
const singlyLinkedList = new SinglyLinkedList();
|
|
67
|
+
for (const item of data) {
|
|
68
|
+
singlyLinkedList.push(item);
|
|
128
69
|
}
|
|
129
70
|
return singlyLinkedList;
|
|
130
|
-
}
|
|
131
|
-
|
|
71
|
+
}
|
|
72
|
+
getLength() {
|
|
132
73
|
return this._length;
|
|
133
|
-
}
|
|
74
|
+
}
|
|
134
75
|
/**
|
|
135
76
|
* The `push` function adds a new node with the given data to the end of a singly linked list.
|
|
136
77
|
* @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
|
|
137
78
|
* any type (T) as specified in the generic type declaration of the class or function.
|
|
138
79
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
80
|
+
push(data) {
|
|
81
|
+
const newNode = new SinglyLinkedListNode(data);
|
|
141
82
|
if (!this.head) {
|
|
142
83
|
this.head = newNode;
|
|
143
84
|
this.tail = newNode;
|
|
@@ -147,52 +88,52 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
147
88
|
this.tail = newNode;
|
|
148
89
|
}
|
|
149
90
|
this._length++;
|
|
150
|
-
}
|
|
91
|
+
}
|
|
151
92
|
/**
|
|
152
93
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
153
94
|
* pointers accordingly.
|
|
154
95
|
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
155
96
|
* the linked list is empty, it returns `null`.
|
|
156
97
|
*/
|
|
157
|
-
|
|
98
|
+
pop() {
|
|
158
99
|
if (!this.head)
|
|
159
100
|
return null;
|
|
160
101
|
if (this.head === this.tail) {
|
|
161
|
-
|
|
102
|
+
const val = this.head.val;
|
|
162
103
|
this.head = null;
|
|
163
104
|
this.tail = null;
|
|
164
105
|
this._length--;
|
|
165
|
-
return
|
|
106
|
+
return val;
|
|
166
107
|
}
|
|
167
|
-
|
|
108
|
+
let current = this.head;
|
|
168
109
|
while (current.next !== this.tail) {
|
|
169
110
|
current = current.next;
|
|
170
111
|
}
|
|
171
|
-
|
|
112
|
+
const val = this.tail.val;
|
|
172
113
|
current.next = null;
|
|
173
114
|
this.tail = current;
|
|
174
115
|
this._length--;
|
|
175
116
|
return val;
|
|
176
|
-
}
|
|
117
|
+
}
|
|
177
118
|
/**
|
|
178
119
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
179
120
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
180
121
|
*/
|
|
181
|
-
|
|
122
|
+
shift() {
|
|
182
123
|
if (!this.head)
|
|
183
124
|
return null;
|
|
184
|
-
|
|
125
|
+
const removedNode = this.head;
|
|
185
126
|
this.head = this.head.next;
|
|
186
127
|
this._length--;
|
|
187
128
|
return removedNode.val;
|
|
188
|
-
}
|
|
129
|
+
}
|
|
189
130
|
/**
|
|
190
131
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
191
132
|
* @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
192
133
|
* linked list.
|
|
193
134
|
*/
|
|
194
|
-
|
|
195
|
-
|
|
135
|
+
unshift(val) {
|
|
136
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
196
137
|
if (!this.head) {
|
|
197
138
|
this.head = newNode;
|
|
198
139
|
this.tail = newNode;
|
|
@@ -202,7 +143,7 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
202
143
|
this.head = newNode;
|
|
203
144
|
}
|
|
204
145
|
this._length++;
|
|
205
|
-
}
|
|
146
|
+
}
|
|
206
147
|
/**
|
|
207
148
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
208
149
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -210,15 +151,15 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
210
151
|
* @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
|
|
211
152
|
* `null` if the index is out of bounds.
|
|
212
153
|
*/
|
|
213
|
-
|
|
154
|
+
getAt(index) {
|
|
214
155
|
if (index < 0 || index >= this.length)
|
|
215
156
|
return null;
|
|
216
|
-
|
|
217
|
-
for (
|
|
157
|
+
let current = this.head;
|
|
158
|
+
for (let i = 0; i < index; i++) {
|
|
218
159
|
current = current.next;
|
|
219
160
|
}
|
|
220
161
|
return current.val;
|
|
221
|
-
}
|
|
162
|
+
}
|
|
222
163
|
/**
|
|
223
164
|
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
224
165
|
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
@@ -226,13 +167,13 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
226
167
|
* @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
|
|
227
168
|
* specified index exists, or `null` if the index is out of bounds.
|
|
228
169
|
*/
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
for (
|
|
170
|
+
getNodeAt(index) {
|
|
171
|
+
let current = this.head;
|
|
172
|
+
for (let i = 0; i < index; i++) {
|
|
232
173
|
current = current.next;
|
|
233
174
|
}
|
|
234
175
|
return current;
|
|
235
|
-
}
|
|
176
|
+
}
|
|
236
177
|
/**
|
|
237
178
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
238
179
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -240,19 +181,19 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
240
181
|
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
241
182
|
* bounds.
|
|
242
183
|
*/
|
|
243
|
-
|
|
184
|
+
deleteAt(index) {
|
|
244
185
|
if (index < 0 || index >= this.length)
|
|
245
186
|
return null;
|
|
246
187
|
if (index === 0)
|
|
247
188
|
return this.shift();
|
|
248
189
|
if (index === this.length - 1)
|
|
249
190
|
return this.pop();
|
|
250
|
-
|
|
251
|
-
|
|
191
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
192
|
+
const removedNode = prevNode.next;
|
|
252
193
|
prevNode.next = removedNode.next;
|
|
253
194
|
this._length--;
|
|
254
195
|
return removedNode.val;
|
|
255
|
-
}
|
|
196
|
+
}
|
|
256
197
|
/**
|
|
257
198
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
258
199
|
* @param {T | SinglyLinkedListNode<T>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `T`
|
|
@@ -260,15 +201,15 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
260
201
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
261
202
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
262
203
|
*/
|
|
263
|
-
|
|
264
|
-
|
|
204
|
+
delete(valueOrNode) {
|
|
205
|
+
let value;
|
|
265
206
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
266
207
|
value = valueOrNode.val;
|
|
267
208
|
}
|
|
268
209
|
else {
|
|
269
210
|
value = valueOrNode;
|
|
270
211
|
}
|
|
271
|
-
|
|
212
|
+
let current = this.head, prev = null;
|
|
272
213
|
while (current) {
|
|
273
214
|
if (current.val === value) {
|
|
274
215
|
if (prev === null) {
|
|
@@ -290,7 +231,7 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
290
231
|
current = current.next;
|
|
291
232
|
}
|
|
292
233
|
return false;
|
|
293
|
-
}
|
|
234
|
+
}
|
|
294
235
|
/**
|
|
295
236
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
296
237
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -300,7 +241,7 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
300
241
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
301
242
|
* if the index is out of bounds.
|
|
302
243
|
*/
|
|
303
|
-
|
|
244
|
+
insertAt(index, val) {
|
|
304
245
|
if (index < 0 || index > this.length)
|
|
305
246
|
return false;
|
|
306
247
|
if (index === 0) {
|
|
@@ -311,61 +252,60 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
311
252
|
this.push(val);
|
|
312
253
|
return true;
|
|
313
254
|
}
|
|
314
|
-
|
|
315
|
-
|
|
255
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
256
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
316
257
|
newNode.next = prevNode.next;
|
|
317
258
|
prevNode.next = newNode;
|
|
318
259
|
this._length++;
|
|
319
260
|
return true;
|
|
320
|
-
}
|
|
261
|
+
}
|
|
321
262
|
/**
|
|
322
263
|
* The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
|
|
323
264
|
* whether it is empty or not.
|
|
324
265
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
325
266
|
*/
|
|
326
|
-
|
|
267
|
+
isEmpty() {
|
|
327
268
|
return this.length === 0;
|
|
328
|
-
}
|
|
269
|
+
}
|
|
329
270
|
/**
|
|
330
271
|
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
331
272
|
*/
|
|
332
|
-
|
|
273
|
+
clear() {
|
|
333
274
|
this._head = null;
|
|
334
275
|
this._tail = null;
|
|
335
276
|
this._length = 0;
|
|
336
|
-
}
|
|
277
|
+
}
|
|
337
278
|
/**
|
|
338
279
|
* The `toArray` function converts a linked list into an array.
|
|
339
280
|
* @returns The `toArray()` method is returning an array of type `T[]`.
|
|
340
281
|
*/
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
282
|
+
toArray() {
|
|
283
|
+
const array = [];
|
|
284
|
+
let current = this.head;
|
|
344
285
|
while (current) {
|
|
345
286
|
array.push(current.val);
|
|
346
287
|
current = current.next;
|
|
347
288
|
}
|
|
348
289
|
return array;
|
|
349
|
-
}
|
|
290
|
+
}
|
|
350
291
|
/**
|
|
351
292
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
352
293
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
353
294
|
*/
|
|
354
|
-
|
|
355
|
-
var _a;
|
|
295
|
+
reverse() {
|
|
356
296
|
if (!this.head || this.head === this.tail)
|
|
357
297
|
return;
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
298
|
+
let prev = null;
|
|
299
|
+
let current = this.head;
|
|
300
|
+
let next = null;
|
|
361
301
|
while (current) {
|
|
362
302
|
next = current.next;
|
|
363
303
|
current.next = prev;
|
|
364
304
|
prev = current;
|
|
365
305
|
current = next;
|
|
366
306
|
}
|
|
367
|
-
|
|
368
|
-
}
|
|
307
|
+
[this.head, this.tail] = [this.tail, this.head];
|
|
308
|
+
}
|
|
369
309
|
/**
|
|
370
310
|
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
371
311
|
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
@@ -373,8 +313,8 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
373
313
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
374
314
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
375
315
|
*/
|
|
376
|
-
|
|
377
|
-
|
|
316
|
+
find(callback) {
|
|
317
|
+
let current = this.head;
|
|
378
318
|
while (current) {
|
|
379
319
|
if (callback(current.val)) {
|
|
380
320
|
return current.val;
|
|
@@ -382,16 +322,16 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
382
322
|
current = current.next;
|
|
383
323
|
}
|
|
384
324
|
return null;
|
|
385
|
-
}
|
|
325
|
+
}
|
|
386
326
|
/**
|
|
387
327
|
* The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
|
|
388
328
|
* @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
|
|
389
329
|
* @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
|
|
390
330
|
* value is not found, it returns -1.
|
|
391
331
|
*/
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
332
|
+
indexOf(value) {
|
|
333
|
+
let index = 0;
|
|
334
|
+
let current = this.head;
|
|
395
335
|
while (current) {
|
|
396
336
|
if (current.val === value) {
|
|
397
337
|
return index;
|
|
@@ -400,7 +340,7 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
400
340
|
current = current.next;
|
|
401
341
|
}
|
|
402
342
|
return -1;
|
|
403
|
-
}
|
|
343
|
+
}
|
|
404
344
|
/**
|
|
405
345
|
* The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
|
|
406
346
|
* null.
|
|
@@ -408,8 +348,8 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
408
348
|
* @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
|
|
409
349
|
* the specified value is found, the function returns `null`.
|
|
410
350
|
*/
|
|
411
|
-
|
|
412
|
-
|
|
351
|
+
findNode(value) {
|
|
352
|
+
let current = this.head;
|
|
413
353
|
while (current) {
|
|
414
354
|
if (current.val === value) {
|
|
415
355
|
return current;
|
|
@@ -417,7 +357,7 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
417
357
|
current = current.next;
|
|
418
358
|
}
|
|
419
359
|
return null;
|
|
420
|
-
}
|
|
360
|
+
}
|
|
421
361
|
/**
|
|
422
362
|
* The `insertBefore` function inserts a new value before an existing value in a singly linked list.
|
|
423
363
|
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node that you want to insert the
|
|
@@ -426,10 +366,10 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
426
366
|
* @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
427
367
|
* inserted before the existing value, and `false` otherwise.
|
|
428
368
|
*/
|
|
429
|
-
|
|
369
|
+
insertBefore(existingValueOrNode, newValue) {
|
|
430
370
|
if (!this.head)
|
|
431
371
|
return false;
|
|
432
|
-
|
|
372
|
+
let existingValue;
|
|
433
373
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
434
374
|
existingValue = existingValueOrNode.val;
|
|
435
375
|
}
|
|
@@ -440,10 +380,10 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
440
380
|
this.unshift(newValue);
|
|
441
381
|
return true;
|
|
442
382
|
}
|
|
443
|
-
|
|
383
|
+
let current = this.head;
|
|
444
384
|
while (current.next) {
|
|
445
385
|
if (current.next.val === existingValue) {
|
|
446
|
-
|
|
386
|
+
const newNode = new SinglyLinkedListNode(newValue);
|
|
447
387
|
newNode.next = current.next;
|
|
448
388
|
current.next = newNode;
|
|
449
389
|
this._length++;
|
|
@@ -452,7 +392,7 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
452
392
|
current = current.next;
|
|
453
393
|
}
|
|
454
394
|
return false;
|
|
455
|
-
}
|
|
395
|
+
}
|
|
456
396
|
/**
|
|
457
397
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
458
398
|
* @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node in the linked list after which
|
|
@@ -461,8 +401,8 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
461
401
|
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
462
402
|
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
463
403
|
*/
|
|
464
|
-
|
|
465
|
-
|
|
404
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
405
|
+
let existingNode;
|
|
466
406
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
467
407
|
existingNode = existingValueOrNode;
|
|
468
408
|
}
|
|
@@ -470,7 +410,7 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
470
410
|
existingNode = this.findNode(existingValueOrNode);
|
|
471
411
|
}
|
|
472
412
|
if (existingNode) {
|
|
473
|
-
|
|
413
|
+
const newNode = new SinglyLinkedListNode(newValue);
|
|
474
414
|
newNode.next = existingNode.next;
|
|
475
415
|
existingNode.next = newNode;
|
|
476
416
|
if (existingNode === this.tail) {
|
|
@@ -480,15 +420,15 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
480
420
|
return true;
|
|
481
421
|
}
|
|
482
422
|
return false;
|
|
483
|
-
}
|
|
423
|
+
}
|
|
484
424
|
/**
|
|
485
425
|
* The function counts the number of occurrences of a given value in a linked list.
|
|
486
426
|
* @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
|
|
487
427
|
* @returns The count of occurrences of the given value in the linked list.
|
|
488
428
|
*/
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
429
|
+
countOccurrences(value) {
|
|
430
|
+
let count = 0;
|
|
431
|
+
let current = this.head;
|
|
492
432
|
while (current) {
|
|
493
433
|
if (current.val === value) {
|
|
494
434
|
count++;
|
|
@@ -496,7 +436,6 @@ var SinglyLinkedList = /** @class */ (function () {
|
|
|
496
436
|
current = current.next;
|
|
497
437
|
}
|
|
498
438
|
return count;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
}());
|
|
439
|
+
}
|
|
440
|
+
}
|
|
502
441
|
exports.SinglyLinkedList = SinglyLinkedList;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SkipLinkedList = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
return SkipLinkedList;
|
|
8
|
-
}());
|
|
4
|
+
class SkipLinkedList {
|
|
5
|
+
}
|
|
9
6
|
exports.SkipLinkedList = SkipLinkedList;
|
|
@@ -9,21 +9,20 @@ exports.MatrixNTI2D = void 0;
|
|
|
9
9
|
* @license MIT License
|
|
10
10
|
*/
|
|
11
11
|
// todo need to be improved
|
|
12
|
-
|
|
12
|
+
class MatrixNTI2D {
|
|
13
13
|
/**
|
|
14
14
|
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
15
15
|
* given initial value or 0 if not provided.
|
|
16
16
|
* @param options - An object containing the following properties:
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
this._matrix = new Array(row).fill(undefined).map(
|
|
18
|
+
constructor(options) {
|
|
19
|
+
const { row, col, initialVal } = options;
|
|
20
|
+
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
21
21
|
}
|
|
22
22
|
/* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
|
|
23
23
|
matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
|
|
24
|
-
|
|
24
|
+
toArray() {
|
|
25
25
|
return this._matrix;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
}());
|
|
26
|
+
}
|
|
27
|
+
}
|
|
29
28
|
exports.MatrixNTI2D = MatrixNTI2D;
|