@stimulcross/ds-doubly-linked-list 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Stimul Cross
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Doubly Linked List
2
+
3
+ A doubly linked list implementation on TypeScript.
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Doubly linked list implementation.
3
+ *
4
+ * @template T The type of elements stored in the list. Defaults to `unknown`.
5
+ */
6
+ export declare class DoublyLinkedList<T = unknown> {
7
+ private _head;
8
+ private _tail;
9
+ private _size;
10
+ /**
11
+ * Creates a doubly linked list from an iterable.
12
+ *
13
+ * Complexity: **O(n)**, where `n` is the number of elements in `iterable`.
14
+ *
15
+ * @param iterable An iterable of values to create a doubly linked list from.
16
+ *
17
+ * @returns A new doubly linked list instance.
18
+ */
19
+ static from<T>(iterable: Iterable<T>): DoublyLinkedList<T>;
20
+ /**
21
+ * The number of elements in the doubly linked list.
22
+ *
23
+ * Complexity: **O(1)**.
24
+ */
25
+ get size(): number;
26
+ /**
27
+ * Checks if the doubly linked list is empty.
28
+ *
29
+ * Complexity: **O(1)**.
30
+ */
31
+ get isEmpty(): boolean;
32
+ /**
33
+ * The first item in the doubly linked list. `null` if the list is empty.
34
+ *
35
+ * Complexity: **O(1)**.
36
+ */
37
+ get head(): T | null;
38
+ /**
39
+ * The last item in the doubly linked list. `null` if the list is empty.
40
+ *
41
+ * Complexity: **O(1)**.
42
+ */
43
+ get tail(): T | null;
44
+ /**
45
+ * Adds a value to the end of the doubly linked list.
46
+ *
47
+ * Complexity: **O(1)**.
48
+ *
49
+ * @param value The value to add.
50
+ */
51
+ push(value: T): void;
52
+ /**
53
+ * Removes and returns the value from the end of the doubly linked list.
54
+ *
55
+ * Complexity: **O(1)**.
56
+ *
57
+ * @returns The value removed from the end of the doubly linked list, or `null` if the list is empty.
58
+ */
59
+ pop(): T | null;
60
+ /**
61
+ * Adds a value to the beginning of the doubly linked list.
62
+ *
63
+ * Complexity: **O(1)**.
64
+ *
65
+ * @param value The value to add.
66
+ */
67
+ unshift(value: T): void;
68
+ /**
69
+ * Removes and returns the value from the beginning of the doubly linked list.
70
+ *
71
+ * Complexity: **O(1)**.
72
+ *
73
+ * @returns The value removed from the beginning of the doubly linked list, or `null` if the list is empty.
74
+ */
75
+ shift(): T | null;
76
+ /**
77
+ * Removes all elements from the doubly linked list.
78
+ *
79
+ * Complexity: **O(1)**.
80
+ */
81
+ clear(): void;
82
+ /**
83
+ * Checks if a value is in the doubly linked list.
84
+ *
85
+ * @param value The value to check.
86
+ *
87
+ * @returns `true` if the value is in the list, `false` otherwise.
88
+ */
89
+ contains(value: T): boolean;
90
+ /**
91
+ * Returns the index of a value in the doubly linked list.
92
+ *
93
+ * @param value The value to get the index of.
94
+ *
95
+ * @returns The index of the value, or `-1` if the value is not found.
96
+ */
97
+ indexOf(value: T): number;
98
+ /**
99
+ * Inserts a value at the specified index.
100
+ *
101
+ * Complexity: **O(n)**.
102
+ *
103
+ * @param index The index at which to insert the value.
104
+ * @param value The value to insert.
105
+ *
106
+ * @returns `true` if the value was inserted, `false` otherwise.
107
+ */
108
+ insert(index: number, value: T): boolean;
109
+ /**
110
+ * Removes a value from the doubly linked list.
111
+ *
112
+ * Complexity: **O(n)**.
113
+ *
114
+ * @param value The value to remove.
115
+ *
116
+ * @returns `true` if the value was removed, `false` otherwise.
117
+ */
118
+ remove(value: T): boolean;
119
+ /**
120
+ * Removes a value from the doubly linked list at the specified index.
121
+ *
122
+ * Complexity: **O(n)**.
123
+ *
124
+ * @param index The index of the value to remove.
125
+ *
126
+ * @returns The value removed from the list, or `null` if the list is empty or index is out of bounds.
127
+ */
128
+ removeByIndex(index: number): T | null;
129
+ /**
130
+ * Finds the value in the doubly linked list.
131
+ *
132
+ * Complexity: **O(n)**.
133
+ *
134
+ * @param predicate The predicate function to test each value.
135
+ *
136
+ * @returns The value found in the list, or `null` if the value is not found.
137
+ */
138
+ find(predicate: (value: T) => boolean): T | null;
139
+ /**
140
+ * Converts the doubly linked list to an array.
141
+ *
142
+ * Complexity: **O(n)**.
143
+ */
144
+ toArray(): T[];
145
+ /**
146
+ * Converts the doubly linked list to an array in reverse order.
147
+ *
148
+ * Complexity: **O(n)**.
149
+ */
150
+ toReversedArray(): T[];
151
+ /**
152
+ * Returns an iterator that iterates over the values in the doubly linked list.
153
+ */
154
+ [Symbol.iterator](): IterableIterator<T>;
155
+ /**
156
+ * Returns an iterator that iterates over the values in the doubly linked list in reverse order.
157
+ */
158
+ reverse(): IterableIterator<T>;
159
+ private _getNode;
160
+ }
161
+ //# sourceMappingURL=doubly-linked-list.d.ts.map
@@ -0,0 +1,364 @@
1
+ import { DoublyLinkedNode } from './doubly-linked-node.js';
2
+ /**
3
+ * Doubly linked list implementation.
4
+ *
5
+ * @template T The type of elements stored in the list. Defaults to `unknown`.
6
+ */
7
+ export class DoublyLinkedList {
8
+ _head = null;
9
+ _tail = null;
10
+ _size = 0;
11
+ /**
12
+ * Creates a doubly linked list from an iterable.
13
+ *
14
+ * Complexity: **O(n)**, where `n` is the number of elements in `iterable`.
15
+ *
16
+ * @param iterable An iterable of values to create a doubly linked list from.
17
+ *
18
+ * @returns A new doubly linked list instance.
19
+ */
20
+ static from(iterable) {
21
+ const list = new DoublyLinkedList();
22
+ for (const value of iterable) {
23
+ list.push(value);
24
+ }
25
+ return list;
26
+ }
27
+ /**
28
+ * The number of elements in the doubly linked list.
29
+ *
30
+ * Complexity: **O(1)**.
31
+ */
32
+ get size() {
33
+ return this._size;
34
+ }
35
+ /**
36
+ * Checks if the doubly linked list is empty.
37
+ *
38
+ * Complexity: **O(1)**.
39
+ */
40
+ get isEmpty() {
41
+ return this._size === 0;
42
+ }
43
+ /**
44
+ * The first item in the doubly linked list. `null` if the list is empty.
45
+ *
46
+ * Complexity: **O(1)**.
47
+ */
48
+ get head() {
49
+ return this._head?.value ?? null;
50
+ }
51
+ /**
52
+ * The last item in the doubly linked list. `null` if the list is empty.
53
+ *
54
+ * Complexity: **O(1)**.
55
+ */
56
+ get tail() {
57
+ return this._tail?.value ?? null;
58
+ }
59
+ /**
60
+ * Adds a value to the end of the doubly linked list.
61
+ *
62
+ * Complexity: **O(1)**.
63
+ *
64
+ * @param value The value to add.
65
+ */
66
+ push(value) {
67
+ const node = new DoublyLinkedNode(value);
68
+ if (this._tail) {
69
+ node.prev = this._tail;
70
+ this._tail.next = node;
71
+ this._tail = node;
72
+ }
73
+ else {
74
+ this._head = node;
75
+ this._tail = node;
76
+ }
77
+ this._size += 1;
78
+ }
79
+ /**
80
+ * Removes and returns the value from the end of the doubly linked list.
81
+ *
82
+ * Complexity: **O(1)**.
83
+ *
84
+ * @returns The value removed from the end of the doubly linked list, or `null` if the list is empty.
85
+ */
86
+ pop() {
87
+ if (!this._tail) {
88
+ return null;
89
+ }
90
+ const value = this._tail.value;
91
+ if (this._head === this._tail) {
92
+ this._head = null;
93
+ this._tail = null;
94
+ }
95
+ else {
96
+ this._tail = this._tail.prev;
97
+ this._tail.next = null;
98
+ }
99
+ this._size -= 1;
100
+ return value;
101
+ }
102
+ /**
103
+ * Adds a value to the beginning of the doubly linked list.
104
+ *
105
+ * Complexity: **O(1)**.
106
+ *
107
+ * @param value The value to add.
108
+ */
109
+ unshift(value) {
110
+ const node = new DoublyLinkedNode(value);
111
+ if (this._head) {
112
+ node.next = this._head;
113
+ this._head.prev = node;
114
+ this._head = node;
115
+ }
116
+ else {
117
+ this._head = node;
118
+ this._tail = node;
119
+ }
120
+ this._size += 1;
121
+ }
122
+ /**
123
+ * Removes and returns the value from the beginning of the doubly linked list.
124
+ *
125
+ * Complexity: **O(1)**.
126
+ *
127
+ * @returns The value removed from the beginning of the doubly linked list, or `null` if the list is empty.
128
+ */
129
+ shift() {
130
+ if (!this._head) {
131
+ return null;
132
+ }
133
+ const value = this._head.value;
134
+ if (this._head === this._tail) {
135
+ this._head = null;
136
+ this._tail = null;
137
+ }
138
+ else {
139
+ this._head = this._head.next;
140
+ this._head.prev = null;
141
+ }
142
+ this._size -= 1;
143
+ return value;
144
+ }
145
+ /**
146
+ * Removes all elements from the doubly linked list.
147
+ *
148
+ * Complexity: **O(1)**.
149
+ */
150
+ clear() {
151
+ this._head = null;
152
+ this._tail = null;
153
+ this._size = 0;
154
+ }
155
+ /**
156
+ * Checks if a value is in the doubly linked list.
157
+ *
158
+ * @param value The value to check.
159
+ *
160
+ * @returns `true` if the value is in the list, `false` otherwise.
161
+ */
162
+ contains(value) {
163
+ for (const item of this) {
164
+ if (item === value) {
165
+ return true;
166
+ }
167
+ }
168
+ return false;
169
+ }
170
+ /**
171
+ * Returns the index of a value in the doubly linked list.
172
+ *
173
+ * @param value The value to get the index of.
174
+ *
175
+ * @returns The index of the value, or `-1` if the value is not found.
176
+ */
177
+ indexOf(value) {
178
+ let index = 0;
179
+ for (const item of this) {
180
+ if (item === value) {
181
+ return index;
182
+ }
183
+ index += 1;
184
+ }
185
+ return -1;
186
+ }
187
+ /**
188
+ * Inserts a value at the specified index.
189
+ *
190
+ * Complexity: **O(n)**.
191
+ *
192
+ * @param index The index at which to insert the value.
193
+ * @param value The value to insert.
194
+ *
195
+ * @returns `true` if the value was inserted, `false` otherwise.
196
+ */
197
+ insert(index, value) {
198
+ if (index < 0 || index > this._size) {
199
+ return false;
200
+ }
201
+ if (index === 0) {
202
+ this.unshift(value);
203
+ return true;
204
+ }
205
+ if (index === this._size) {
206
+ this.push(value);
207
+ return true;
208
+ }
209
+ const nextNode = this._getNode(index);
210
+ const prevNode = nextNode.prev;
211
+ const node = new DoublyLinkedNode(value);
212
+ node.next = nextNode;
213
+ node.prev = prevNode;
214
+ nextNode.prev = node;
215
+ prevNode.next = node;
216
+ this._size += 1;
217
+ return true;
218
+ }
219
+ /**
220
+ * Removes a value from the doubly linked list.
221
+ *
222
+ * Complexity: **O(n)**.
223
+ *
224
+ * @param value The value to remove.
225
+ *
226
+ * @returns `true` if the value was removed, `false` otherwise.
227
+ */
228
+ remove(value) {
229
+ let current = this._head;
230
+ while (current) {
231
+ if (current.value === value) {
232
+ const prevNode = current.prev;
233
+ const nextNode = current.next;
234
+ if (prevNode) {
235
+ prevNode.next = nextNode;
236
+ }
237
+ else {
238
+ this._head = nextNode;
239
+ }
240
+ if (nextNode) {
241
+ nextNode.prev = prevNode;
242
+ }
243
+ else {
244
+ this._tail = prevNode;
245
+ }
246
+ current.prev = null;
247
+ current.next = null;
248
+ this._size -= 1;
249
+ return true;
250
+ }
251
+ current = current.next;
252
+ }
253
+ return false;
254
+ }
255
+ /**
256
+ * Removes a value from the doubly linked list at the specified index.
257
+ *
258
+ * Complexity: **O(n)**.
259
+ *
260
+ * @param index The index of the value to remove.
261
+ *
262
+ * @returns The value removed from the list, or `null` if the list is empty or index is out of bounds.
263
+ */
264
+ removeByIndex(index) {
265
+ if (index < 0 || index >= this._size) {
266
+ return null;
267
+ }
268
+ if (index === 0) {
269
+ return this.shift();
270
+ }
271
+ if (index === this._size - 1) {
272
+ return this.pop();
273
+ }
274
+ const node = this._getNode(index);
275
+ const prevNode = node.prev;
276
+ const nextNode = node.next;
277
+ prevNode.next = nextNode;
278
+ nextNode.prev = prevNode;
279
+ node.prev = null;
280
+ node.next = null;
281
+ this._size -= 1;
282
+ return node.value;
283
+ }
284
+ /**
285
+ * Finds the value in the doubly linked list.
286
+ *
287
+ * Complexity: **O(n)**.
288
+ *
289
+ * @param predicate The predicate function to test each value.
290
+ *
291
+ * @returns The value found in the list, or `null` if the value is not found.
292
+ */
293
+ find(predicate) {
294
+ let current = this._head;
295
+ while (current) {
296
+ if (predicate(current.value)) {
297
+ return current.value;
298
+ }
299
+ current = current.next;
300
+ }
301
+ return null;
302
+ }
303
+ /**
304
+ * Converts the doubly linked list to an array.
305
+ *
306
+ * Complexity: **O(n)**.
307
+ */
308
+ toArray() {
309
+ return [...this];
310
+ }
311
+ /**
312
+ * Converts the doubly linked list to an array in reverse order.
313
+ *
314
+ * Complexity: **O(n)**.
315
+ */
316
+ toReversedArray() {
317
+ return [...this.reverse()];
318
+ }
319
+ /**
320
+ * Returns an iterator that iterates over the values in the doubly linked list.
321
+ */
322
+ *[Symbol.iterator]() {
323
+ let currentNode = this._head;
324
+ while (currentNode) {
325
+ yield currentNode.value;
326
+ currentNode = currentNode.next;
327
+ }
328
+ }
329
+ /**
330
+ * Returns an iterator that iterates over the values in the doubly linked list in reverse order.
331
+ */
332
+ *reverse() {
333
+ let currentNode = this._tail;
334
+ while (currentNode) {
335
+ yield currentNode.value;
336
+ currentNode = currentNode.prev;
337
+ }
338
+ }
339
+ _getNode(index) {
340
+ if (index < 0 || index >= this._size) {
341
+ return null;
342
+ }
343
+ let currentNode;
344
+ let currentIdx;
345
+ if (index < this._size / 2) {
346
+ currentNode = this._head;
347
+ currentIdx = 0;
348
+ while (currentIdx < index) {
349
+ currentNode = currentNode.next;
350
+ currentIdx += 1;
351
+ }
352
+ }
353
+ else {
354
+ currentNode = this._tail;
355
+ currentIdx = this._size - 1;
356
+ while (currentIdx > index) {
357
+ currentNode = currentNode.prev;
358
+ currentIdx -= 1;
359
+ }
360
+ }
361
+ return currentNode;
362
+ }
363
+ }
364
+ //# sourceMappingURL=doubly-linked-list.js.map
@@ -0,0 +1,8 @@
1
+ /** @internal */
2
+ export declare class DoublyLinkedNode<T = unknown> {
3
+ value: T;
4
+ prev: DoublyLinkedNode<T> | null;
5
+ next: DoublyLinkedNode<T> | null;
6
+ constructor(value: T);
7
+ }
8
+ //# sourceMappingURL=doubly-linked-node.d.ts.map
@@ -0,0 +1,10 @@
1
+ /** @internal */
2
+ export class DoublyLinkedNode {
3
+ value;
4
+ prev = null;
5
+ next = null;
6
+ constructor(value) {
7
+ this.value = value;
8
+ }
9
+ }
10
+ //# sourceMappingURL=doubly-linked-node.js.map
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { DoublyLinkedList } from './doubly-linked-list.js';
2
+ //# sourceMappingURL=index.d.ts.map
package/lib/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { DoublyLinkedList } from './doubly-linked-list.js';
2
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@stimulcross/ds-doubly-linked-list",
3
+ "version": "0.0.1",
4
+ "description": "Doubly linked list implementation for TypeScript.",
5
+ "keywords": [
6
+ "data-structure",
7
+ "doubly-linked-list",
8
+ "linked-list"
9
+ ],
10
+ "author": "StimulCross <stimulcross@gmail.com>",
11
+ "license": "MIT",
12
+ "homepage": "https://github.com/StimulCross/data-structures/tree/main/packages/doubly-linked-list#readme",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/StimulCross/data-structures.git",
16
+ "directory": "packages/doubly-linked-list"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "engines": {
22
+ "node": ">=20"
23
+ },
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "main": "./lib/index.js",
27
+ "types": "./lib/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./lib/index.d.ts",
31
+ "default": "./lib/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "LICENSE",
36
+ "README.md",
37
+ "lib",
38
+ "!lib/**/*.d.ts.map",
39
+ "!lib/**/*.js.map"
40
+ ],
41
+ "devDependencies": {
42
+ "eslint-config": "0.1.0",
43
+ "typescript-config": "0.1.0"
44
+ },
45
+ "scripts": {
46
+ "build": "tsc -p tsconfig.json",
47
+ "rebuild": "pnpm run clean && pnpm run build",
48
+ "clean": "del-cli lib *.tsbuildinfo",
49
+ "lint": "eslint src tests",
50
+ "lint:fix": "eslint src tests --fix",
51
+ "format:check": "prettier --check src",
52
+ "format:fix": "prettier --write src",
53
+ "check-types": "tsc -p tsconfig.json --noEmit",
54
+ "preversion": "pnpm run rebuild && pnpm run format:check && pnpm run lint"
55
+ }
56
+ }