data-structure-typed 0.8.6
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/.idea/data-structure-typed.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +2 -0
- package/dist/data-structures/binary-tree/aa-tree.js +6 -0
- package/dist/data-structures/binary-tree/avl-tree.js +231 -0
- package/dist/data-structures/binary-tree/b-tree.js +6 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +992 -0
- package/dist/data-structures/binary-tree/bst.js +431 -0
- package/dist/data-structures/binary-tree/index.js +20 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -0
- package/dist/data-structures/binary-tree/segment-tree.js +151 -0
- package/dist/data-structures/binary-tree/splay-tree.js +6 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
- package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
- package/dist/data-structures/graph/abstract-graph.js +648 -0
- package/dist/data-structures/graph/directed-graph.js +268 -0
- package/dist/data-structures/graph/index.js +19 -0
- package/dist/data-structures/graph/undirected-graph.js +142 -0
- package/dist/data-structures/hash/coordinate-map.js +24 -0
- package/dist/data-structures/hash/coordinate-set.js +21 -0
- package/dist/data-structures/hash/hash-table.js +2 -0
- package/dist/data-structures/hash/index.js +17 -0
- package/dist/data-structures/hash/pair.js +2 -0
- package/dist/data-structures/hash/tree-map.js +2 -0
- package/dist/data-structures/hash/tree-set.js +2 -0
- package/dist/data-structures/heap/heap.js +114 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.js +22 -0
- package/dist/data-structures/heap/min-heap.js +22 -0
- package/dist/data-structures/index.js +25 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
- package/dist/data-structures/linked-list/index.js +18 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/data-structures/matrix/index.js +19 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.js +119 -0
- package/dist/data-structures/matrix/navigator.js +78 -0
- package/dist/data-structures/matrix/vector2d.js +161 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +174 -0
- package/dist/data-structures/queue/deque.js +132 -0
- package/dist/data-structures/queue/index.js +17 -0
- package/dist/data-structures/queue/queue.js +113 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.js +97 -0
- package/dist/data-structures/trampoline.js +52 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.js +141 -0
- package/dist/index.js +17 -0
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/pair.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.d.ts +72 -0
- package/dist/types/data-structures/heap/index.d.ts +3 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
- package/dist/types/data-structures/index.d.ts +9 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.d.ts +3 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
- package/dist/types/data-structures/queue/deque.d.ts +37 -0
- package/dist/types/data-structures/queue/index.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +76 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +69 -0
- package/dist/types/data-structures/trampoline.d.ts +25 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +28 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +17 -0
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/utils.d.ts +46 -0
- package/dist/types/utils.d.ts +122 -0
- package/dist/types/utils.js +53 -0
- package/dist/utils.js +569 -0
- package/package.json +75 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/avl-tree.ts +232 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
- package/src/data-structures/binary-tree/bst.ts +404 -0
- package/src/data-structures/binary-tree/index.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +3 -0
- package/src/data-structures/binary-tree/segment-tree.ts +164 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/graph/abstract-graph.ts +789 -0
- package/src/data-structures/graph/directed-graph.ts +322 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +154 -0
- package/src/data-structures/hash/coordinate-map.ts +24 -0
- package/src/data-structures/hash/coordinate-set.ts +20 -0
- package/src/data-structures/hash/hash-table.ts +1 -0
- package/src/data-structures/hash/index.ts +1 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +136 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +22 -0
- package/src/data-structures/heap/min-heap.ts +24 -0
- package/src/data-structures/index.ts +10 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
- package/src/data-structures/linked-list/index.ts +2 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/data-structures/matrix/index.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +13 -0
- package/src/data-structures/matrix/matrix2d.ts +125 -0
- package/src/data-structures/matrix/navigator.ts +99 -0
- package/src/data-structures/matrix/vector2d.ts +189 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/priority-queue.ts +208 -0
- package/src/data-structures/queue/deque.ts +139 -0
- package/src/data-structures/queue/index.ts +1 -0
- package/src/data-structures/queue/queue.ts +123 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +104 -0
- package/src/data-structures/trampoline.ts +91 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +153 -0
- package/src/index.ts +1 -0
- package/src/types/index.ts +1 -0
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +158 -0
- package/src/utils.ts +605 -0
- package/tsconfig.json +52 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2020 Pablo Rios <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {Heap, HeapItem, HeapOptions} from './heap';
|
|
7
|
+
import {PriorityQueue} from '../priority-queue';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @class MinHeap
|
|
11
|
+
* @extends Heap
|
|
12
|
+
*/
|
|
13
|
+
export class MinHeap<T> extends Heap<T> {
|
|
14
|
+
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
15
|
+
|
|
16
|
+
constructor(options?: HeapOptions<T>) {
|
|
17
|
+
super(options);
|
|
18
|
+
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
19
|
+
comparator: (a, b) => a.priority - b.priority
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './hash';
|
|
2
|
+
export * from './linked-list';
|
|
3
|
+
export * from './stack';
|
|
4
|
+
export * from './queue';
|
|
5
|
+
export * from './graph';
|
|
6
|
+
export * from './binary-tree';
|
|
7
|
+
export * from './heap';
|
|
8
|
+
export * from './priority-queue';
|
|
9
|
+
export * from './matrix';
|
|
10
|
+
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby
|
|
2
|
+
// 尾部插入 inject, snoc Append offerLast push push_back append push array_push push
|
|
3
|
+
// 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift
|
|
4
|
+
// 尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop
|
|
5
|
+
// 头部删除 pop Delete_First pollFirst shift pop_front popleft shift array_shift shift
|
|
6
|
+
// 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
|
|
7
|
+
// 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|
|
8
|
+
|
|
9
|
+
export class DoublyLinkedListNode<T> {
|
|
10
|
+
val: T;
|
|
11
|
+
next: DoublyLinkedListNode<T> | null;
|
|
12
|
+
prev: DoublyLinkedListNode<T> | null;
|
|
13
|
+
|
|
14
|
+
constructor(nodeValue: T) {
|
|
15
|
+
this.val = nodeValue;
|
|
16
|
+
this.next = null;
|
|
17
|
+
this.prev = null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type DoublyLinkedListGetBy = 'node' | 'val';
|
|
22
|
+
|
|
23
|
+
export class DoublyLinkedList<T> {
|
|
24
|
+
private _first: DoublyLinkedListNode<T> | null = null;
|
|
25
|
+
private _last: DoublyLinkedListNode<T> | null = null;
|
|
26
|
+
private _size = 0;
|
|
27
|
+
get size(): number {
|
|
28
|
+
return this._size;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
set size(v: number) {
|
|
32
|
+
this._size = v;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Adds a node at the beginning of the linked list
|
|
37
|
+
* @param val Value to be stored at the beginning of the linked list
|
|
38
|
+
*/
|
|
39
|
+
offerFirst(val: T): boolean {
|
|
40
|
+
const newNode = new DoublyLinkedListNode(val);
|
|
41
|
+
if (this._size === 0) {
|
|
42
|
+
this._first = newNode;
|
|
43
|
+
this._last = newNode;
|
|
44
|
+
} else {
|
|
45
|
+
if (this._first) this._first.prev = newNode;
|
|
46
|
+
newNode.next = this._first;
|
|
47
|
+
this._first = newNode;
|
|
48
|
+
}
|
|
49
|
+
this._size++;
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Adds a node to the end of the linked list
|
|
55
|
+
* @param val Value to be stored in the Doubly linked list node
|
|
56
|
+
*/
|
|
57
|
+
offerLast(val: T): boolean {
|
|
58
|
+
const newNode = new DoublyLinkedListNode(val);
|
|
59
|
+
if (this._size === 0) {
|
|
60
|
+
this._first = newNode;
|
|
61
|
+
this._last = newNode;
|
|
62
|
+
} else {
|
|
63
|
+
if (this._last) this._last.next = newNode;
|
|
64
|
+
newNode.prev = this._last;
|
|
65
|
+
this._last = newNode;
|
|
66
|
+
}
|
|
67
|
+
this._size++;
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
peekFirst(): T | null;
|
|
72
|
+
peekFirst(by: 'val'): T | null;
|
|
73
|
+
peekFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
74
|
+
peekFirst(by?: DoublyLinkedListGetBy): T | DoublyLinkedListNode<T> | null {
|
|
75
|
+
switch (by) {
|
|
76
|
+
case 'node':
|
|
77
|
+
return this._first ?? null;
|
|
78
|
+
case 'val':
|
|
79
|
+
return this._first?.val ?? null;
|
|
80
|
+
default:
|
|
81
|
+
return this._first?.val ?? null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
peekLast(): T | null;
|
|
86
|
+
peekLast(by: 'val'): T | null;
|
|
87
|
+
peekLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
88
|
+
peekLast(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
89
|
+
switch (by) {
|
|
90
|
+
case 'node':
|
|
91
|
+
return this._last ?? null;
|
|
92
|
+
case 'val':
|
|
93
|
+
return this._last?.val ?? null;
|
|
94
|
+
default:
|
|
95
|
+
return this._last?.val ?? null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
pollFirst(): T | null;
|
|
100
|
+
pollFirst(by: 'val'): T | null;
|
|
101
|
+
pollFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
102
|
+
/**
|
|
103
|
+
* Removes a node form the beginning of the linked list and will return the node val
|
|
104
|
+
*/
|
|
105
|
+
pollFirst(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
106
|
+
if (this._size === 0) return null;
|
|
107
|
+
const oldHead = this._first;
|
|
108
|
+
if (this._size === 1) {
|
|
109
|
+
this._first = null;
|
|
110
|
+
this._last = null;
|
|
111
|
+
} else {
|
|
112
|
+
this._first = oldHead?.next ?? null;
|
|
113
|
+
if (this._first) this._first.prev = null;
|
|
114
|
+
if (oldHead) oldHead.next = null;
|
|
115
|
+
}
|
|
116
|
+
this._size--;
|
|
117
|
+
switch (by) {
|
|
118
|
+
case 'node':
|
|
119
|
+
return oldHead ?? null;
|
|
120
|
+
case 'val':
|
|
121
|
+
return oldHead?.val ?? null;
|
|
122
|
+
default:
|
|
123
|
+
return oldHead?.val ?? null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
pollLast(): T | null;
|
|
128
|
+
pollLast(by: 'val'): T | null;
|
|
129
|
+
pollLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
130
|
+
/**
|
|
131
|
+
* Removes a node at the end of the linked list and will return the node value
|
|
132
|
+
*/
|
|
133
|
+
pollLast(by: DoublyLinkedListGetBy = 'val'): DoublyLinkedListNode<T> | T | null {
|
|
134
|
+
if (this._size === 0) return null;
|
|
135
|
+
const polled = this._last;
|
|
136
|
+
if (this._size === 1) {
|
|
137
|
+
this._first = null;
|
|
138
|
+
this._last = null;
|
|
139
|
+
} else {
|
|
140
|
+
this._last = polled?.prev ?? null;
|
|
141
|
+
if (this._last) this._last.next = null;
|
|
142
|
+
if (polled) polled.prev = null;
|
|
143
|
+
}
|
|
144
|
+
this._size--;
|
|
145
|
+
switch (by) {
|
|
146
|
+
case 'node':
|
|
147
|
+
return polled ?? null;
|
|
148
|
+
case 'val':
|
|
149
|
+
return polled?.val ?? null;
|
|
150
|
+
default:
|
|
151
|
+
return polled?.val ?? null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get(index: number): T | null;
|
|
156
|
+
get(index: number, by: 'node'): DoublyLinkedListNode<T> | null;
|
|
157
|
+
get(index: number, by: 'val'): T | null;
|
|
158
|
+
/**
|
|
159
|
+
* Returns the node at the specified index of the linked list.
|
|
160
|
+
* If index = 0; first element in the list is returned.
|
|
161
|
+
* If index = 3; fourth element in the list is returned.
|
|
162
|
+
* @param index Index of the node to be retrieved
|
|
163
|
+
* @param by Return value type
|
|
164
|
+
*/
|
|
165
|
+
get(index: number, by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
166
|
+
if (index < 0 || index >= this._size) return null;
|
|
167
|
+
let count, current;
|
|
168
|
+
if (index <= this._size / 2) {
|
|
169
|
+
count = 0;
|
|
170
|
+
current = this._first;
|
|
171
|
+
while (count !== index) {
|
|
172
|
+
current = current?.next;
|
|
173
|
+
count++;
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
count = this._size - 1;
|
|
177
|
+
current = this._last;
|
|
178
|
+
while (count !== index) {
|
|
179
|
+
current = current?.prev;
|
|
180
|
+
count--;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
switch (by) {
|
|
184
|
+
case 'node':
|
|
185
|
+
return current ?? null;
|
|
186
|
+
case 'val':
|
|
187
|
+
return current?.val ?? null;
|
|
188
|
+
default:
|
|
189
|
+
return current?.val ?? null;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Updates the value of the node at the specified index.
|
|
195
|
+
* If index = 0; Value of the first element in the list is updated.
|
|
196
|
+
* If index = 3; Value of the fourth element in the list is updated.
|
|
197
|
+
* @param index Index of the node to be updated
|
|
198
|
+
* @param val New value of the node
|
|
199
|
+
*/
|
|
200
|
+
set(index: number, val: T): boolean {
|
|
201
|
+
const foundNode = this.get(index, 'node');
|
|
202
|
+
if (foundNode !== null) {
|
|
203
|
+
foundNode.val = val;
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
isEmpty() {
|
|
210
|
+
return this._size === 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// --- start extra methods ---
|
|
214
|
+
/**
|
|
215
|
+
* Inserts a new node at the specified index.
|
|
216
|
+
* @param index Index at which the new node has to be inserted
|
|
217
|
+
* @param val Value of the new node to be inserted
|
|
218
|
+
*/
|
|
219
|
+
insert(index: number, val: T): boolean {
|
|
220
|
+
if (index < 0 || index > this._size) return false;
|
|
221
|
+
if (index === 0) return !!this.offerFirst(val);
|
|
222
|
+
if (index === this._size) return !!this.offerLast(val);
|
|
223
|
+
|
|
224
|
+
const newNode = new DoublyLinkedListNode(val);
|
|
225
|
+
const prevNode = this.get(index - 1, 'node');
|
|
226
|
+
const nextNode = prevNode?.next;
|
|
227
|
+
|
|
228
|
+
if (prevNode) prevNode.next = newNode;
|
|
229
|
+
newNode.prev = prevNode;
|
|
230
|
+
newNode.next = nextNode ?? null;
|
|
231
|
+
if (nextNode) nextNode.prev = newNode;
|
|
232
|
+
this._size++;
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Removes a node at the specified index and returns its value.
|
|
238
|
+
* @param index Index at which the node has to be removed.
|
|
239
|
+
*/
|
|
240
|
+
remove(index: number): T | null {
|
|
241
|
+
if (index < 0 || index > this._size - 1) return null;
|
|
242
|
+
else if (index === 0) return this.pollFirst();
|
|
243
|
+
else if (index === this._size - 1) return this.pollLast('node')?.val ?? null;
|
|
244
|
+
else {
|
|
245
|
+
const prevNode = this.get(index - 1, 'node');
|
|
246
|
+
const removeNode = prevNode?.next;
|
|
247
|
+
const nextNode = removeNode?.next;
|
|
248
|
+
if (prevNode) prevNode.next = nextNode ?? null;
|
|
249
|
+
if (nextNode) nextNode.prev = prevNode;
|
|
250
|
+
if (removeNode) removeNode.next = null;
|
|
251
|
+
if (removeNode) removeNode.prev = null;
|
|
252
|
+
this._size--;
|
|
253
|
+
return removeNode?.val ?? null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// --- end extra methods ---
|
|
258
|
+
}
|