data-structure-typed 0.8.18 → 0.9.16
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/modules.xml +1 -1
- package/README.md +197 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
- package/dist/data-structures/binary-tree/avl-tree.js +93 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
- package/dist/data-structures/binary-tree/binary-tree.js +480 -370
- package/dist/data-structures/binary-tree/bst.d.ts +4 -8
- package/dist/data-structures/binary-tree/bst.js +152 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/segment-tree.js +91 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
- package/dist/data-structures/graph/abstract-graph.js +546 -311
- package/dist/data-structures/graph/directed-graph.d.ts +5 -13
- package/dist/data-structures/graph/directed-graph.js +250 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
- package/dist/data-structures/graph/undirected-graph.js +166 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
- package/dist/data-structures/hash/coordinate-map.js +38 -20
- package/dist/data-structures/hash/coordinate-set.js +33 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +2 -8
- package/dist/data-structures/heap/heap.js +36 -31
- package/dist/data-structures/heap/max-heap.d.ts +3 -2
- package/dist/data-structures/heap/max-heap.js +27 -9
- package/dist/data-structures/heap/min-heap.d.ts +3 -2
- package/dist/data-structures/heap/min-heap.js +27 -9
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
- package/dist/data-structures/matrix/matrix.js +8 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
- package/dist/data-structures/matrix/matrix2d.js +80 -63
- package/dist/data-structures/matrix/navigator.d.ts +2 -16
- package/dist/data-structures/matrix/navigator.js +37 -18
- package/dist/data-structures/matrix/vector2d.d.ts +18 -18
- package/dist/data-structures/matrix/vector2d.js +117 -94
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
- package/dist/data-structures/priority-queue/priority-queue.js +159 -116
- package/dist/data-structures/queue/deque.js +82 -56
- package/dist/data-structures/queue/queue.d.ts +9 -10
- package/dist/data-structures/queue/queue.js +34 -34
- package/dist/data-structures/stack/stack.d.ts +9 -10
- package/dist/data-structures/stack/stack.js +31 -31
- package/dist/data-structures/trampoline.d.ts +14 -23
- package/dist/data-structures/trampoline.js +103 -25
- package/dist/data-structures/trie/trie.d.ts +13 -3
- package/dist/data-structures/trie/trie.js +234 -80
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/{types → data-structures/types}/utils.d.ts +7 -1
- package/dist/{types → data-structures/types}/utils.js +20 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
- package/dist/utils/utils.js +651 -0
- package/package.json +20 -42
- package/src/data-structures/binary-tree/avl-tree.ts +1 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
- package/src/data-structures/binary-tree/binary-tree.ts +184 -139
- package/src/data-structures/binary-tree/bst.ts +15 -24
- 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/segment-tree.ts +20 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
- package/src/data-structures/diagrams/README.md +7 -0
- package/src/data-structures/graph/abstract-graph.ts +58 -94
- 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 +12 -28
- package/src/data-structures/graph/undirected-graph.ts +11 -10
- package/src/data-structures/hash/coordinate-map.ts +1 -1
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +2 -11
- package/src/data-structures/heap/max-heap.ts +3 -2
- package/src/data-structures/heap/min-heap.ts +3 -2
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
- package/src/data-structures/matrix/matrix2d.ts +11 -11
- package/src/data-structures/matrix/navigator.ts +2 -14
- package/src/data-structures/matrix/vector2d.ts +52 -52
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/priority-queue.ts +70 -78
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +12 -13
- package/src/data-structures/stack/stack.ts +12 -13
- package/src/data-structures/trampoline.ts +31 -71
- package/src/data-structures/trie/trie.ts +61 -11
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +12 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +15 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/{types → data-structures/types}/utils.ts +20 -5
- package/src/utils/index.ts +1 -0
- package/src/{utils.ts → utils/utils.ts} +32 -132
- package/tsconfig.json +9 -6
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/index.d.ts +0 -1
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
|
@@ -1,22 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* @copyright 2020
|
|
3
|
+
* @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
+
var __extends = (this && this.__extends) || (function () {
|
|
7
|
+
var extendStatics = function (d, b) {
|
|
8
|
+
extendStatics = Object.setPrototypeOf ||
|
|
9
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
10
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
11
|
+
return extendStatics(d, b);
|
|
12
|
+
};
|
|
13
|
+
return function (d, b) {
|
|
14
|
+
if (typeof b !== "function" && b !== null)
|
|
15
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
16
|
+
extendStatics(d, b);
|
|
17
|
+
function __() { this.constructor = d; }
|
|
18
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
19
|
+
};
|
|
20
|
+
})();
|
|
6
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
22
|
exports.MaxHeap = void 0;
|
|
8
|
-
|
|
9
|
-
|
|
23
|
+
var heap_1 = require("./heap");
|
|
24
|
+
var priority_queue_1 = require("../priority-queue");
|
|
10
25
|
/**
|
|
11
26
|
* @class MaxHeap
|
|
12
27
|
* @extends Heap
|
|
13
28
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
29
|
+
var MaxHeap = /** @class */ (function (_super) {
|
|
30
|
+
__extends(MaxHeap, _super);
|
|
31
|
+
function MaxHeap(options) {
|
|
32
|
+
var _this = _super.call(this, options) || this;
|
|
33
|
+
_this._pq = new priority_queue_1.PriorityQueue({
|
|
34
|
+
comparator: function (a, b) { return b.priority - a.priority; }
|
|
19
35
|
});
|
|
36
|
+
return _this;
|
|
20
37
|
}
|
|
21
|
-
|
|
38
|
+
return MaxHeap;
|
|
39
|
+
}(heap_1.Heap));
|
|
22
40
|
exports.MaxHeap = MaxHeap;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @copyright 2020
|
|
2
|
+
* @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
|
|
3
3
|
* @license MIT
|
|
4
4
|
*/
|
|
5
|
-
import { Heap
|
|
5
|
+
import { Heap } from './heap';
|
|
6
6
|
import { PriorityQueue } from '../priority-queue';
|
|
7
|
+
import type { HeapItem, HeapOptions } from '../types';
|
|
7
8
|
/**
|
|
8
9
|
* @class MinHeap
|
|
9
10
|
* @extends Heap
|
|
@@ -1,22 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* @copyright 2020
|
|
3
|
+
* @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
+
var __extends = (this && this.__extends) || (function () {
|
|
7
|
+
var extendStatics = function (d, b) {
|
|
8
|
+
extendStatics = Object.setPrototypeOf ||
|
|
9
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
10
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
11
|
+
return extendStatics(d, b);
|
|
12
|
+
};
|
|
13
|
+
return function (d, b) {
|
|
14
|
+
if (typeof b !== "function" && b !== null)
|
|
15
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
16
|
+
extendStatics(d, b);
|
|
17
|
+
function __() { this.constructor = d; }
|
|
18
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
19
|
+
};
|
|
20
|
+
})();
|
|
6
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
22
|
exports.MinHeap = void 0;
|
|
8
|
-
|
|
9
|
-
|
|
23
|
+
var heap_1 = require("./heap");
|
|
24
|
+
var priority_queue_1 = require("../priority-queue");
|
|
10
25
|
/**
|
|
11
26
|
* @class MinHeap
|
|
12
27
|
* @extends Heap
|
|
13
28
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
29
|
+
var MinHeap = /** @class */ (function (_super) {
|
|
30
|
+
__extends(MinHeap, _super);
|
|
31
|
+
function MinHeap(options) {
|
|
32
|
+
var _this = _super.call(this, options) || this;
|
|
33
|
+
_this._pq = new priority_queue_1.PriorityQueue({
|
|
34
|
+
comparator: function (a, b) { return a.priority - b.priority; }
|
|
19
35
|
});
|
|
36
|
+
return _this;
|
|
20
37
|
}
|
|
21
|
-
|
|
38
|
+
return MinHeap;
|
|
39
|
+
}(heap_1.Heap));
|
|
22
40
|
exports.MinHeap = MinHeap;
|
|
@@ -1,40 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby
|
|
3
|
-
// 尾部插入 inject, snoc Append offerLast push push_back append push array_push push
|
|
4
|
-
// 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift
|
|
5
|
-
// 尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop
|
|
6
|
-
// 头部删除 pop Delete_First pollFirst shift pop_front popleft shift array_shift shift
|
|
7
|
-
// 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
|
|
8
|
-
// 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
3
|
exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
var DoublyLinkedListNode = /** @class */ (function () {
|
|
5
|
+
function DoublyLinkedListNode(nodeValue) {
|
|
13
6
|
this.val = nodeValue;
|
|
14
7
|
this.next = null;
|
|
15
8
|
this.prev = null;
|
|
16
9
|
}
|
|
17
|
-
|
|
10
|
+
return DoublyLinkedListNode;
|
|
11
|
+
}());
|
|
18
12
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
var DoublyLinkedList = /** @class */ (function () {
|
|
14
|
+
function DoublyLinkedList() {
|
|
21
15
|
this._first = null;
|
|
22
16
|
this._last = null;
|
|
23
17
|
this._size = 0;
|
|
24
18
|
// --- end extra methods ---
|
|
25
19
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
Object.defineProperty(DoublyLinkedList.prototype, "size", {
|
|
21
|
+
get: function () {
|
|
22
|
+
return this._size;
|
|
23
|
+
},
|
|
24
|
+
set: function (v) {
|
|
25
|
+
this._size = v;
|
|
26
|
+
},
|
|
27
|
+
enumerable: false,
|
|
28
|
+
configurable: true
|
|
29
|
+
});
|
|
32
30
|
/**
|
|
33
31
|
* Adds a node at the beginning of the linked list
|
|
34
32
|
* @param val Value to be stored at the beginning of the linked list
|
|
35
33
|
*/
|
|
36
|
-
offerFirst(val) {
|
|
37
|
-
|
|
34
|
+
DoublyLinkedList.prototype.offerFirst = function (val) {
|
|
35
|
+
var newNode = new DoublyLinkedListNode(val);
|
|
38
36
|
if (this._size === 0) {
|
|
39
37
|
this._first = newNode;
|
|
40
38
|
this._last = newNode;
|
|
@@ -47,13 +45,13 @@ class DoublyLinkedList {
|
|
|
47
45
|
}
|
|
48
46
|
this._size++;
|
|
49
47
|
return true;
|
|
50
|
-
}
|
|
48
|
+
};
|
|
51
49
|
/**
|
|
52
50
|
* Adds a node to the end of the linked list
|
|
53
51
|
* @param val Value to be stored in the Doubly linked list node
|
|
54
52
|
*/
|
|
55
|
-
offerLast(val) {
|
|
56
|
-
|
|
53
|
+
DoublyLinkedList.prototype.offerLast = function (val) {
|
|
54
|
+
var newNode = new DoublyLinkedListNode(val);
|
|
57
55
|
if (this._size === 0) {
|
|
58
56
|
this._first = newNode;
|
|
59
57
|
this._last = newNode;
|
|
@@ -66,8 +64,8 @@ class DoublyLinkedList {
|
|
|
66
64
|
}
|
|
67
65
|
this._size++;
|
|
68
66
|
return true;
|
|
69
|
-
}
|
|
70
|
-
peekFirst(by) {
|
|
67
|
+
};
|
|
68
|
+
DoublyLinkedList.prototype.peekFirst = function (by) {
|
|
71
69
|
var _a, _b, _c, _d, _e;
|
|
72
70
|
switch (by) {
|
|
73
71
|
case 'node':
|
|
@@ -77,9 +75,10 @@ class DoublyLinkedList {
|
|
|
77
75
|
default:
|
|
78
76
|
return (_e = (_d = this._first) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
|
|
79
77
|
}
|
|
80
|
-
}
|
|
81
|
-
peekLast
|
|
78
|
+
};
|
|
79
|
+
DoublyLinkedList.prototype.peekLast = function (by) {
|
|
82
80
|
var _a, _b, _c, _d, _e;
|
|
81
|
+
if (by === void 0) { by = 'val'; }
|
|
83
82
|
switch (by) {
|
|
84
83
|
case 'node':
|
|
85
84
|
return (_a = this._last) !== null && _a !== void 0 ? _a : null;
|
|
@@ -88,15 +87,16 @@ class DoublyLinkedList {
|
|
|
88
87
|
default:
|
|
89
88
|
return (_e = (_d = this._last) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
|
|
90
89
|
}
|
|
91
|
-
}
|
|
90
|
+
};
|
|
92
91
|
/**
|
|
93
92
|
* Removes a node form the beginning of the linked list and will return the node val
|
|
94
93
|
*/
|
|
95
|
-
pollFirst
|
|
94
|
+
DoublyLinkedList.prototype.pollFirst = function (by) {
|
|
96
95
|
var _a, _b, _c;
|
|
96
|
+
if (by === void 0) { by = 'val'; }
|
|
97
97
|
if (this._size === 0)
|
|
98
98
|
return null;
|
|
99
|
-
|
|
99
|
+
var oldHead = this._first;
|
|
100
100
|
if (this._size === 1) {
|
|
101
101
|
this._first = null;
|
|
102
102
|
this._last = null;
|
|
@@ -117,15 +117,16 @@ class DoublyLinkedList {
|
|
|
117
117
|
default:
|
|
118
118
|
return (_c = oldHead === null || oldHead === void 0 ? void 0 : oldHead.val) !== null && _c !== void 0 ? _c : null;
|
|
119
119
|
}
|
|
120
|
-
}
|
|
120
|
+
};
|
|
121
121
|
/**
|
|
122
122
|
* Removes a node at the end of the linked list and will return the node value
|
|
123
123
|
*/
|
|
124
|
-
pollLast
|
|
124
|
+
DoublyLinkedList.prototype.pollLast = function (by) {
|
|
125
125
|
var _a, _b, _c;
|
|
126
|
+
if (by === void 0) { by = 'val'; }
|
|
126
127
|
if (this._size === 0)
|
|
127
128
|
return null;
|
|
128
|
-
|
|
129
|
+
var polled = this._last;
|
|
129
130
|
if (this._size === 1) {
|
|
130
131
|
this._first = null;
|
|
131
132
|
this._last = null;
|
|
@@ -146,7 +147,7 @@ class DoublyLinkedList {
|
|
|
146
147
|
default:
|
|
147
148
|
return (_c = polled === null || polled === void 0 ? void 0 : polled.val) !== null && _c !== void 0 ? _c : null;
|
|
148
149
|
}
|
|
149
|
-
}
|
|
150
|
+
};
|
|
150
151
|
/**
|
|
151
152
|
* Returns the node at the specified index of the linked list.
|
|
152
153
|
* If index = 0; first element in the list is returned.
|
|
@@ -154,11 +155,12 @@ class DoublyLinkedList {
|
|
|
154
155
|
* @param index Index of the node to be retrieved
|
|
155
156
|
* @param by Return value type
|
|
156
157
|
*/
|
|
157
|
-
get(index, by
|
|
158
|
+
DoublyLinkedList.prototype.get = function (index, by) {
|
|
158
159
|
var _a, _b;
|
|
160
|
+
if (by === void 0) { by = 'val'; }
|
|
159
161
|
if (index < 0 || index >= this._size)
|
|
160
162
|
return null;
|
|
161
|
-
|
|
163
|
+
var count, current;
|
|
162
164
|
if (index <= this._size / 2) {
|
|
163
165
|
count = 0;
|
|
164
166
|
current = this._first;
|
|
@@ -183,7 +185,7 @@ class DoublyLinkedList {
|
|
|
183
185
|
default:
|
|
184
186
|
return (_b = current === null || current === void 0 ? void 0 : current.val) !== null && _b !== void 0 ? _b : null;
|
|
185
187
|
}
|
|
186
|
-
}
|
|
188
|
+
};
|
|
187
189
|
/**
|
|
188
190
|
* Updates the value of the node at the specified index.
|
|
189
191
|
* If index = 0; Value of the first element in the list is updated.
|
|
@@ -191,33 +193,33 @@ class DoublyLinkedList {
|
|
|
191
193
|
* @param index Index of the node to be updated
|
|
192
194
|
* @param val New value of the node
|
|
193
195
|
*/
|
|
194
|
-
set(index, val) {
|
|
195
|
-
|
|
196
|
+
DoublyLinkedList.prototype.set = function (index, val) {
|
|
197
|
+
var foundNode = this.get(index, 'node');
|
|
196
198
|
if (foundNode !== null) {
|
|
197
199
|
foundNode.val = val;
|
|
198
200
|
return true;
|
|
199
201
|
}
|
|
200
202
|
return false;
|
|
201
|
-
}
|
|
202
|
-
isEmpty() {
|
|
203
|
+
};
|
|
204
|
+
DoublyLinkedList.prototype.isEmpty = function () {
|
|
203
205
|
return this._size === 0;
|
|
204
|
-
}
|
|
206
|
+
};
|
|
205
207
|
// --- start extra methods ---
|
|
206
208
|
/**
|
|
207
209
|
* Inserts a new node at the specified index.
|
|
208
210
|
* @param index Index at which the new node has to be inserted
|
|
209
211
|
* @param val Value of the new node to be inserted
|
|
210
212
|
*/
|
|
211
|
-
insert(index, val) {
|
|
213
|
+
DoublyLinkedList.prototype.insert = function (index, val) {
|
|
212
214
|
if (index < 0 || index > this._size)
|
|
213
215
|
return false;
|
|
214
216
|
if (index === 0)
|
|
215
217
|
return !!this.offerFirst(val);
|
|
216
218
|
if (index === this._size)
|
|
217
219
|
return !!this.offerLast(val);
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
220
|
+
var newNode = new DoublyLinkedListNode(val);
|
|
221
|
+
var prevNode = this.get(index - 1, 'node');
|
|
222
|
+
var nextNode = prevNode === null || prevNode === void 0 ? void 0 : prevNode.next;
|
|
221
223
|
if (prevNode)
|
|
222
224
|
prevNode.next = newNode;
|
|
223
225
|
newNode.prev = prevNode;
|
|
@@ -226,12 +228,12 @@ class DoublyLinkedList {
|
|
|
226
228
|
nextNode.prev = newNode;
|
|
227
229
|
this._size++;
|
|
228
230
|
return true;
|
|
229
|
-
}
|
|
231
|
+
};
|
|
230
232
|
/**
|
|
231
233
|
* Removes a node at the specified index and returns its value.
|
|
232
234
|
* @param index Index at which the node has to be removed.
|
|
233
235
|
*/
|
|
234
|
-
remove(index) {
|
|
236
|
+
DoublyLinkedList.prototype.remove = function (index) {
|
|
235
237
|
var _a, _b, _c;
|
|
236
238
|
if (index < 0 || index > this._size - 1)
|
|
237
239
|
return null;
|
|
@@ -240,9 +242,9 @@ class DoublyLinkedList {
|
|
|
240
242
|
else if (index === this._size - 1)
|
|
241
243
|
return (_b = (_a = this.pollLast('node')) === null || _a === void 0 ? void 0 : _a.val) !== null && _b !== void 0 ? _b : null;
|
|
242
244
|
else {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
245
|
+
var prevNode = this.get(index - 1, 'node');
|
|
246
|
+
var removeNode = prevNode === null || prevNode === void 0 ? void 0 : prevNode.next;
|
|
247
|
+
var nextNode = removeNode === null || removeNode === void 0 ? void 0 : removeNode.next;
|
|
246
248
|
if (prevNode)
|
|
247
249
|
prevNode.next = nextNode !== null && nextNode !== void 0 ? nextNode : null;
|
|
248
250
|
if (nextNode)
|
|
@@ -254,6 +256,7 @@ class DoublyLinkedList {
|
|
|
254
256
|
this._size--;
|
|
255
257
|
return (_c = removeNode === null || removeNode === void 0 ? void 0 : removeNode.val) !== null && _c !== void 0 ? _c : null;
|
|
256
258
|
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
+
};
|
|
260
|
+
return DoublyLinkedList;
|
|
261
|
+
}());
|
|
259
262
|
exports.DoublyLinkedList = DoublyLinkedList;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
type TTestFunction<NodeData> = (data: NodeData, index: number, list: SinglyLinkedList<NodeData>) => boolean;
|
|
3
|
-
/** Type used for map and forEach methods, returning anything */
|
|
4
|
-
type TMapFunction<NodeData> = (data: any, index: number, list: SinglyLinkedList<NodeData>) => any;
|
|
1
|
+
import type { TMapFunction, TTestFunction } from '../types';
|
|
5
2
|
/**
|
|
6
3
|
* The class which represents one link or node in a linked list
|
|
7
4
|
* ```ts
|
|
@@ -72,6 +69,13 @@ export declare class SinglyLinkedListNode<NodeData = any> {
|
|
|
72
69
|
* ```
|
|
73
70
|
*/
|
|
74
71
|
export declare class SinglyLinkedList<NodeData = any> {
|
|
72
|
+
/** The head of the list, the first node */
|
|
73
|
+
head: SinglyLinkedListNode<NodeData> | null;
|
|
74
|
+
/** The tail of the list, the last node */
|
|
75
|
+
tail: SinglyLinkedListNode<NodeData> | null;
|
|
76
|
+
/** Internal size reference */
|
|
77
|
+
private size;
|
|
78
|
+
constructor(...args: NodeData[]);
|
|
75
79
|
/**
|
|
76
80
|
* The length of the list
|
|
77
81
|
*/
|
|
@@ -85,13 +89,6 @@ export declare class SinglyLinkedList<NodeData = any> {
|
|
|
85
89
|
* @param iterable Any iterable datatype like Array or Map
|
|
86
90
|
*/
|
|
87
91
|
static from<T>(iterable: Iterable<T>): SinglyLinkedList<T>;
|
|
88
|
-
/** The head of the list, the first node */
|
|
89
|
-
head: SinglyLinkedListNode<NodeData> | null;
|
|
90
|
-
/** The tail of the list, the last node */
|
|
91
|
-
tail: SinglyLinkedListNode<NodeData> | null;
|
|
92
|
-
/** Internal size reference */
|
|
93
|
-
private size;
|
|
94
|
-
constructor(...args: NodeData[]);
|
|
95
92
|
/**
|
|
96
93
|
* Get the node val at a specified index, zero based
|
|
97
94
|
* ```ts
|
|
@@ -355,4 +352,3 @@ export declare class SinglyLinkedList<NodeData = any> {
|
|
|
355
352
|
/** Private helper function to reduce duplication of pop() and shift() methods */
|
|
356
353
|
private removeFromAnyEnd;
|
|
357
354
|
}
|
|
358
|
-
export {};
|