data-structure-typed 0.9.16 → 1.3.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 +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- 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 +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +134 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- 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 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +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/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- 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 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- 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/navigator.ts +0 -12
- 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 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
-
* @license MIT
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {Heap} from './heap';
|
|
7
|
-
import {PriorityQueue} from '../priority-queue';
|
|
8
|
-
import type {HeapItem, HeapOptions} from '../types';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @class MaxHeap
|
|
12
|
-
* @extends Heap
|
|
13
|
-
*/
|
|
14
|
-
export class MaxHeap<T> extends Heap<T> {
|
|
15
|
-
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
16
|
-
|
|
17
|
-
constructor(options?: HeapOptions<T>) {
|
|
18
|
-
super(options);
|
|
19
|
-
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
20
|
-
comparator: (a, b) => b.priority - a.priority
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
-
* @license MIT
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {Heap} from './heap';
|
|
7
|
-
import {PriorityQueue} from '../priority-queue';
|
|
8
|
-
import type {HeapItem, HeapOptions} from '../types';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @class MinHeap
|
|
12
|
-
* @extends Heap
|
|
13
|
-
*/
|
|
14
|
-
export class MinHeap<T> extends Heap<T> {
|
|
15
|
-
protected _pq: PriorityQueue<HeapItem<T>>;
|
|
16
|
-
|
|
17
|
-
constructor(options?: HeapOptions<T>) {
|
|
18
|
-
super(options);
|
|
19
|
-
this._pq = new PriorityQueue<HeapItem<T>>({
|
|
20
|
-
comparator: (a, b) => a.priority - b.priority
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
export * from './trie';
|
|
11
|
-
export * from './types';
|
|
12
|
-
|
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import type {DoublyLinkedListGetBy} from '../types';
|
|
2
|
-
|
|
3
|
-
export class DoublyLinkedListNode<T> {
|
|
4
|
-
val: T;
|
|
5
|
-
next: DoublyLinkedListNode<T> | null;
|
|
6
|
-
prev: DoublyLinkedListNode<T> | null;
|
|
7
|
-
|
|
8
|
-
constructor(nodeValue: T) {
|
|
9
|
-
this.val = nodeValue;
|
|
10
|
-
this.next = null;
|
|
11
|
-
this.prev = null;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class DoublyLinkedList<T> {
|
|
16
|
-
private _first: DoublyLinkedListNode<T> | null = null;
|
|
17
|
-
private _last: DoublyLinkedListNode<T> | null = null;
|
|
18
|
-
private _size = 0;
|
|
19
|
-
get size(): number {
|
|
20
|
-
return this._size;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
set size(v: number) {
|
|
24
|
-
this._size = v;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Adds a node at the beginning of the linked list
|
|
29
|
-
* @param val Value to be stored at the beginning of the linked list
|
|
30
|
-
*/
|
|
31
|
-
offerFirst(val: T): boolean {
|
|
32
|
-
const newNode = new DoublyLinkedListNode(val);
|
|
33
|
-
if (this._size === 0) {
|
|
34
|
-
this._first = newNode;
|
|
35
|
-
this._last = newNode;
|
|
36
|
-
} else {
|
|
37
|
-
if (this._first) this._first.prev = newNode;
|
|
38
|
-
newNode.next = this._first;
|
|
39
|
-
this._first = newNode;
|
|
40
|
-
}
|
|
41
|
-
this._size++;
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Adds a node to the end of the linked list
|
|
47
|
-
* @param val Value to be stored in the Doubly linked list node
|
|
48
|
-
*/
|
|
49
|
-
offerLast(val: T): boolean {
|
|
50
|
-
const newNode = new DoublyLinkedListNode(val);
|
|
51
|
-
if (this._size === 0) {
|
|
52
|
-
this._first = newNode;
|
|
53
|
-
this._last = newNode;
|
|
54
|
-
} else {
|
|
55
|
-
if (this._last) this._last.next = newNode;
|
|
56
|
-
newNode.prev = this._last;
|
|
57
|
-
this._last = newNode;
|
|
58
|
-
}
|
|
59
|
-
this._size++;
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
peekFirst(): T | null;
|
|
64
|
-
peekFirst(by: 'val'): T | null;
|
|
65
|
-
peekFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
66
|
-
peekFirst(by?: DoublyLinkedListGetBy): T | DoublyLinkedListNode<T> | null {
|
|
67
|
-
switch (by) {
|
|
68
|
-
case 'node':
|
|
69
|
-
return this._first ?? null;
|
|
70
|
-
case 'val':
|
|
71
|
-
return this._first?.val ?? null;
|
|
72
|
-
default:
|
|
73
|
-
return this._first?.val ?? null;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
peekLast(): T | null;
|
|
78
|
-
peekLast(by: 'val'): T | null;
|
|
79
|
-
peekLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
80
|
-
peekLast(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
81
|
-
switch (by) {
|
|
82
|
-
case 'node':
|
|
83
|
-
return this._last ?? null;
|
|
84
|
-
case 'val':
|
|
85
|
-
return this._last?.val ?? null;
|
|
86
|
-
default:
|
|
87
|
-
return this._last?.val ?? null;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
pollFirst(): T | null;
|
|
92
|
-
pollFirst(by: 'val'): T | null;
|
|
93
|
-
pollFirst(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
94
|
-
/**
|
|
95
|
-
* Removes a node form the beginning of the linked list and will return the node val
|
|
96
|
-
*/
|
|
97
|
-
pollFirst(by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
98
|
-
if (this._size === 0) return null;
|
|
99
|
-
const oldHead = this._first;
|
|
100
|
-
if (this._size === 1) {
|
|
101
|
-
this._first = null;
|
|
102
|
-
this._last = null;
|
|
103
|
-
} else {
|
|
104
|
-
this._first = oldHead?.next ?? null;
|
|
105
|
-
if (this._first) this._first.prev = null;
|
|
106
|
-
if (oldHead) oldHead.next = null;
|
|
107
|
-
}
|
|
108
|
-
this._size--;
|
|
109
|
-
switch (by) {
|
|
110
|
-
case 'node':
|
|
111
|
-
return oldHead ?? null;
|
|
112
|
-
case 'val':
|
|
113
|
-
return oldHead?.val ?? null;
|
|
114
|
-
default:
|
|
115
|
-
return oldHead?.val ?? null;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
pollLast(): T | null;
|
|
120
|
-
pollLast(by: 'val'): T | null;
|
|
121
|
-
pollLast(by: 'node'): DoublyLinkedListNode<T> | null;
|
|
122
|
-
/**
|
|
123
|
-
* Removes a node at the end of the linked list and will return the node value
|
|
124
|
-
*/
|
|
125
|
-
pollLast(by: DoublyLinkedListGetBy = 'val'): DoublyLinkedListNode<T> | T | null {
|
|
126
|
-
if (this._size === 0) return null;
|
|
127
|
-
const polled = this._last;
|
|
128
|
-
if (this._size === 1) {
|
|
129
|
-
this._first = null;
|
|
130
|
-
this._last = null;
|
|
131
|
-
} else {
|
|
132
|
-
this._last = polled?.prev ?? null;
|
|
133
|
-
if (this._last) this._last.next = null;
|
|
134
|
-
if (polled) polled.prev = null;
|
|
135
|
-
}
|
|
136
|
-
this._size--;
|
|
137
|
-
switch (by) {
|
|
138
|
-
case 'node':
|
|
139
|
-
return polled ?? null;
|
|
140
|
-
case 'val':
|
|
141
|
-
return polled?.val ?? null;
|
|
142
|
-
default:
|
|
143
|
-
return polled?.val ?? null;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
get(index: number): T | null;
|
|
148
|
-
get(index: number, by: 'node'): DoublyLinkedListNode<T> | null;
|
|
149
|
-
get(index: number, by: 'val'): T | null;
|
|
150
|
-
/**
|
|
151
|
-
* Returns the node at the specified index of the linked list.
|
|
152
|
-
* If index = 0; first element in the list is returned.
|
|
153
|
-
* If index = 3; fourth element in the list is returned.
|
|
154
|
-
* @param index Index of the node to be retrieved
|
|
155
|
-
* @param by Return value type
|
|
156
|
-
*/
|
|
157
|
-
get(index: number, by: DoublyLinkedListGetBy = 'val'): T | DoublyLinkedListNode<T> | null {
|
|
158
|
-
if (index < 0 || index >= this._size) return null;
|
|
159
|
-
let count, current;
|
|
160
|
-
if (index <= this._size / 2) {
|
|
161
|
-
count = 0;
|
|
162
|
-
current = this._first;
|
|
163
|
-
while (count !== index) {
|
|
164
|
-
current = current?.next;
|
|
165
|
-
count++;
|
|
166
|
-
}
|
|
167
|
-
} else {
|
|
168
|
-
count = this._size - 1;
|
|
169
|
-
current = this._last;
|
|
170
|
-
while (count !== index) {
|
|
171
|
-
current = current?.prev;
|
|
172
|
-
count--;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
switch (by) {
|
|
176
|
-
case 'node':
|
|
177
|
-
return current ?? null;
|
|
178
|
-
case 'val':
|
|
179
|
-
return current?.val ?? null;
|
|
180
|
-
default:
|
|
181
|
-
return current?.val ?? null;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Updates the value of the node at the specified index.
|
|
187
|
-
* If index = 0; Value of the first element in the list is updated.
|
|
188
|
-
* If index = 3; Value of the fourth element in the list is updated.
|
|
189
|
-
* @param index Index of the node to be updated
|
|
190
|
-
* @param val New value of the node
|
|
191
|
-
*/
|
|
192
|
-
set(index: number, val: T): boolean {
|
|
193
|
-
const foundNode = this.get(index, 'node');
|
|
194
|
-
if (foundNode !== null) {
|
|
195
|
-
foundNode.val = val;
|
|
196
|
-
return true;
|
|
197
|
-
}
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
isEmpty() {
|
|
202
|
-
return this._size === 0;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// --- start extra methods ---
|
|
206
|
-
/**
|
|
207
|
-
* Inserts a new node at the specified index.
|
|
208
|
-
* @param index Index at which the new node has to be inserted
|
|
209
|
-
* @param val Value of the new node to be inserted
|
|
210
|
-
*/
|
|
211
|
-
insert(index: number, val: T): boolean {
|
|
212
|
-
if (index < 0 || index > this._size) return false;
|
|
213
|
-
if (index === 0) return !!this.offerFirst(val);
|
|
214
|
-
if (index === this._size) return !!this.offerLast(val);
|
|
215
|
-
|
|
216
|
-
const newNode = new DoublyLinkedListNode(val);
|
|
217
|
-
const prevNode = this.get(index - 1, 'node');
|
|
218
|
-
const nextNode = prevNode?.next;
|
|
219
|
-
|
|
220
|
-
if (prevNode) prevNode.next = newNode;
|
|
221
|
-
newNode.prev = prevNode;
|
|
222
|
-
newNode.next = nextNode ?? null;
|
|
223
|
-
if (nextNode) nextNode.prev = newNode;
|
|
224
|
-
this._size++;
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Removes a node at the specified index and returns its value.
|
|
230
|
-
* @param index Index at which the node has to be removed.
|
|
231
|
-
*/
|
|
232
|
-
remove(index: number): T | null {
|
|
233
|
-
if (index < 0 || index > this._size - 1) return null;
|
|
234
|
-
else if (index === 0) return this.pollFirst();
|
|
235
|
-
else if (index === this._size - 1) return this.pollLast('node')?.val ?? null;
|
|
236
|
-
else {
|
|
237
|
-
const prevNode = this.get(index - 1, 'node');
|
|
238
|
-
const removeNode = prevNode?.next;
|
|
239
|
-
const nextNode = removeNode?.next;
|
|
240
|
-
if (prevNode) prevNode.next = nextNode ?? null;
|
|
241
|
-
if (nextNode) nextNode.prev = prevNode;
|
|
242
|
-
if (removeNode) removeNode.next = null;
|
|
243
|
-
if (removeNode) removeNode.prev = null;
|
|
244
|
-
this._size--;
|
|
245
|
-
return removeNode?.val ?? null;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// --- end extra methods ---
|
|
250
|
-
}
|