min-heap-typed 1.39.6 → 1.40.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/dist/data-structures/binary-tree/avl-tree.js +0 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -20
- package/dist/data-structures/binary-tree/binary-tree.js +8 -29
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/data-structures/binary-tree/rb-tree.js +1 -7
- package/dist/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/data-structures/graph/abstract-graph.js +4 -43
- package/dist/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/data-structures/graph/directed-graph.js +2 -20
- package/dist/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/data-structures/graph/map-graph.js +2 -20
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/data-structures/graph/undirected-graph.js +1 -14
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/data-structures/hash/hash-table.js +0 -9
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +17 -42
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/rb-tree.ts +13 -21
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -8,17 +8,26 @@
|
|
|
8
8
|
import type {Comparator, DFSOrderPattern} from '../../types';
|
|
9
9
|
|
|
10
10
|
export class Heap<E = any> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
15
|
-
this.comparator = options.comparator;
|
|
11
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
12
|
+
this._comparator = options.comparator;
|
|
16
13
|
if (options.nodes && options.nodes.length > 0) {
|
|
17
|
-
this.
|
|
14
|
+
this._nodes = options.nodes;
|
|
18
15
|
this.fix();
|
|
19
16
|
}
|
|
20
17
|
}
|
|
21
18
|
|
|
19
|
+
protected _nodes: E[] = [];
|
|
20
|
+
|
|
21
|
+
get nodes(): E[] {
|
|
22
|
+
return this._nodes;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected _comparator: Comparator<E>;
|
|
26
|
+
|
|
27
|
+
get comparator(): Comparator<E> {
|
|
28
|
+
return this._comparator;
|
|
29
|
+
}
|
|
30
|
+
|
|
22
31
|
/**
|
|
23
32
|
* Get the size (number of elements) of the heap.
|
|
24
33
|
*/
|
|
@@ -39,7 +48,7 @@ export class Heap<E = any> {
|
|
|
39
48
|
* @returns A new Heap instance.
|
|
40
49
|
* @param options
|
|
41
50
|
*/
|
|
42
|
-
static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
|
|
51
|
+
static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
|
|
43
52
|
return new Heap<E>(options);
|
|
44
53
|
}
|
|
45
54
|
|
|
@@ -110,7 +119,7 @@ export class Heap<E = any> {
|
|
|
110
119
|
* Reset the nodes of the heap. Make the nodes empty.
|
|
111
120
|
*/
|
|
112
121
|
clear() {
|
|
113
|
-
this.
|
|
122
|
+
this._nodes = [];
|
|
114
123
|
}
|
|
115
124
|
|
|
116
125
|
/**
|
|
@@ -118,7 +127,7 @@ export class Heap<E = any> {
|
|
|
118
127
|
* @param nodes
|
|
119
128
|
*/
|
|
120
129
|
refill(nodes: E[]) {
|
|
121
|
-
this.
|
|
130
|
+
this._nodes = nodes;
|
|
122
131
|
this.fix();
|
|
123
132
|
}
|
|
124
133
|
|
|
@@ -181,7 +190,7 @@ export class Heap<E = any> {
|
|
|
181
190
|
*/
|
|
182
191
|
clone(): Heap<E> {
|
|
183
192
|
const clonedHeap = new Heap<E>({comparator: this.comparator});
|
|
184
|
-
clonedHeap.
|
|
193
|
+
clonedHeap._nodes = [...this.nodes];
|
|
185
194
|
return clonedHeap;
|
|
186
195
|
}
|
|
187
196
|
|
|
@@ -268,28 +277,47 @@ export class FibonacciHeapNode<E> {
|
|
|
268
277
|
}
|
|
269
278
|
|
|
270
279
|
export class FibonacciHeap<E> {
|
|
271
|
-
root?: FibonacciHeapNode<E>;
|
|
272
|
-
size = 0;
|
|
273
|
-
protected min?: FibonacciHeapNode<E>;
|
|
274
|
-
protected readonly comparator: Comparator<E>;
|
|
275
|
-
|
|
276
280
|
constructor(comparator?: Comparator<E>) {
|
|
277
281
|
this.clear();
|
|
278
|
-
this.
|
|
282
|
+
this._comparator = comparator || this.defaultComparator;
|
|
279
283
|
|
|
280
284
|
if (typeof this.comparator !== 'function') {
|
|
281
285
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
282
286
|
}
|
|
283
287
|
}
|
|
284
288
|
|
|
289
|
+
protected _root?: FibonacciHeapNode<E>;
|
|
290
|
+
|
|
291
|
+
get root(): FibonacciHeapNode<E> | undefined {
|
|
292
|
+
return this._root;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
protected _size = 0;
|
|
296
|
+
|
|
297
|
+
get size(): number {
|
|
298
|
+
return this._size;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
protected _min?: FibonacciHeapNode<E>;
|
|
302
|
+
|
|
303
|
+
get min(): FibonacciHeapNode<E> | undefined {
|
|
304
|
+
return this._min;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
protected _comparator: Comparator<E>;
|
|
308
|
+
|
|
309
|
+
get comparator(): Comparator<E> {
|
|
310
|
+
return this._comparator;
|
|
311
|
+
}
|
|
312
|
+
|
|
285
313
|
/**
|
|
286
314
|
* Get the size (number of elements) of the heap.
|
|
287
315
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
288
316
|
*/
|
|
289
317
|
clear(): void {
|
|
290
|
-
this.
|
|
291
|
-
this.
|
|
292
|
-
this.
|
|
318
|
+
this._root = undefined;
|
|
319
|
+
this._min = undefined;
|
|
320
|
+
this._size = 0;
|
|
293
321
|
}
|
|
294
322
|
|
|
295
323
|
/**
|
|
@@ -315,10 +343,10 @@ export class FibonacciHeap<E> {
|
|
|
315
343
|
this.mergeWithRoot(node);
|
|
316
344
|
|
|
317
345
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
|
|
318
|
-
this.
|
|
346
|
+
this._min = node;
|
|
319
347
|
}
|
|
320
348
|
|
|
321
|
-
this.
|
|
349
|
+
this._size++;
|
|
322
350
|
return this;
|
|
323
351
|
}
|
|
324
352
|
|
|
@@ -405,14 +433,14 @@ export class FibonacciHeap<E> {
|
|
|
405
433
|
this.removeFromRoot(z);
|
|
406
434
|
|
|
407
435
|
if (z === z.right) {
|
|
408
|
-
this.
|
|
409
|
-
this.
|
|
436
|
+
this._min = undefined;
|
|
437
|
+
this._root = undefined;
|
|
410
438
|
} else {
|
|
411
|
-
this.
|
|
439
|
+
this._min = z.right;
|
|
412
440
|
this.consolidate();
|
|
413
441
|
}
|
|
414
442
|
|
|
415
|
-
this.
|
|
443
|
+
this._size--;
|
|
416
444
|
|
|
417
445
|
return z.element;
|
|
418
446
|
}
|
|
@@ -444,11 +472,11 @@ export class FibonacciHeap<E> {
|
|
|
444
472
|
|
|
445
473
|
// Update the minimum node
|
|
446
474
|
if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
|
|
447
|
-
this.
|
|
475
|
+
this._min = heapToMerge.min;
|
|
448
476
|
}
|
|
449
477
|
|
|
450
478
|
// Update the size
|
|
451
|
-
this.
|
|
479
|
+
this._size += heapToMerge.size;
|
|
452
480
|
|
|
453
481
|
// Clear the heap that was merged
|
|
454
482
|
heapToMerge.clear();
|
|
@@ -481,7 +509,7 @@ export class FibonacciHeap<E> {
|
|
|
481
509
|
*/
|
|
482
510
|
protected mergeWithRoot(node: FibonacciHeapNode<E>): void {
|
|
483
511
|
if (!this.root) {
|
|
484
|
-
this.
|
|
512
|
+
this._root = node;
|
|
485
513
|
} else {
|
|
486
514
|
node.right = this.root.right;
|
|
487
515
|
node.left = this.root;
|
|
@@ -497,7 +525,7 @@ export class FibonacciHeap<E> {
|
|
|
497
525
|
* @protected
|
|
498
526
|
*/
|
|
499
527
|
protected removeFromRoot(node: FibonacciHeapNode<E>): void {
|
|
500
|
-
if (this.root === node) this.
|
|
528
|
+
if (this.root === node) this._root = node.right;
|
|
501
529
|
if (node.left) node.left.right = node.right;
|
|
502
530
|
if (node.right) node.right.left = node.left;
|
|
503
531
|
}
|
|
@@ -554,7 +582,7 @@ export class FibonacciHeap<E> {
|
|
|
554
582
|
|
|
555
583
|
for (let i = 0; i < this.size; i++) {
|
|
556
584
|
if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
|
|
557
|
-
this.
|
|
585
|
+
this._min = A[i]!;
|
|
558
586
|
}
|
|
559
587
|
}
|
|
560
588
|
}
|
|
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -6,45 +6,19 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class DoublyLinkedListNode<E = any> {
|
|
9
|
+
value: E;
|
|
10
|
+
next: DoublyLinkedListNode<E> | null;
|
|
11
|
+
prev: DoublyLinkedListNode<E> | null;
|
|
12
|
+
|
|
9
13
|
/**
|
|
10
14
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
11
15
|
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
12
16
|
* is defined as a generic type "E".
|
|
13
17
|
*/
|
|
14
18
|
constructor(value: E) {
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private _value: E;
|
|
21
|
-
|
|
22
|
-
get value(): E {
|
|
23
|
-
return this._value;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
set value(value: E) {
|
|
27
|
-
this._value = value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private _next: DoublyLinkedListNode<E> | null;
|
|
31
|
-
|
|
32
|
-
get next(): DoublyLinkedListNode<E> | null {
|
|
33
|
-
return this._next;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
set next(value: DoublyLinkedListNode<E> | null) {
|
|
37
|
-
this._next = value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private _prev: DoublyLinkedListNode<E> | null;
|
|
41
|
-
|
|
42
|
-
get prev(): DoublyLinkedListNode<E> | null {
|
|
43
|
-
return this._prev;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
set prev(value: DoublyLinkedListNode<E> | null) {
|
|
47
|
-
this._prev = value;
|
|
19
|
+
this.value = value;
|
|
20
|
+
this.next = null;
|
|
21
|
+
this.prev = null;
|
|
48
22
|
}
|
|
49
23
|
}
|
|
50
24
|
|
|
@@ -58,27 +32,19 @@ export class DoublyLinkedList<E = any> {
|
|
|
58
32
|
this._length = 0;
|
|
59
33
|
}
|
|
60
34
|
|
|
61
|
-
|
|
35
|
+
protected _head: DoublyLinkedListNode<E> | null;
|
|
62
36
|
|
|
63
37
|
get head(): DoublyLinkedListNode<E> | null {
|
|
64
38
|
return this._head;
|
|
65
39
|
}
|
|
66
40
|
|
|
67
|
-
|
|
68
|
-
this._head = value;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private _tail: DoublyLinkedListNode<E> | null;
|
|
41
|
+
protected _tail: DoublyLinkedListNode<E> | null;
|
|
72
42
|
|
|
73
43
|
get tail(): DoublyLinkedListNode<E> | null {
|
|
74
44
|
return this._tail;
|
|
75
45
|
}
|
|
76
46
|
|
|
77
|
-
|
|
78
|
-
this._tail = value;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private _length: number;
|
|
47
|
+
protected _length: number;
|
|
82
48
|
|
|
83
49
|
get length(): number {
|
|
84
50
|
return this._length;
|
|
@@ -109,12 +75,12 @@ export class DoublyLinkedList<E = any> {
|
|
|
109
75
|
push(value: E): void {
|
|
110
76
|
const newNode = new DoublyLinkedListNode(value);
|
|
111
77
|
if (!this.head) {
|
|
112
|
-
this.
|
|
113
|
-
this.
|
|
78
|
+
this._head = newNode;
|
|
79
|
+
this._tail = newNode;
|
|
114
80
|
} else {
|
|
115
81
|
newNode.prev = this.tail;
|
|
116
82
|
this.tail!.next = newNode;
|
|
117
|
-
this.
|
|
83
|
+
this._tail = newNode;
|
|
118
84
|
}
|
|
119
85
|
this._length++;
|
|
120
86
|
}
|
|
@@ -136,10 +102,10 @@ export class DoublyLinkedList<E = any> {
|
|
|
136
102
|
if (!this.tail) return undefined;
|
|
137
103
|
const removedNode = this.tail;
|
|
138
104
|
if (this.head === this.tail) {
|
|
139
|
-
this.
|
|
140
|
-
this.
|
|
105
|
+
this._head = null;
|
|
106
|
+
this._tail = null;
|
|
141
107
|
} else {
|
|
142
|
-
this.
|
|
108
|
+
this._tail = removedNode.prev;
|
|
143
109
|
this.tail!.next = null;
|
|
144
110
|
}
|
|
145
111
|
this._length--;
|
|
@@ -164,10 +130,10 @@ export class DoublyLinkedList<E = any> {
|
|
|
164
130
|
if (!this.head) return undefined;
|
|
165
131
|
const removedNode = this.head;
|
|
166
132
|
if (this.head === this.tail) {
|
|
167
|
-
this.
|
|
168
|
-
this.
|
|
133
|
+
this._head = null;
|
|
134
|
+
this._tail = null;
|
|
169
135
|
} else {
|
|
170
|
-
this.
|
|
136
|
+
this._head = removedNode.next;
|
|
171
137
|
this.head!.prev = null;
|
|
172
138
|
}
|
|
173
139
|
this._length--;
|
|
@@ -191,12 +157,12 @@ export class DoublyLinkedList<E = any> {
|
|
|
191
157
|
unshift(value: E): void {
|
|
192
158
|
const newNode = new DoublyLinkedListNode(value);
|
|
193
159
|
if (!this.head) {
|
|
194
|
-
this.
|
|
195
|
-
this.
|
|
160
|
+
this._head = newNode;
|
|
161
|
+
this._tail = newNode;
|
|
196
162
|
} else {
|
|
197
163
|
newNode.next = this.head;
|
|
198
164
|
this.head!.prev = newNode;
|
|
199
|
-
this.
|
|
165
|
+
this._head = newNode;
|
|
200
166
|
}
|
|
201
167
|
this._length++;
|
|
202
168
|
}
|
|
@@ -338,7 +304,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
338
304
|
newNode.next = existingNode;
|
|
339
305
|
existingNode.prev = newNode;
|
|
340
306
|
if (existingNode === this.head) {
|
|
341
|
-
this.
|
|
307
|
+
this._head = newNode;
|
|
342
308
|
}
|
|
343
309
|
this._length++;
|
|
344
310
|
return true;
|
|
@@ -508,7 +474,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
508
474
|
*/
|
|
509
475
|
reverse(): void {
|
|
510
476
|
let current = this.head;
|
|
511
|
-
[this.
|
|
477
|
+
[this._head, this._tail] = [this.tail, this.head];
|
|
512
478
|
while (current) {
|
|
513
479
|
const next = current.next;
|
|
514
480
|
[current.prev, current.next] = [current.next, current.prev];
|
|
@@ -616,7 +582,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
616
582
|
newNode.prev = existingNode;
|
|
617
583
|
existingNode.next = newNode;
|
|
618
584
|
if (existingNode === this.tail) {
|
|
619
|
-
this.
|
|
585
|
+
this._tail = newNode;
|
|
620
586
|
}
|
|
621
587
|
this._length++;
|
|
622
588
|
return true;
|
|
@@ -628,7 +594,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
628
594
|
/**
|
|
629
595
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
630
596
|
*/
|
|
631
|
-
*[Symbol.iterator]() {
|
|
597
|
+
* [Symbol.iterator]() {
|
|
632
598
|
let current = this.head;
|
|
633
599
|
|
|
634
600
|
while (current) {
|
|
@@ -6,34 +6,17 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class SinglyLinkedListNode<E = any> {
|
|
9
|
+
value: E;
|
|
10
|
+
next: SinglyLinkedListNode<E> | null;
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
11
14
|
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
12
15
|
* will be stored in the node of a linked list.
|
|
13
16
|
*/
|
|
14
17
|
constructor(value: E) {
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private _value: E;
|
|
20
|
-
|
|
21
|
-
get value(): E {
|
|
22
|
-
return this._value;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
set value(value: E) {
|
|
26
|
-
this._value = value;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private _next: SinglyLinkedListNode<E> | null;
|
|
30
|
-
|
|
31
|
-
get next(): SinglyLinkedListNode<E> | null {
|
|
32
|
-
return this._next;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
set next(value: SinglyLinkedListNode<E> | null) {
|
|
36
|
-
this._next = value;
|
|
18
|
+
this.value = value;
|
|
19
|
+
this.next = null;
|
|
37
20
|
}
|
|
38
21
|
}
|
|
39
22
|
|
|
@@ -47,27 +30,19 @@ export class SinglyLinkedList<E = any> {
|
|
|
47
30
|
this._length = 0;
|
|
48
31
|
}
|
|
49
32
|
|
|
50
|
-
|
|
33
|
+
protected _head: SinglyLinkedListNode<E> | null;
|
|
51
34
|
|
|
52
35
|
get head(): SinglyLinkedListNode<E> | null {
|
|
53
36
|
return this._head;
|
|
54
37
|
}
|
|
55
38
|
|
|
56
|
-
|
|
57
|
-
this._head = value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private _tail: SinglyLinkedListNode<E> | null;
|
|
39
|
+
protected _tail: SinglyLinkedListNode<E> | null;
|
|
61
40
|
|
|
62
41
|
get tail(): SinglyLinkedListNode<E> | null {
|
|
63
42
|
return this._tail;
|
|
64
43
|
}
|
|
65
44
|
|
|
66
|
-
|
|
67
|
-
this._tail = value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private _length: number;
|
|
45
|
+
protected _length: number;
|
|
71
46
|
|
|
72
47
|
get length(): number {
|
|
73
48
|
return this._length;
|
|
@@ -95,11 +70,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
95
70
|
push(value: E): void {
|
|
96
71
|
const newNode = new SinglyLinkedListNode(value);
|
|
97
72
|
if (!this.head) {
|
|
98
|
-
this.
|
|
99
|
-
this.
|
|
73
|
+
this._head = newNode;
|
|
74
|
+
this._tail = newNode;
|
|
100
75
|
} else {
|
|
101
76
|
this.tail!.next = newNode;
|
|
102
|
-
this.
|
|
77
|
+
this._tail = newNode;
|
|
103
78
|
}
|
|
104
79
|
this._length++;
|
|
105
80
|
}
|
|
@@ -123,8 +98,8 @@ export class SinglyLinkedList<E = any> {
|
|
|
123
98
|
if (!this.head) return undefined;
|
|
124
99
|
if (this.head === this.tail) {
|
|
125
100
|
const value = this.head.value;
|
|
126
|
-
this.
|
|
127
|
-
this.
|
|
101
|
+
this._head = null;
|
|
102
|
+
this._tail = null;
|
|
128
103
|
this._length--;
|
|
129
104
|
return value;
|
|
130
105
|
}
|
|
@@ -135,7 +110,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
135
110
|
}
|
|
136
111
|
const value = this.tail!.value;
|
|
137
112
|
current.next = null;
|
|
138
|
-
this.
|
|
113
|
+
this._tail = current;
|
|
139
114
|
this._length--;
|
|
140
115
|
return value;
|
|
141
116
|
}
|
|
@@ -157,7 +132,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
157
132
|
shift(): E | undefined {
|
|
158
133
|
if (!this.head) return undefined;
|
|
159
134
|
const removedNode = this.head;
|
|
160
|
-
this.
|
|
135
|
+
this._head = this.head.next;
|
|
161
136
|
this._length--;
|
|
162
137
|
return removedNode.value;
|
|
163
138
|
}
|
|
@@ -178,11 +153,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
178
153
|
unshift(value: E): void {
|
|
179
154
|
const newNode = new SinglyLinkedListNode(value);
|
|
180
155
|
if (!this.head) {
|
|
181
|
-
this.
|
|
182
|
-
this.
|
|
156
|
+
this._head = newNode;
|
|
157
|
+
this._tail = newNode;
|
|
183
158
|
} else {
|
|
184
159
|
newNode.next = this.head;
|
|
185
|
-
this.
|
|
160
|
+
this._head = newNode;
|
|
186
161
|
}
|
|
187
162
|
this._length++;
|
|
188
163
|
}
|
|
@@ -267,14 +242,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
267
242
|
while (current) {
|
|
268
243
|
if (current.value === value) {
|
|
269
244
|
if (prev === null) {
|
|
270
|
-
this.
|
|
245
|
+
this._head = current.next;
|
|
271
246
|
if (current === this.tail) {
|
|
272
|
-
this.
|
|
247
|
+
this._tail = null;
|
|
273
248
|
}
|
|
274
249
|
} else {
|
|
275
250
|
prev.next = current.next;
|
|
276
251
|
if (current === this.tail) {
|
|
277
|
-
this.
|
|
252
|
+
this._tail = prev;
|
|
278
253
|
}
|
|
279
254
|
}
|
|
280
255
|
this._length--;
|
|
@@ -365,7 +340,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
365
340
|
current = next;
|
|
366
341
|
}
|
|
367
342
|
|
|
368
|
-
[this.
|
|
343
|
+
[this._head, this._tail] = [this.tail!, this.head!];
|
|
369
344
|
}
|
|
370
345
|
|
|
371
346
|
/**
|
|
@@ -486,7 +461,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
486
461
|
newNode.next = existingNode.next;
|
|
487
462
|
existingNode.next = newNode;
|
|
488
463
|
if (existingNode === this.tail) {
|
|
489
|
-
this.
|
|
464
|
+
this._tail = newNode;
|
|
490
465
|
}
|
|
491
466
|
this._length++;
|
|
492
467
|
return true;
|
|
@@ -590,7 +565,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
590
565
|
/**
|
|
591
566
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
592
567
|
*/
|
|
593
|
-
*[Symbol.iterator]() {
|
|
568
|
+
* [Symbol.iterator]() {
|
|
594
569
|
let current = this.head;
|
|
595
570
|
|
|
596
571
|
while (current) {
|