data-structure-typed 1.19.3 → 1.19.5
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 +3 -2
- 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.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
31
4
|
/**
|
|
@@ -35,116 +8,80 @@ exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
|
35
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
36
9
|
* @license MIT License
|
|
37
10
|
*/
|
|
38
|
-
|
|
11
|
+
class DoublyLinkedListNode {
|
|
39
12
|
/**
|
|
40
13
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
41
14
|
* @param {T} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
42
15
|
* is defined as a generic type "T".
|
|
43
16
|
*/
|
|
44
|
-
|
|
17
|
+
constructor(val) {
|
|
45
18
|
this._val = val;
|
|
46
19
|
this._next = null;
|
|
47
20
|
this._prev = null;
|
|
48
21
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
});
|
|
69
|
-
Object.defineProperty(DoublyLinkedListNode.prototype, "prev", {
|
|
70
|
-
get: function () {
|
|
71
|
-
return this._prev;
|
|
72
|
-
},
|
|
73
|
-
set: function (value) {
|
|
74
|
-
this._prev = value;
|
|
75
|
-
},
|
|
76
|
-
enumerable: false,
|
|
77
|
-
configurable: true
|
|
78
|
-
});
|
|
79
|
-
return DoublyLinkedListNode;
|
|
80
|
-
}());
|
|
22
|
+
get val() {
|
|
23
|
+
return this._val;
|
|
24
|
+
}
|
|
25
|
+
set val(value) {
|
|
26
|
+
this._val = value;
|
|
27
|
+
}
|
|
28
|
+
get next() {
|
|
29
|
+
return this._next;
|
|
30
|
+
}
|
|
31
|
+
set next(value) {
|
|
32
|
+
this._next = value;
|
|
33
|
+
}
|
|
34
|
+
get prev() {
|
|
35
|
+
return this._prev;
|
|
36
|
+
}
|
|
37
|
+
set prev(value) {
|
|
38
|
+
this._prev = value;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
81
41
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
82
|
-
|
|
42
|
+
class DoublyLinkedList {
|
|
83
43
|
/**
|
|
84
44
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
85
45
|
*/
|
|
86
|
-
|
|
46
|
+
constructor() {
|
|
87
47
|
this._head = null;
|
|
88
48
|
this._tail = null;
|
|
89
49
|
this._length = 0;
|
|
90
50
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
this._tail = value;
|
|
107
|
-
},
|
|
108
|
-
enumerable: false,
|
|
109
|
-
configurable: true
|
|
110
|
-
});
|
|
111
|
-
Object.defineProperty(DoublyLinkedList.prototype, "length", {
|
|
112
|
-
get: function () {
|
|
113
|
-
return this._length;
|
|
114
|
-
},
|
|
115
|
-
enumerable: false,
|
|
116
|
-
configurable: true
|
|
117
|
-
});
|
|
51
|
+
get head() {
|
|
52
|
+
return this._head;
|
|
53
|
+
}
|
|
54
|
+
set head(value) {
|
|
55
|
+
this._head = value;
|
|
56
|
+
}
|
|
57
|
+
get tail() {
|
|
58
|
+
return this._tail;
|
|
59
|
+
}
|
|
60
|
+
set tail(value) {
|
|
61
|
+
this._tail = value;
|
|
62
|
+
}
|
|
63
|
+
get length() {
|
|
64
|
+
return this._length;
|
|
65
|
+
}
|
|
118
66
|
/**
|
|
119
67
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
120
68
|
* given array.
|
|
121
69
|
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
|
|
122
70
|
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
123
71
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
|
|
129
|
-
var item = data_1_1.value;
|
|
130
|
-
doublyLinkedList.push(item);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
134
|
-
finally {
|
|
135
|
-
try {
|
|
136
|
-
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
|
|
137
|
-
}
|
|
138
|
-
finally { if (e_1) throw e_1.error; }
|
|
72
|
+
static fromArray(data) {
|
|
73
|
+
const doublyLinkedList = new DoublyLinkedList();
|
|
74
|
+
for (const item of data) {
|
|
75
|
+
doublyLinkedList.push(item);
|
|
139
76
|
}
|
|
140
77
|
return doublyLinkedList;
|
|
141
|
-
}
|
|
78
|
+
}
|
|
142
79
|
/**
|
|
143
80
|
* The push function adds a new node with the given value to the end of the doubly linked list.
|
|
144
81
|
* @param {T} val - The value to be added to the linked list.
|
|
145
82
|
*/
|
|
146
|
-
|
|
147
|
-
|
|
83
|
+
push(val) {
|
|
84
|
+
const newNode = new DoublyLinkedListNode(val);
|
|
148
85
|
if (!this.head) {
|
|
149
86
|
this.head = newNode;
|
|
150
87
|
this.tail = newNode;
|
|
@@ -155,16 +92,16 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
155
92
|
this.tail = newNode;
|
|
156
93
|
}
|
|
157
94
|
this._length++;
|
|
158
|
-
}
|
|
95
|
+
}
|
|
159
96
|
/**
|
|
160
97
|
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
|
|
161
98
|
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
162
99
|
* list is empty, it returns null.
|
|
163
100
|
*/
|
|
164
|
-
|
|
101
|
+
pop() {
|
|
165
102
|
if (!this.tail)
|
|
166
103
|
return null;
|
|
167
|
-
|
|
104
|
+
const removedNode = this.tail;
|
|
168
105
|
if (this.head === this.tail) {
|
|
169
106
|
this.head = null;
|
|
170
107
|
this.tail = null;
|
|
@@ -175,16 +112,16 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
175
112
|
}
|
|
176
113
|
this._length--;
|
|
177
114
|
return removedNode.val;
|
|
178
|
-
}
|
|
115
|
+
}
|
|
179
116
|
/**
|
|
180
117
|
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
|
|
181
118
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
182
119
|
* list.
|
|
183
120
|
*/
|
|
184
|
-
|
|
121
|
+
shift() {
|
|
185
122
|
if (!this.head)
|
|
186
123
|
return null;
|
|
187
|
-
|
|
124
|
+
const removedNode = this.head;
|
|
188
125
|
if (this.head === this.tail) {
|
|
189
126
|
this.head = null;
|
|
190
127
|
this.tail = null;
|
|
@@ -195,14 +132,14 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
195
132
|
}
|
|
196
133
|
this._length--;
|
|
197
134
|
return removedNode.val;
|
|
198
|
-
}
|
|
135
|
+
}
|
|
199
136
|
/**
|
|
200
137
|
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
|
|
201
138
|
* @param {T} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
|
|
202
139
|
* doubly linked list.
|
|
203
140
|
*/
|
|
204
|
-
|
|
205
|
-
|
|
141
|
+
unshift(val) {
|
|
142
|
+
const newNode = new DoublyLinkedListNode(val);
|
|
206
143
|
if (!this.head) {
|
|
207
144
|
this.head = newNode;
|
|
208
145
|
this.tail = newNode;
|
|
@@ -213,7 +150,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
213
150
|
this.head = newNode;
|
|
214
151
|
}
|
|
215
152
|
this._length++;
|
|
216
|
-
}
|
|
153
|
+
}
|
|
217
154
|
/**
|
|
218
155
|
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
|
|
219
156
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -221,15 +158,15 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
221
158
|
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
222
159
|
* or the linked list is empty, it will return null.
|
|
223
160
|
*/
|
|
224
|
-
|
|
161
|
+
getAt(index) {
|
|
225
162
|
if (index < 0 || index >= this.length)
|
|
226
163
|
return null;
|
|
227
|
-
|
|
228
|
-
for (
|
|
164
|
+
let current = this.head;
|
|
165
|
+
for (let i = 0; i < index; i++) {
|
|
229
166
|
current = current.next;
|
|
230
167
|
}
|
|
231
168
|
return current.val;
|
|
232
|
-
}
|
|
169
|
+
}
|
|
233
170
|
/**
|
|
234
171
|
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
|
|
235
172
|
* range.
|
|
@@ -238,15 +175,15 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
238
175
|
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<T>` object if the index is within the
|
|
239
176
|
* valid range of the linked list, otherwise it returns `null`.
|
|
240
177
|
*/
|
|
241
|
-
|
|
178
|
+
getNodeAt(index) {
|
|
242
179
|
if (index < 0 || index >= this.length)
|
|
243
180
|
return null;
|
|
244
|
-
|
|
245
|
-
for (
|
|
181
|
+
let current = this.head;
|
|
182
|
+
for (let i = 0; i < index; i++) {
|
|
246
183
|
current = current.next;
|
|
247
184
|
}
|
|
248
185
|
return current;
|
|
249
|
-
}
|
|
186
|
+
}
|
|
250
187
|
/**
|
|
251
188
|
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
|
|
252
189
|
* node if found, otherwise it returns null.
|
|
@@ -254,8 +191,8 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
254
191
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<T>` if a node with the specified value `val`
|
|
255
192
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
256
193
|
*/
|
|
257
|
-
|
|
258
|
-
|
|
194
|
+
findNode(val) {
|
|
195
|
+
let current = this.head;
|
|
259
196
|
while (current) {
|
|
260
197
|
if (current.val === val) {
|
|
261
198
|
return current;
|
|
@@ -263,7 +200,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
263
200
|
current = current.next;
|
|
264
201
|
}
|
|
265
202
|
return null;
|
|
266
|
-
}
|
|
203
|
+
}
|
|
267
204
|
/**
|
|
268
205
|
* The `insert` function inserts a value at a specified index in a doubly linked list.
|
|
269
206
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
@@ -273,7 +210,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
273
210
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
274
211
|
* if the index is out of bounds.
|
|
275
212
|
*/
|
|
276
|
-
|
|
213
|
+
insertAt(index, val) {
|
|
277
214
|
if (index < 0 || index > this.length)
|
|
278
215
|
return false;
|
|
279
216
|
if (index === 0) {
|
|
@@ -284,16 +221,16 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
284
221
|
this.push(val);
|
|
285
222
|
return true;
|
|
286
223
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
224
|
+
const newNode = new DoublyLinkedListNode(val);
|
|
225
|
+
const prevNode = this.getNodeAt(index - 1);
|
|
226
|
+
const nextNode = prevNode.next;
|
|
290
227
|
newNode.prev = prevNode;
|
|
291
228
|
newNode.next = nextNode;
|
|
292
229
|
prevNode.next = newNode;
|
|
293
230
|
nextNode.prev = newNode;
|
|
294
231
|
this._length++;
|
|
295
232
|
return true;
|
|
296
|
-
}
|
|
233
|
+
}
|
|
297
234
|
/**
|
|
298
235
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
299
236
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -301,21 +238,21 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
301
238
|
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
|
|
302
239
|
* bounds.
|
|
303
240
|
*/
|
|
304
|
-
|
|
241
|
+
deleteAt(index) {
|
|
305
242
|
if (index < 0 || index >= this.length)
|
|
306
243
|
return null;
|
|
307
244
|
if (index === 0)
|
|
308
245
|
return this.shift();
|
|
309
246
|
if (index === this.length - 1)
|
|
310
247
|
return this.pop();
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
248
|
+
const removedNode = this.getNodeAt(index);
|
|
249
|
+
const prevNode = removedNode.prev;
|
|
250
|
+
const nextNode = removedNode.next;
|
|
314
251
|
prevNode.next = nextNode;
|
|
315
252
|
nextNode.prev = prevNode;
|
|
316
253
|
this._length--;
|
|
317
254
|
return removedNode.val;
|
|
318
|
-
}
|
|
255
|
+
}
|
|
319
256
|
/**
|
|
320
257
|
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
321
258
|
* @param {T | DoublyLinkedListNode<T>} valOrNode - The `valOrNode` parameter can accept either a value of type `T` or
|
|
@@ -323,8 +260,8 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
323
260
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
|
|
324
261
|
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
|
|
325
262
|
*/
|
|
326
|
-
|
|
327
|
-
|
|
263
|
+
delete(valOrNode) {
|
|
264
|
+
let node;
|
|
328
265
|
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
329
266
|
node = valOrNode;
|
|
330
267
|
}
|
|
@@ -339,8 +276,8 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
339
276
|
this.pop();
|
|
340
277
|
}
|
|
341
278
|
else {
|
|
342
|
-
|
|
343
|
-
|
|
279
|
+
const prevNode = node.prev;
|
|
280
|
+
const nextNode = node.next;
|
|
344
281
|
prevNode.next = nextNode;
|
|
345
282
|
nextNode.prev = prevNode;
|
|
346
283
|
this._length--;
|
|
@@ -348,28 +285,28 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
348
285
|
return true;
|
|
349
286
|
}
|
|
350
287
|
return false;
|
|
351
|
-
}
|
|
288
|
+
}
|
|
352
289
|
/**
|
|
353
290
|
* The `toArray` function converts a linked list into an array.
|
|
354
291
|
* @returns The `toArray()` method is returning an array of type `T[]`.
|
|
355
292
|
*/
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
293
|
+
toArray() {
|
|
294
|
+
const array = [];
|
|
295
|
+
let current = this.head;
|
|
359
296
|
while (current) {
|
|
360
297
|
array.push(current.val);
|
|
361
298
|
current = current.next;
|
|
362
299
|
}
|
|
363
300
|
return array;
|
|
364
|
-
}
|
|
301
|
+
}
|
|
365
302
|
/**
|
|
366
303
|
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
|
|
367
304
|
*/
|
|
368
|
-
|
|
305
|
+
clear() {
|
|
369
306
|
this._head = null;
|
|
370
307
|
this._tail = null;
|
|
371
308
|
this._length = 0;
|
|
372
|
-
}
|
|
309
|
+
}
|
|
373
310
|
/**
|
|
374
311
|
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
|
|
375
312
|
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
|
|
@@ -377,8 +314,8 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
377
314
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
378
315
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
379
316
|
*/
|
|
380
|
-
|
|
381
|
-
|
|
317
|
+
find(callback) {
|
|
318
|
+
let current = this.head;
|
|
382
319
|
while (current) {
|
|
383
320
|
if (callback(current.val)) {
|
|
384
321
|
return current.val;
|
|
@@ -386,7 +323,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
386
323
|
current = current.next;
|
|
387
324
|
}
|
|
388
325
|
return null;
|
|
389
|
-
}
|
|
326
|
+
}
|
|
390
327
|
/**
|
|
391
328
|
* The function returns the index of the first occurrence of a given value in a linked list.
|
|
392
329
|
* @param {T} val - The parameter `val` is of type `T`, which means it can be any data type. It represents the value
|
|
@@ -394,9 +331,9 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
394
331
|
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
|
|
395
332
|
* list. If the value is not found, it returns -1.
|
|
396
333
|
*/
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
334
|
+
indexOf(val) {
|
|
335
|
+
let index = 0;
|
|
336
|
+
let current = this.head;
|
|
400
337
|
while (current) {
|
|
401
338
|
if (current.val === val) {
|
|
402
339
|
return index;
|
|
@@ -405,7 +342,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
405
342
|
current = current.next;
|
|
406
343
|
}
|
|
407
344
|
return -1;
|
|
408
|
-
}
|
|
345
|
+
}
|
|
409
346
|
/**
|
|
410
347
|
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last
|
|
411
348
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
@@ -414,8 +351,8 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
414
351
|
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
|
|
415
352
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
416
353
|
*/
|
|
417
|
-
|
|
418
|
-
|
|
354
|
+
findLast(callback) {
|
|
355
|
+
let current = this.tail;
|
|
419
356
|
while (current) {
|
|
420
357
|
if (callback(current.val)) {
|
|
421
358
|
return current.val;
|
|
@@ -423,48 +360,47 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
423
360
|
current = current.prev;
|
|
424
361
|
}
|
|
425
362
|
return null;
|
|
426
|
-
}
|
|
363
|
+
}
|
|
427
364
|
/**
|
|
428
365
|
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
|
|
429
366
|
* @returns The `toArrayReverse()` function returns an array of type `T[]`.
|
|
430
367
|
*/
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
368
|
+
toArrayReverse() {
|
|
369
|
+
const array = [];
|
|
370
|
+
let current = this.tail;
|
|
434
371
|
while (current) {
|
|
435
372
|
array.push(current.val);
|
|
436
373
|
current = current.prev;
|
|
437
374
|
}
|
|
438
375
|
return array;
|
|
439
|
-
}
|
|
376
|
+
}
|
|
440
377
|
/**
|
|
441
378
|
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
442
379
|
*/
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
_a = __read([this.tail, this.head], 2), this.head = _a[0], this.tail = _a[1];
|
|
380
|
+
reverse() {
|
|
381
|
+
let current = this.head;
|
|
382
|
+
[this.head, this.tail] = [this.tail, this.head];
|
|
447
383
|
while (current) {
|
|
448
|
-
|
|
449
|
-
|
|
384
|
+
const next = current.next;
|
|
385
|
+
[current.prev, current.next] = [current.next, current.prev];
|
|
450
386
|
current = next;
|
|
451
387
|
}
|
|
452
|
-
}
|
|
388
|
+
}
|
|
453
389
|
/**
|
|
454
390
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
455
391
|
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
456
392
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
457
393
|
* current node in the linked list.
|
|
458
394
|
*/
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
395
|
+
forEach(callback) {
|
|
396
|
+
let current = this.head;
|
|
397
|
+
let index = 0;
|
|
462
398
|
while (current) {
|
|
463
399
|
callback(current.val, index);
|
|
464
400
|
current = current.next;
|
|
465
401
|
index++;
|
|
466
402
|
}
|
|
467
|
-
}
|
|
403
|
+
}
|
|
468
404
|
/**
|
|
469
405
|
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
|
|
470
406
|
* DoublyLinkedList with the transformed values.
|
|
@@ -473,15 +409,15 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
473
409
|
* DoublyLinkedList).
|
|
474
410
|
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
|
|
475
411
|
*/
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
412
|
+
map(callback) {
|
|
413
|
+
const mappedList = new DoublyLinkedList();
|
|
414
|
+
let current = this.head;
|
|
479
415
|
while (current) {
|
|
480
416
|
mappedList.push(callback(current.val));
|
|
481
417
|
current = current.next;
|
|
482
418
|
}
|
|
483
419
|
return mappedList;
|
|
484
|
-
}
|
|
420
|
+
}
|
|
485
421
|
/**
|
|
486
422
|
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
|
|
487
423
|
* elements that satisfy the given callback function.
|
|
@@ -489,9 +425,9 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
489
425
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
490
426
|
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
|
|
491
427
|
*/
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
428
|
+
filter(callback) {
|
|
429
|
+
const filteredList = new DoublyLinkedList();
|
|
430
|
+
let current = this.head;
|
|
495
431
|
while (current) {
|
|
496
432
|
if (callback(current.val)) {
|
|
497
433
|
filteredList.push(current.val);
|
|
@@ -499,7 +435,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
499
435
|
current = current.next;
|
|
500
436
|
}
|
|
501
437
|
return filteredList;
|
|
502
|
-
}
|
|
438
|
+
}
|
|
503
439
|
/**
|
|
504
440
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
505
441
|
* single value.
|
|
@@ -510,15 +446,15 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
510
446
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
511
447
|
* elements in the linked list.
|
|
512
448
|
*/
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
449
|
+
reduce(callback, initialValue) {
|
|
450
|
+
let accumulator = initialValue;
|
|
451
|
+
let current = this.head;
|
|
516
452
|
while (current) {
|
|
517
453
|
accumulator = callback(accumulator, current.val);
|
|
518
454
|
current = current.next;
|
|
519
455
|
}
|
|
520
456
|
return accumulator;
|
|
521
|
-
}
|
|
457
|
+
}
|
|
522
458
|
/**
|
|
523
459
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
524
460
|
* @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -528,8 +464,8 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
528
464
|
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
529
465
|
* existing value or node is not found in the doubly linked list.
|
|
530
466
|
*/
|
|
531
|
-
|
|
532
|
-
|
|
467
|
+
insertAfter(existingValueOrNode, newValue) {
|
|
468
|
+
let existingNode;
|
|
533
469
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
534
470
|
existingNode = existingValueOrNode;
|
|
535
471
|
}
|
|
@@ -537,7 +473,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
537
473
|
existingNode = this.findNode(existingValueOrNode);
|
|
538
474
|
}
|
|
539
475
|
if (existingNode) {
|
|
540
|
-
|
|
476
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
541
477
|
newNode.next = existingNode.next;
|
|
542
478
|
if (existingNode.next) {
|
|
543
479
|
existingNode.next.prev = newNode;
|
|
@@ -551,7 +487,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
551
487
|
return true;
|
|
552
488
|
}
|
|
553
489
|
return false;
|
|
554
|
-
}
|
|
490
|
+
}
|
|
555
491
|
/**
|
|
556
492
|
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
557
493
|
* @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -562,8 +498,8 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
562
498
|
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
563
499
|
* insertion fails.
|
|
564
500
|
*/
|
|
565
|
-
|
|
566
|
-
|
|
501
|
+
insertBefore(existingValueOrNode, newValue) {
|
|
502
|
+
let existingNode;
|
|
567
503
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
568
504
|
existingNode = existingValueOrNode;
|
|
569
505
|
}
|
|
@@ -571,7 +507,7 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
571
507
|
existingNode = this.findNode(existingValueOrNode);
|
|
572
508
|
}
|
|
573
509
|
if (existingNode) {
|
|
574
|
-
|
|
510
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
575
511
|
newNode.prev = existingNode.prev;
|
|
576
512
|
if (existingNode.prev) {
|
|
577
513
|
existingNode.prev.next = newNode;
|
|
@@ -585,7 +521,6 @@ var DoublyLinkedList = /** @class */ (function () {
|
|
|
585
521
|
return true;
|
|
586
522
|
}
|
|
587
523
|
return false;
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
}());
|
|
524
|
+
}
|
|
525
|
+
}
|
|
591
526
|
exports.DoublyLinkedList = DoublyLinkedList;
|