data-structure-typed 1.15.2 → 1.16.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/README.md +24 -24
- package/dist/data-structures/heap/heap.d.ts +30 -15
- package/dist/data-structures/heap/heap.js +60 -15
- package/dist/data-structures/heap/max-heap.d.ts +3 -3
- package/dist/data-structures/heap/min-heap.d.ts +3 -3
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
- package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
- package/dist/data-structures/priority-queue/priority-queue.js +28 -22
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +9 -0
- package/dist/data-structures/tree/tree.js +52 -0
- package/dist/data-structures/types/heap.d.ts +0 -4
- package/dist/utils/types/utils.d.ts +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +92 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2192 -0
- package/docs/classes/AVLTreeNode.html +574 -0
- package/docs/classes/AaTree.html +150 -0
- package/docs/classes/AbstractEdge.html +269 -0
- package/docs/classes/AbstractGraph.html +927 -0
- package/docs/classes/AbstractVertex.html +215 -0
- package/docs/classes/ArrayDeque.html +417 -0
- package/docs/classes/BST.html +2038 -0
- package/docs/classes/BSTNode.html +570 -0
- package/docs/classes/BTree.html +150 -0
- package/docs/classes/BinaryIndexedTree.html +289 -0
- package/docs/classes/BinaryTree.html +1827 -0
- package/docs/classes/BinaryTreeNode.html +533 -0
- package/docs/classes/Character.html +198 -0
- package/docs/classes/CoordinateMap.html +469 -0
- package/docs/classes/CoordinateSet.html +430 -0
- package/docs/classes/Deque.html +767 -0
- package/docs/classes/DirectedEdge.html +354 -0
- package/docs/classes/DirectedGraph.html +1243 -0
- package/docs/classes/DirectedVertex.html +226 -0
- package/docs/classes/DoublyLinkedList.html +733 -0
- package/docs/classes/DoublyLinkedListNode.html +258 -0
- package/docs/classes/Heap.html +482 -0
- package/docs/classes/HeapItem.html +233 -0
- package/docs/classes/Matrix2D.html +480 -0
- package/docs/classes/MatrixNTI2D.html +218 -0
- package/docs/classes/MaxHeap.html +500 -0
- package/docs/classes/MaxPriorityQueue.html +809 -0
- package/docs/classes/MinHeap.html +501 -0
- package/docs/classes/MinPriorityQueue.html +811 -0
- package/docs/classes/Navigator.html +291 -0
- package/docs/classes/ObjectDeque.html +423 -0
- package/docs/classes/PriorityQueue.html +733 -0
- package/docs/classes/Queue.html +370 -0
- package/docs/classes/RBTree.html +150 -0
- package/docs/classes/SegmentTree.html +345 -0
- package/docs/classes/SegmentTreeNode.html +418 -0
- package/docs/classes/SinglyLinkedList.html +1105 -0
- package/docs/classes/SinglyLinkedListNode.html +375 -0
- package/docs/classes/SplayTree.html +150 -0
- package/docs/classes/Stack.html +346 -0
- package/docs/classes/TreeMultiSet.html +2036 -0
- package/docs/classes/TreeNode.html +236 -0
- package/docs/classes/Trie.html +350 -0
- package/docs/classes/TrieNode.html +258 -0
- package/docs/classes/TwoThreeTree.html +150 -0
- package/docs/classes/UndirectedEdge.html +313 -0
- package/docs/classes/UndirectedGraph.html +1080 -0
- package/docs/classes/UndirectedVertex.html +226 -0
- package/docs/classes/Vector2D.html +783 -0
- package/docs/enums/CP.html +159 -0
- package/docs/enums/FamilyPosition.html +159 -0
- package/docs/enums/LoopType.html +160 -0
- package/docs/index.html +494 -0
- package/docs/interfaces/AVLTreeDeleted.html +161 -0
- package/docs/interfaces/BinaryTreeNodeObj.html +168 -0
- package/docs/interfaces/HeapOptions.html +167 -0
- package/docs/interfaces/IDirectedGraph.html +243 -0
- package/docs/interfaces/IGraph.html +427 -0
- package/docs/interfaces/NavigatorParams.html +197 -0
- package/docs/interfaces/PriorityQueueOptions.html +168 -0
- package/docs/modules.html +218 -0
- package/docs/types/BSTComparator.html +140 -0
- package/docs/types/BSTDeletedResult.html +137 -0
- package/docs/types/BinaryTreeDeleted.html +137 -0
- package/docs/types/BinaryTreeNodeId.html +125 -0
- package/docs/types/BinaryTreeNodePropertyName.html +125 -0
- package/docs/types/DFSOrderPattern.html +125 -0
- package/docs/types/DijkstraResult.html +145 -0
- package/docs/types/Direction.html +125 -0
- package/docs/types/DoublyLinkedListGetBy.html +125 -0
- package/docs/types/NodeOrPropertyName.html +125 -0
- package/docs/types/PriorityQueueComparator.html +145 -0
- package/docs/types/PriorityQueueDFSOrderPattern.html +125 -0
- package/docs/types/ResultByProperty.html +130 -0
- package/docs/types/ResultsByProperty.html +130 -0
- package/docs/types/SegmentTreeNodeVal.html +125 -0
- package/docs/types/SpecifyOptional.html +132 -0
- package/docs/types/Thunk.html +133 -0
- package/docs/types/ToThunkFn.html +133 -0
- package/docs/types/TopologicalStatus.html +125 -0
- package/docs/types/TreeMultiSetDeletedResult.html +137 -0
- package/docs/types/TrlAsyncFn.html +138 -0
- package/docs/types/TrlFn.html +138 -0
- package/docs/types/Turning.html +125 -0
- package/docs/types/VertexId.html +125 -0
- package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
- package/notes/note.md +23 -0
- package/package.json +1 -1
- package/.idea/data-structure-typed.iml +0 -14
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- 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 +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -293
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
- package/src/data-structures/binary-tree/bst.ts +0 -497
- 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 +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -267
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -5
- package/src/data-structures/graph/abstract-graph.ts +0 -958
- 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 +0 -429
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -259
- package/src/data-structures/hash/coordinate-map.ts +0 -74
- package/src/data-structures/hash/coordinate-set.ts +0 -63
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -162
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -34
- package/src/data-structures/index.ts +0 -13
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -208
- package/src/data-structures/matrix/navigator.ts +0 -122
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
- package/src/data-structures/priority-queue/priority-queue.ts +0 -349
- package/src/data-structures/queue/deque.ts +0 -251
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -120
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/index.ts +0 -13
- package/src/data-structures/types/navigator.ts +0 -13
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -1
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -2
- package/src/utils/types/index.ts +0 -1
- package/src/utils/types/utils.ts +0 -4
- package/src/utils/utils.ts +0 -78
- package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
- package/tests/unit/data-structures/graph/index.ts +0 -3
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
|
@@ -1,365 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
import type {DoublyLinkedListGetBy} from '../types';
|
|
9
|
-
|
|
10
|
-
export class DoublyLinkedListNode<T> {
|
|
11
|
-
constructor(nodeValue: T) {
|
|
12
|
-
this._val = nodeValue;
|
|
13
|
-
this._next = null;
|
|
14
|
-
this._prev = null;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
protected _val: T;
|
|
18
|
-
|
|
19
|
-
get val(): T {
|
|
20
|
-
return this._val;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
set val(v: T) {
|
|
24
|
-
this._val = v;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
protected _next: DoublyLinkedListNode<T> | null;
|
|
28
|
-
|
|
29
|
-
get next(): DoublyLinkedListNode<T> | null {
|
|
30
|
-
return this._next;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
set next(v: DoublyLinkedListNode<T> | null) {
|
|
34
|
-
this._next = v;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
protected _prev: DoublyLinkedListNode<T> | null;
|
|
38
|
-
|
|
39
|
-
get prev(): DoublyLinkedListNode<T> | null {
|
|
40
|
-
return this._prev;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
set prev(v: DoublyLinkedListNode<T> | null) {
|
|
44
|
-
this._prev = v;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export class DoublyLinkedList<T> {
|
|
49
|
-
constructor() {
|
|
50
|
-
this._first = null;
|
|
51
|
-
this._last = null;
|
|
52
|
-
this._size = 0;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
protected _first: DoublyLinkedListNode<T> | null;
|
|
56
|
-
|
|
57
|
-
get first(): DoublyLinkedListNode<T> | null {
|
|
58
|
-
return this._first;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
protected set first(v: DoublyLinkedListNode<T> | null) {
|
|
62
|
-
this._first = v;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
protected _last: DoublyLinkedListNode<T> | null;
|
|
66
|
-
get last(): DoublyLinkedListNode<T> | null {
|
|
67
|
-
return this._last;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
protected set last(v: DoublyLinkedListNode<T> | null) {
|
|
71
|
-
this._last = v;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
protected _size: number;
|
|
75
|
-
|
|
76
|
-
get size(): number {
|
|
77
|
-
return this._size;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
protected set size(v: number) {
|
|
81
|
-
this._size = v;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
86
|
-
*/
|
|
87
|
-
getFirst(): DoublyLinkedListNode<T> | null {
|
|
88
|
-
return this._first;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
93
|
-
*/
|
|
94
|
-
getLast(): DoublyLinkedListNode<T> | null {
|
|
95
|
-
return this._last;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
100
|
-
*/
|
|
101
|
-
getSize(): number {
|
|
102
|
-
return this._size;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The function adds a new node with a given value to the beginning of a doubly linked list.
|
|
107
|
-
* @param {T} val - The `val` parameter represents the value of the element that you want to add to the beginning of
|
|
108
|
-
* the doubly linked list.
|
|
109
|
-
* @returns A boolean value is being returned.
|
|
110
|
-
*/
|
|
111
|
-
addFirst(val: T): boolean {
|
|
112
|
-
const newNode = new DoublyLinkedListNode(val);
|
|
113
|
-
if (this._size === 0) {
|
|
114
|
-
this._first = newNode;
|
|
115
|
-
this._last = newNode;
|
|
116
|
-
} else {
|
|
117
|
-
if (this._first) this._first.prev = newNode;
|
|
118
|
-
newNode.next = this._first;
|
|
119
|
-
this._first = newNode;
|
|
120
|
-
}
|
|
121
|
-
this._size++;
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* The function adds a new node with a given value to the end of a doubly linked list.
|
|
127
|
-
* @param {T} val - The `val` parameter represents the value of the element that you want to add to the end of the
|
|
128
|
-
* doubly linked list.
|
|
129
|
-
* @returns a boolean value, which is always true.
|
|
130
|
-
*/
|
|
131
|
-
addLast(val: T): boolean {
|
|
132
|
-
const newNode = new DoublyLinkedListNode(val);
|
|
133
|
-
if (this._size === 0) {
|
|
134
|
-
this._first = newNode;
|
|
135
|
-
this._last = newNode;
|
|
136
|
-
} else {
|
|
137
|
-
if (this._last) this._last.next = newNode;
|
|
138
|
-
newNode.prev = this._last;
|
|
139
|
-
this._last = newNode;
|
|
140
|
-
}
|
|
141
|
-
this._size++;
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
peekFirst(): T | null;
|
|
146
|
-
peekFirst(by: 'val'): T | null;
|
|
147
|
-
peekFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
148
|
-
/**
|
|
149
|
-
* The `peekFirst` function returns the first node or value in a doubly linked list, depending on the specified
|
|
150
|
-
* parameter.
|
|
151
|
-
* @param {DoublyLinkedListGetBy} [by] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy. It
|
|
152
|
-
* is used to specify whether to return the first node, the value of the first node, or the first node itself.
|
|
153
|
-
* @returns The method `peekFirst` returns either the first node of the doubly linked list (`DoublyLinkedListNode<T>`),
|
|
154
|
-
* the value of the first node (`T`), or `null` depending on the value of the `by` parameter.
|
|
155
|
-
*/
|
|
156
|
-
peekFirst(by?: DoublyLinkedListGetBy): T | DoublyLinkedListNode<T> | null {
|
|
157
|
-
switch (by) {
|
|
158
|
-
case 'node':
|
|
159
|
-
return this._first ?? null;
|
|
160
|
-
case 'val':
|
|
161
|
-
return this._first?.val ?? null;
|
|
162
|
-
default:
|
|
163
|
-
return this._first?.val ?? null;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
peekLast(): T | null;
|
|
168
|
-
peekLast(by: 'val'): T | null;
|
|
169
|
-
peekLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
170
|
-
/**
|
|
171
|
-
* The `peekLast` function returns the last node or value in a doubly linked list.
|
|
172
|
-
* @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
|
|
173
|
-
* It specifies whether to return the last node, the value of the last node, or both. The default value is 'val', which
|
|
174
|
-
* means that if no value is provided for the "by" parameter, the method
|
|
175
|
-
* @returns The method `peekLast` returns the last node, value, or null based on the specified `by` parameter.
|
|
176
|
-
*/
|
|
177
|
-
peekLast(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
178
|
-
switch (by) {
|
|
179
|
-
case 'node':
|
|
180
|
-
return this._last ?? null;
|
|
181
|
-
case 'val':
|
|
182
|
-
return this._last?.val ?? null;
|
|
183
|
-
default:
|
|
184
|
-
return this._last?.val ?? null;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
pollFirst(): T | null;
|
|
189
|
-
pollFirst(by: 'val'): T | null;
|
|
190
|
-
pollFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
191
|
-
/**
|
|
192
|
-
* The function `pollFirst` removes and returns the first element of a doubly linked list, either as a node or its
|
|
193
|
-
* value, depending on the specified parameter.
|
|
194
|
-
* @param {DoublyLinkedListGetBy} [by=val] - The "by" parameter is an optional parameter of type DoublyLinkedListGetBy.
|
|
195
|
-
* It specifies the criteria by which the first element should be retrieved from the doubly linked list. The default
|
|
196
|
-
* value is 'val', which means the first element will be retrieved by its value. Other possible values for "by
|
|
197
|
-
* @returns The method `pollFirst` returns either the value of the first node in the doubly linked list, the first node
|
|
198
|
-
* itself, or null if the list is empty. The specific return type depends on the value of the `by` parameter. If `by`
|
|
199
|
-
* is set to 'node', the method returns the first node. If `by` is set to 'val', the method returns the value
|
|
200
|
-
*/
|
|
201
|
-
pollFirst(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
202
|
-
if (this._size === 0) return null;
|
|
203
|
-
const oldHead = this._first;
|
|
204
|
-
if (this._size === 1) {
|
|
205
|
-
this._first = null;
|
|
206
|
-
this._last = null;
|
|
207
|
-
} else {
|
|
208
|
-
this._first = oldHead?.next ?? null;
|
|
209
|
-
if (this._first) this._first.prev = null;
|
|
210
|
-
if (oldHead) oldHead.next = null;
|
|
211
|
-
}
|
|
212
|
-
this._size--;
|
|
213
|
-
switch (by) {
|
|
214
|
-
case 'node':
|
|
215
|
-
return oldHead ?? null;
|
|
216
|
-
case 'val':
|
|
217
|
-
return oldHead?.val ?? null;
|
|
218
|
-
default:
|
|
219
|
-
return oldHead?.val ?? null;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
pollLast(): T | null;
|
|
224
|
-
pollLast(by: 'val'): T | null;
|
|
225
|
-
pollLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
226
|
-
/**
|
|
227
|
-
* The function `pollLast` removes and returns the last element in a doubly linked list, either as a node or its value,
|
|
228
|
-
* depending on the specified parameter.
|
|
229
|
-
* @param {DoublyLinkedListGetBy} [by=val] - The parameter "by" is of type DoublyLinkedListGetBy, which is an enum that
|
|
230
|
-
* can have two possible values: 'node' or 'val'. It determines the type of value that will be returned by the pollLast
|
|
231
|
-
* method. If 'node' is specified, the method will return the
|
|
232
|
-
* @returns The method `pollLast` returns either a `DoublyLinkedListNode<T>`, the value of the node (`T`), or `null`.
|
|
233
|
-
* The specific type that is returned depends on the value of the `by` parameter. If `by` is set to `'node'`, then a
|
|
234
|
-
* `DoublyLinkedListNode<T>` is returned. If `by` is set to `'
|
|
235
|
-
*/
|
|
236
|
-
pollLast(by: DoublyLinkedListGetBy = 'val'): DoublyLinkedListNode<T> | T | null {
|
|
237
|
-
if (this._size === 0) return null;
|
|
238
|
-
const polled = this._last;
|
|
239
|
-
if (this._size === 1) {
|
|
240
|
-
this._first = null;
|
|
241
|
-
this._last = null;
|
|
242
|
-
} else {
|
|
243
|
-
this._last = polled?.prev ?? null;
|
|
244
|
-
if (this._last) this._last.next = null;
|
|
245
|
-
if (polled) polled.prev = null;
|
|
246
|
-
}
|
|
247
|
-
this._size--;
|
|
248
|
-
switch (by) {
|
|
249
|
-
case 'node':
|
|
250
|
-
return polled ?? null;
|
|
251
|
-
case 'val':
|
|
252
|
-
return polled?.val ?? null;
|
|
253
|
-
default:
|
|
254
|
-
return polled?.val ?? null;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
get(index: number): T | null;
|
|
259
|
-
get(index: number, by: 'node'): DoublyLinkedListNode<T> | null;
|
|
260
|
-
get(index: number, by: 'val'): T | null;
|
|
261
|
-
/**
|
|
262
|
-
* Returns the node at the specified index of the linked list.
|
|
263
|
-
* If index = 0; first element in the list is returned.
|
|
264
|
-
* If index = 3; fourth element in the list is returned.
|
|
265
|
-
* @param index Index of the node to be retrieved
|
|
266
|
-
* @param by Return value type
|
|
267
|
-
*/
|
|
268
|
-
get(index: number, by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
269
|
-
if (index < 0 || index >= this._size) return null;
|
|
270
|
-
let count, current;
|
|
271
|
-
if (index <= this._size / 2) {
|
|
272
|
-
count = 0;
|
|
273
|
-
current = this._first;
|
|
274
|
-
while (count !== index) {
|
|
275
|
-
current = current?.next;
|
|
276
|
-
count++;
|
|
277
|
-
}
|
|
278
|
-
} else {
|
|
279
|
-
count = this._size - 1;
|
|
280
|
-
current = this._last;
|
|
281
|
-
while (count !== index) {
|
|
282
|
-
current = current?.prev;
|
|
283
|
-
count--;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
switch (by) {
|
|
287
|
-
case 'node':
|
|
288
|
-
return current ?? null;
|
|
289
|
-
case 'val':
|
|
290
|
-
return current?.val ?? null;
|
|
291
|
-
default:
|
|
292
|
-
return current?.val ?? null;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Updates the value of the node at the specified index.
|
|
298
|
-
* If index = 0; Value of the first element in the list is updated.
|
|
299
|
-
* If index = 3; Value of the fourth element in the list is updated.
|
|
300
|
-
* @param index Index of the node to be updated
|
|
301
|
-
* @param val New value of the node
|
|
302
|
-
*/
|
|
303
|
-
set(index: number, val: T): boolean {
|
|
304
|
-
const foundNode = this.get(index, 'node');
|
|
305
|
-
if (foundNode !== null) {
|
|
306
|
-
foundNode.val = val;
|
|
307
|
-
return true;
|
|
308
|
-
}
|
|
309
|
-
return false;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
isEmpty() {
|
|
313
|
-
return this._size === 0;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// --- start extra methods ---
|
|
317
|
-
/**
|
|
318
|
-
* Inserts a new node at the specified index.
|
|
319
|
-
* @param index Index at which the new node has to be inserted
|
|
320
|
-
* @param val Value of the new node to be inserted
|
|
321
|
-
*/
|
|
322
|
-
insert(index: number, val: T): boolean {
|
|
323
|
-
if (index < 0 || index > this._size) return false;
|
|
324
|
-
if (index === 0) return !!this.addFirst(val);
|
|
325
|
-
if (index === this._size) return !!this.addLast(val);
|
|
326
|
-
|
|
327
|
-
const newNode = new DoublyLinkedListNode(val);
|
|
328
|
-
const prevNode = this.get(index - 1, 'node');
|
|
329
|
-
const nextNode = prevNode?.next;
|
|
330
|
-
|
|
331
|
-
if (prevNode) prevNode.next = newNode;
|
|
332
|
-
newNode.prev = prevNode;
|
|
333
|
-
newNode.next = nextNode ?? null;
|
|
334
|
-
if (nextNode) nextNode.prev = newNode;
|
|
335
|
-
this._size++;
|
|
336
|
-
return true;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* The `remove` function removes an element at a specified index from a data structure, updating the links between
|
|
341
|
-
* nodes accordingly.
|
|
342
|
-
* @param {number} index - The index parameter represents the position of the element to be removed in the data
|
|
343
|
-
* structure. It is of type number.
|
|
344
|
-
* @returns The `remove` method returns the value of the removed element (`T`) if the removal is successful, or `null`
|
|
345
|
-
* if the index is out of bounds.
|
|
346
|
-
*/
|
|
347
|
-
remove(index: number): T | null {
|
|
348
|
-
if (index < 0 || index > this._size - 1) return null;
|
|
349
|
-
else if (index === 0) return this.pollFirst();
|
|
350
|
-
else if (index === this._size - 1) return this.pollLast('node')?.val ?? null;
|
|
351
|
-
else {
|
|
352
|
-
const prevNode = this.get(index - 1, 'node');
|
|
353
|
-
const removeNode = prevNode?.next;
|
|
354
|
-
const nextNode = removeNode?.next;
|
|
355
|
-
if (prevNode) prevNode.next = nextNode ?? null;
|
|
356
|
-
if (nextNode) nextNode.prev = prevNode;
|
|
357
|
-
if (removeNode) removeNode.next = null;
|
|
358
|
-
if (removeNode) removeNode.prev = null;
|
|
359
|
-
this._size--;
|
|
360
|
-
return removeNode?.val ?? null;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
// --- end extra methods ---
|
|
365
|
-
}
|