@raikuxq/alg-ds 1.1.6 → 1.1.7
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/lib/algorithms/binary-search.d.ts +5 -0
- package/lib/algorithms/binary-search.js +27 -0
- package/lib/algorithms/factorial.d.ts +9 -0
- package/lib/algorithms/factorial.js +17 -0
- package/lib/algorithms/fibonacci.d.ts +9 -0
- package/lib/algorithms/fibonacci.js +17 -0
- package/lib/algorithms/memoize.d.ts +5 -0
- package/lib/algorithms/memoize.js +22 -0
- package/lib/algorithms/sorts/bubble-sort.d.ts +9 -0
- package/lib/algorithms/sorts/bubble-sort.js +23 -0
- package/lib/algorithms/sorts/insertion-sort.d.ts +9 -0
- package/lib/algorithms/sorts/insertion-sort.js +25 -0
- package/lib/algorithms/sorts/merge-sort.d.ts +9 -0
- package/lib/algorithms/sorts/merge-sort.js +61 -0
- package/lib/algorithms/sorts/quick-sort.d.ts +9 -0
- package/lib/algorithms/sorts/quick-sort.js +45 -0
- package/lib/algorithms/sorts/select-sort.d.ts +9 -0
- package/lib/algorithms/sorts/select-sort.js +20 -0
- package/lib/algorithms/transpose-matrix.d.ts +5 -0
- package/lib/algorithms/transpose-matrix.js +18 -0
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +6 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.d.ts +15 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.js +53 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.d.ts +60 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.js +36 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.d.ts +13 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.js +59 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.d.ts +70 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.js +271 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.d.ts +16 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.js +70 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.d.ts +57 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.js +235 -0
- package/lib/data-structures/Graph/AbstractGraph.d.ts +84 -0
- package/lib/data-structures/Graph/AbstractGraph.js +143 -0
- package/lib/data-structures/Graph/DirectedGraph.d.ts +24 -0
- package/lib/data-structures/Graph/DirectedGraph.js +86 -0
- package/lib/data-structures/Graph/GraphEdge.d.ts +16 -0
- package/lib/data-structures/Graph/GraphEdge.js +43 -0
- package/lib/data-structures/Graph/UndirectedGraph.d.ts +28 -0
- package/lib/data-structures/Graph/UndirectedGraph.js +103 -0
- package/lib/data-structures/Graph/demo/generateRandomGraph.d.ts +4 -0
- package/lib/data-structures/Graph/demo/generateRandomGraph.js +66 -0
- package/lib/data-structures/Graph/iterator/AbstractGraphIterator.d.ts +35 -0
- package/lib/data-structures/Graph/iterator/AbstractGraphIterator.js +90 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorBFS.d.ts +28 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorBFS.js +70 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDFS.d.ts +28 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDFS.js +70 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.d.ts +32 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.js +99 -0
- package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.d.ts +19 -0
- package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.js +28 -0
- package/lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.d.ts +32 -0
- package/lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.js +48 -0
- package/lib/data-structures/Graph/searching/hasPath.d.ts +9 -0
- package/lib/data-structures/Graph/searching/hasPath.js +30 -0
- package/lib/data-structures/Graph/searching/shortestPath.d.ts +9 -0
- package/lib/data-structures/Graph/searching/shortestPath.js +30 -0
- package/lib/data-structures/Graph/strategy/BFSIterationStrategy.d.ts +6 -0
- package/lib/data-structures/Graph/strategy/BFSIterationStrategy.js +13 -0
- package/lib/data-structures/Graph/strategy/DFSIterationStrategy.d.ts +6 -0
- package/lib/data-structures/Graph/strategy/DFSIterationStrategy.js +13 -0
- package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.d.ts +6 -0
- package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.js +13 -0
- package/lib/data-structures/Graph/transposing/transposeDirectedGraph.d.ts +2 -0
- package/lib/data-structures/Graph/transposing/transposeDirectedGraph.js +14 -0
- package/lib/data-structures/HashTable/HashTable.d.ts +73 -0
- package/lib/data-structures/HashTable/HashTable.js +171 -0
- package/lib/data-structures/HashTable/HashTableNode.d.ts +11 -0
- package/lib/data-structures/HashTable/HashTableNode.js +39 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.d.ts +125 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.js +241 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.d.ts +20 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.js +41 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.d.ts +48 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.js +151 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.d.ts +25 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.js +65 -0
- package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.d.ts +52 -0
- package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.js +138 -0
- package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.d.ts +7 -0
- package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.js +29 -0
- package/lib/data-structures/LoopedArray/LoopedArray.d.ts +83 -0
- package/lib/data-structures/LoopedArray/LoopedArray.js +169 -0
- package/lib/data-structures/Queue/Queue.d.ts +50 -0
- package/lib/data-structures/Queue/Queue.js +85 -0
- package/lib/data-structures/Stack/Stack.d.ts +50 -0
- package/lib/data-structures/Stack/Stack.js +85 -0
- package/lib/exceptions/IllegalCapacityException.d.ts +4 -0
- package/lib/exceptions/IllegalCapacityException.js +28 -0
- package/lib/exceptions/IndexOutOfBoundsException.d.ts +4 -0
- package/lib/exceptions/IndexOutOfBoundsException.js +28 -0
- package/lib/exceptions/IsAlreadyExistsException.d.ts +4 -0
- package/lib/exceptions/IsAlreadyExistsException.js +28 -0
- package/lib/exceptions/IsEmptyException.d.ts +4 -0
- package/lib/exceptions/IsEmptyException.js +28 -0
- package/lib/exceptions/IsFullException.d.ts +4 -0
- package/lib/exceptions/IsFullException.js +28 -0
- package/lib/exceptions/IsNotFoundException.d.ts +4 -0
- package/lib/exceptions/IsNotFoundException.js +28 -0
- package/lib/exceptions/base/IllegalArgumentException.d.ts +3 -0
- package/lib/exceptions/base/IllegalArgumentException.js +27 -0
- package/lib/exceptions/base/IllegalStateException.d.ts +3 -0
- package/lib/exceptions/base/IllegalStateException.js +27 -0
- package/lib/exports/algorithms.d.ts +16 -0
- package/lib/exports/algorithms.js +36 -0
- package/lib/exports/constants.d.ts +2 -0
- package/lib/exports/constants.js +7 -0
- package/lib/exports/data-structures.d.ts +11 -0
- package/lib/exports/data-structures.js +24 -0
- package/lib/exports/helpers.d.ts +6 -0
- package/lib/exports/helpers.js +14 -0
- package/lib/exports/sorts.d.ts +6 -0
- package/lib/exports/sorts.js +14 -0
- package/lib/exports/utils.d.ts +3 -0
- package/lib/exports/utils.js +14 -0
- package/lib/exports.d.ts +52 -0
- package/lib/exports.js +103 -0
- package/lib/helpers/createBinaryTree.d.ts +6 -0
- package/lib/helpers/createBinaryTree.js +22 -0
- package/lib/helpers/createGraph.d.ts +6 -0
- package/lib/helpers/createGraph.js +24 -0
- package/lib/helpers/createGraphFromMatrix.d.ts +7 -0
- package/lib/helpers/createGraphFromMatrix.js +42 -0
- package/lib/helpers/createLinkedList.d.ts +3 -0
- package/lib/helpers/createLinkedList.js +21 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +6 -0
- package/lib/types/ArrayMatrix.d.ts +1 -0
- package/lib/types/ArrayMatrix.js +3 -0
- package/lib/types/EnumBinarySearchTreeType.d.ts +4 -0
- package/lib/types/EnumBinarySearchTreeType.js +9 -0
- package/lib/types/EnumGraphType.d.ts +4 -0
- package/lib/types/EnumGraphType.js +9 -0
- package/lib/types/EnumLinkedListType.d.ts +4 -0
- package/lib/types/EnumLinkedListType.js +9 -0
- package/lib/types/EnumRandomGenerationFormat.d.ts +4 -0
- package/lib/types/EnumRandomGenerationFormat.js +9 -0
- package/lib/types/EnumTreeTraversalType.d.ts +5 -0
- package/lib/types/EnumTreeTraversalType.js +10 -0
- package/lib/types/FnCompareTwo.d.ts +1 -0
- package/lib/types/FnCompareTwo.js +3 -0
- package/lib/types/FnToMemoize.d.ts +1 -0
- package/lib/types/FnToMemoize.js +3 -0
- package/lib/types/IArrayFacade.d.ts +4 -0
- package/lib/types/IArrayFacade.js +3 -0
- package/lib/types/IBiDirectIterable.d.ts +5 -0
- package/lib/types/IBiDirectIterable.js +3 -0
- package/lib/types/IBiDirectIterator.d.ts +11 -0
- package/lib/types/IBiDirectIterator.js +3 -0
- package/lib/types/IBinaryTree.d.ts +12 -0
- package/lib/types/IBinaryTree.js +3 -0
- package/lib/types/IConvertableToArray.d.ts +4 -0
- package/lib/types/IConvertableToArray.js +3 -0
- package/lib/types/IGraph.d.ts +14 -0
- package/lib/types/IGraph.js +3 -0
- package/lib/types/IGraphIterationStrategy.d.ts +5 -0
- package/lib/types/IGraphIterationStrategy.js +3 -0
- package/lib/types/IGraphIterator.d.ts +11 -0
- package/lib/types/IGraphIterator.js +3 -0
- package/lib/types/IIterable.d.ts +4 -0
- package/lib/types/IIterable.js +3 -0
- package/lib/types/IIterator.d.ts +14 -0
- package/lib/types/IIterator.js +3 -0
- package/lib/types/IKeyValueStorage.d.ts +8 -0
- package/lib/types/IKeyValueStorage.js +3 -0
- package/lib/types/ILinearStorage.d.ts +11 -0
- package/lib/types/ILinearStorage.js +3 -0
- package/lib/types/ILinearStorageRA.d.ts +13 -0
- package/lib/types/ILinearStorageRA.js +3 -0
- package/lib/types/ILinkedList.d.ts +4 -0
- package/lib/types/ILinkedList.js +3 -0
- package/lib/utils.d.ts +33 -0
- package/lib/utils.js +108 -0
- package/package.json +1 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import AbstractLinkedNode from "./AbstractLinkedNode";
|
|
2
|
+
import ILinkedList from "../../../types/ILinkedList";
|
|
3
|
+
export default abstract class AbstractLinkedList<T> implements ILinkedList<T> {
|
|
4
|
+
protected readonly _capacity: number;
|
|
5
|
+
protected _length: number;
|
|
6
|
+
protected _head: AbstractLinkedNode<T> | null;
|
|
7
|
+
protected _tail: AbstractLinkedNode<T> | null;
|
|
8
|
+
/**
|
|
9
|
+
* Create empty instance
|
|
10
|
+
*/
|
|
11
|
+
protected constructor(capacity?: number);
|
|
12
|
+
/**
|
|
13
|
+
* Will calculate real capacity value
|
|
14
|
+
* @throws when capacity <= 0
|
|
15
|
+
*/
|
|
16
|
+
private static calculateCapacity;
|
|
17
|
+
/**
|
|
18
|
+
* Will insert node between nodeLeft and nodeRight
|
|
19
|
+
* @throws when list is full
|
|
20
|
+
*/
|
|
21
|
+
private insertNodeBetweenTwoNodes;
|
|
22
|
+
/**
|
|
23
|
+
* Will remove the node from its neighbors nodes links
|
|
24
|
+
* @throws when node does not exist
|
|
25
|
+
*/
|
|
26
|
+
private deleteNode;
|
|
27
|
+
/**
|
|
28
|
+
* Will find node by its index
|
|
29
|
+
* @throws when node was not found
|
|
30
|
+
*/
|
|
31
|
+
protected getNodeByIndex(index: number): AbstractLinkedNode<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Will set links between target, left and right siblings
|
|
34
|
+
*/
|
|
35
|
+
protected abstract insertNodeBetweenTwoNodesImpl(nodeToPush: AbstractLinkedNode<T>, nodeLeft: AbstractLinkedNode<T>, nodeRight: AbstractLinkedNode<T>): void;
|
|
36
|
+
/**
|
|
37
|
+
* Will unset itself links and its neighbors links
|
|
38
|
+
*/
|
|
39
|
+
protected abstract deleteNodeImpl(node: AbstractLinkedNode<T>): void;
|
|
40
|
+
/**
|
|
41
|
+
* Update head link
|
|
42
|
+
*/
|
|
43
|
+
protected abstract popImpl(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Update tail link
|
|
46
|
+
*/
|
|
47
|
+
protected abstract shiftImpl(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Will create empty node instance
|
|
50
|
+
*/
|
|
51
|
+
protected abstract createNode(value: T): AbstractLinkedNode<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Push into start
|
|
54
|
+
*/
|
|
55
|
+
unshift(value: T): void;
|
|
56
|
+
/**
|
|
57
|
+
* Push into end
|
|
58
|
+
*/
|
|
59
|
+
push(value: T): void;
|
|
60
|
+
/**
|
|
61
|
+
* Push from index
|
|
62
|
+
*/
|
|
63
|
+
pushFromIndex(value: T, fromIndex: number): void;
|
|
64
|
+
/**
|
|
65
|
+
* Delete node from list's end
|
|
66
|
+
*/
|
|
67
|
+
pop(): T;
|
|
68
|
+
/**
|
|
69
|
+
* Delete node from list's start and get its data
|
|
70
|
+
*/
|
|
71
|
+
shift(): T;
|
|
72
|
+
/**
|
|
73
|
+
* Delete node from list by index from start
|
|
74
|
+
*/
|
|
75
|
+
deleteFromIndex(fromIndex: number): T;
|
|
76
|
+
/**
|
|
77
|
+
* List length
|
|
78
|
+
*/
|
|
79
|
+
length(): number;
|
|
80
|
+
/**
|
|
81
|
+
* Is list empty
|
|
82
|
+
*/
|
|
83
|
+
isEmpty(): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Is list full
|
|
86
|
+
*/
|
|
87
|
+
isFull(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Check if element exists in list
|
|
90
|
+
*/
|
|
91
|
+
has(item: T): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Get head element data
|
|
94
|
+
* @throws when head does not exist
|
|
95
|
+
*/
|
|
96
|
+
peek(): T;
|
|
97
|
+
/**
|
|
98
|
+
* Get tail element data
|
|
99
|
+
* @throws when tail does not exists
|
|
100
|
+
*/
|
|
101
|
+
peekFromStart(): T;
|
|
102
|
+
/**
|
|
103
|
+
* Get list element by index from start
|
|
104
|
+
* @throws when element does not exist
|
|
105
|
+
*/
|
|
106
|
+
peekByIndex(index: number): T;
|
|
107
|
+
/**
|
|
108
|
+
* Remove all elements from list
|
|
109
|
+
*/
|
|
110
|
+
clear(): void;
|
|
111
|
+
/**
|
|
112
|
+
* Get elements as an array
|
|
113
|
+
*/
|
|
114
|
+
getAsArray(): Array<T>;
|
|
115
|
+
/**
|
|
116
|
+
* Add elements to list from array
|
|
117
|
+
* @throws when list is full
|
|
118
|
+
* */
|
|
119
|
+
pushFromArray(elements: Array<T>): void;
|
|
120
|
+
/**
|
|
121
|
+
* Reverse list nodes links and swap head with tail
|
|
122
|
+
* @example "4>7>10" will be reversed to "10>7>4"
|
|
123
|
+
*/
|
|
124
|
+
abstract reverse(): void;
|
|
125
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var IllegalCapacityException_1 = require("../../../exceptions/IllegalCapacityException");
|
|
4
|
+
var IsFullException_1 = require("../../../exceptions/IsFullException");
|
|
5
|
+
var IsEmptyException_1 = require("../../../exceptions/IsEmptyException");
|
|
6
|
+
var IndexOutOfBoundsException_1 = require("../../../exceptions/IndexOutOfBoundsException");
|
|
7
|
+
var AbstractLinkedList = /** @class */ (function () {
|
|
8
|
+
/**
|
|
9
|
+
* Create empty instance
|
|
10
|
+
*/
|
|
11
|
+
function AbstractLinkedList(capacity) {
|
|
12
|
+
this._capacity = AbstractLinkedList.calculateCapacity(capacity);
|
|
13
|
+
this._head = null;
|
|
14
|
+
this._tail = null;
|
|
15
|
+
this._length = 0;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Will calculate real capacity value
|
|
19
|
+
* @throws when capacity <= 0
|
|
20
|
+
*/
|
|
21
|
+
AbstractLinkedList.calculateCapacity = function (capacity) {
|
|
22
|
+
if (capacity === undefined) {
|
|
23
|
+
return Number.MAX_VALUE;
|
|
24
|
+
}
|
|
25
|
+
if (capacity <= 0) {
|
|
26
|
+
throw new IllegalCapacityException_1.default("Capacity must be larger than 0");
|
|
27
|
+
}
|
|
28
|
+
return capacity;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Will insert node between nodeLeft and nodeRight
|
|
32
|
+
* @throws when list is full
|
|
33
|
+
*/
|
|
34
|
+
AbstractLinkedList.prototype.insertNodeBetweenTwoNodes = function (targetNode, leftNode, rightNode) {
|
|
35
|
+
if (this.isFull()) {
|
|
36
|
+
throw new IsFullException_1.default("List is full, no more space available");
|
|
37
|
+
}
|
|
38
|
+
if (this._head === null) {
|
|
39
|
+
this._head = targetNode;
|
|
40
|
+
}
|
|
41
|
+
if (this._tail === null) {
|
|
42
|
+
this._tail = targetNode;
|
|
43
|
+
}
|
|
44
|
+
if (!leftNode) {
|
|
45
|
+
leftNode = this._tail;
|
|
46
|
+
}
|
|
47
|
+
if (!rightNode) {
|
|
48
|
+
rightNode = this._head;
|
|
49
|
+
}
|
|
50
|
+
this.insertNodeBetweenTwoNodesImpl(targetNode, leftNode, rightNode);
|
|
51
|
+
this._length++;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Will remove the node from its neighbors nodes links
|
|
55
|
+
* @throws when node does not exist
|
|
56
|
+
*/
|
|
57
|
+
AbstractLinkedList.prototype.deleteNode = function (node) {
|
|
58
|
+
this.deleteNodeImpl(node);
|
|
59
|
+
this._length--;
|
|
60
|
+
if (this.isEmpty()) {
|
|
61
|
+
this.clear();
|
|
62
|
+
}
|
|
63
|
+
return node;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Will find node by its index
|
|
67
|
+
* @throws when node was not found
|
|
68
|
+
*/
|
|
69
|
+
AbstractLinkedList.prototype.getNodeByIndex = function (index) {
|
|
70
|
+
var isIndexNotInRange = index < 0 || index > this._length;
|
|
71
|
+
if (this.isEmpty()) {
|
|
72
|
+
throw new IsEmptyException_1.default("List is empty");
|
|
73
|
+
}
|
|
74
|
+
if (isIndexNotInRange) {
|
|
75
|
+
throw new IndexOutOfBoundsException_1.default("Index exceed list length");
|
|
76
|
+
}
|
|
77
|
+
var currentNode = this._tail;
|
|
78
|
+
var counter = 0;
|
|
79
|
+
while (currentNode && counter < index) {
|
|
80
|
+
currentNode = currentNode.next;
|
|
81
|
+
counter++;
|
|
82
|
+
}
|
|
83
|
+
return currentNode;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Push into start
|
|
87
|
+
*/
|
|
88
|
+
AbstractLinkedList.prototype.unshift = function (value) {
|
|
89
|
+
var node = this.createNode(value);
|
|
90
|
+
this.insertNodeBetweenTwoNodes(node, this._head, this._tail);
|
|
91
|
+
this._tail = node;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Push into end
|
|
95
|
+
*/
|
|
96
|
+
AbstractLinkedList.prototype.push = function (value) {
|
|
97
|
+
var node = this.createNode(value);
|
|
98
|
+
this.insertNodeBetweenTwoNodes(node, this._head, this._tail);
|
|
99
|
+
this._head = node;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Push from index
|
|
103
|
+
*/
|
|
104
|
+
AbstractLinkedList.prototype.pushFromIndex = function (value, fromIndex) {
|
|
105
|
+
var isIndexNotInRange = fromIndex < 0 || fromIndex > this._length;
|
|
106
|
+
var shouldPushAsFirst = this.isEmpty() && fromIndex === 0;
|
|
107
|
+
if (isIndexNotInRange) {
|
|
108
|
+
throw new IndexOutOfBoundsException_1.default("index must be in range between 0 and list length");
|
|
109
|
+
}
|
|
110
|
+
if (shouldPushAsFirst) {
|
|
111
|
+
this.push(value);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
var node = this.createNode(value);
|
|
115
|
+
var nodeLeft = this.getNodeByIndex(fromIndex - 1);
|
|
116
|
+
var nodeRight = this.getNodeByIndex(fromIndex);
|
|
117
|
+
this.insertNodeBetweenTwoNodes(node, nodeLeft, nodeRight);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Delete node from list's end
|
|
122
|
+
*/
|
|
123
|
+
AbstractLinkedList.prototype.pop = function () {
|
|
124
|
+
if (this.isEmpty() || this._head === null) {
|
|
125
|
+
throw new IsEmptyException_1.default("cannot delete because list is empty");
|
|
126
|
+
}
|
|
127
|
+
var deletedNode = this.deleteNode(this._head);
|
|
128
|
+
this.popImpl();
|
|
129
|
+
return deletedNode.data;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Delete node from list's start and get its data
|
|
133
|
+
*/
|
|
134
|
+
AbstractLinkedList.prototype.shift = function () {
|
|
135
|
+
if (this.isEmpty() || this._tail === null) {
|
|
136
|
+
throw new IsEmptyException_1.default("cannot delete because list is empty");
|
|
137
|
+
}
|
|
138
|
+
var deletedNode = this.deleteNode(this._tail);
|
|
139
|
+
this.shiftImpl();
|
|
140
|
+
return deletedNode.data;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Delete node from list by index from start
|
|
144
|
+
*/
|
|
145
|
+
AbstractLinkedList.prototype.deleteFromIndex = function (fromIndex) {
|
|
146
|
+
var nodeToDelete = this.getNodeByIndex(fromIndex);
|
|
147
|
+
var deletedNode = this.deleteNode(nodeToDelete);
|
|
148
|
+
return deletedNode.data;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* List length
|
|
152
|
+
*/
|
|
153
|
+
AbstractLinkedList.prototype.length = function () {
|
|
154
|
+
return this._length;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Is list empty
|
|
158
|
+
*/
|
|
159
|
+
AbstractLinkedList.prototype.isEmpty = function () {
|
|
160
|
+
return this._length === 0;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Is list full
|
|
164
|
+
*/
|
|
165
|
+
AbstractLinkedList.prototype.isFull = function () {
|
|
166
|
+
return this._length >= this._capacity;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Check if element exists in list
|
|
170
|
+
*/
|
|
171
|
+
AbstractLinkedList.prototype.has = function (item) {
|
|
172
|
+
return this.getAsArray().includes(item);
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Get head element data
|
|
176
|
+
* @throws when head does not exist
|
|
177
|
+
*/
|
|
178
|
+
AbstractLinkedList.prototype.peek = function () {
|
|
179
|
+
if (this.isEmpty() || !this._head) {
|
|
180
|
+
throw new IsEmptyException_1.default("head does not exist");
|
|
181
|
+
}
|
|
182
|
+
return this._head.data;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Get tail element data
|
|
186
|
+
* @throws when tail does not exists
|
|
187
|
+
*/
|
|
188
|
+
AbstractLinkedList.prototype.peekFromStart = function () {
|
|
189
|
+
if (this.isEmpty() || !this._tail) {
|
|
190
|
+
throw new IsEmptyException_1.default("tail does not exist");
|
|
191
|
+
}
|
|
192
|
+
return this._tail.data;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Get list element by index from start
|
|
196
|
+
* @throws when element does not exist
|
|
197
|
+
*/
|
|
198
|
+
AbstractLinkedList.prototype.peekByIndex = function (index) {
|
|
199
|
+
var node = this.getNodeByIndex(index);
|
|
200
|
+
return node.data;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Remove all elements from list
|
|
204
|
+
*/
|
|
205
|
+
AbstractLinkedList.prototype.clear = function () {
|
|
206
|
+
this._head = null;
|
|
207
|
+
this._tail = null;
|
|
208
|
+
this._length = 0;
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* Get elements as an array
|
|
212
|
+
*/
|
|
213
|
+
AbstractLinkedList.prototype.getAsArray = function () {
|
|
214
|
+
var array = [];
|
|
215
|
+
var currentNode = this._tail;
|
|
216
|
+
var counter = 0;
|
|
217
|
+
while (currentNode && counter < this._length) {
|
|
218
|
+
if (currentNode)
|
|
219
|
+
array.push(currentNode.data);
|
|
220
|
+
currentNode = currentNode.next;
|
|
221
|
+
counter++;
|
|
222
|
+
}
|
|
223
|
+
return array;
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* Add elements to list from array
|
|
227
|
+
* @throws when list is full
|
|
228
|
+
* */
|
|
229
|
+
AbstractLinkedList.prototype.pushFromArray = function (elements) {
|
|
230
|
+
var _this = this;
|
|
231
|
+
elements.forEach(function (element) {
|
|
232
|
+
if (_this.isFull()) {
|
|
233
|
+
throw new IsFullException_1.default("List is full, no more space available");
|
|
234
|
+
}
|
|
235
|
+
_this.push(element);
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
return AbstractLinkedList;
|
|
239
|
+
}());
|
|
240
|
+
exports.default = AbstractLinkedList;
|
|
241
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQWJzdHJhY3RMaW5rZWRMaXN0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2RhdGEtc3RydWN0dXJlcy9MaW5rZWRMaXN0L0Fic3RyYWN0TGlua2VkTGlzdC9BYnN0cmFjdExpbmtlZExpc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFFQSx5RkFBb0Y7QUFDcEYsdUVBQWtFO0FBQ2xFLHlFQUFvRTtBQ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FBQyxDQUFDO1lBRTlDLFdBQVcsR0FBRyxXQUFXLENBQUMsSUFBSSxDQUFDO1lBQy9CLE9BQU8sRUFBRSxDQUFDO1NBQ1g7UUFFRCxPQUFPLEtBQUssQ0FBQztJQUNmLENBQUM7SUFFRDs7O1NBR0s7SUFDRSwwQ0FBYSxHQUFwQixVQUFxQixRQUFrQjtRQUF2QyxpQkFPQztRQU5DLFFBQVEsQ0FBQyxPQUFPLENBQUMsVUFBQyxPQUFVO1lBQzFCLElBQUksS0FBSSxDQUFDLE1BQU0sRUFBRSxFQUFFO2dCQUNqQixNQUFNLElBQUkseUJBQWUsQ0FBQyx1Q0FBdUMsQ0FBQyxDQUFDO2FBQ3BFO1lBQ0QsS0FBSSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQztRQUNyQixDQUFDLENBQUMsQ0FBQztJQUNMLENBQUM7SUFPSCx5QkFBQztBQUFELENBQUMsQUFuVEQsSUFtVEMifQ==
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export default abstract class AbstractLinkedNode<T> {
|
|
2
|
+
protected _next: AbstractLinkedNode<T> | null;
|
|
3
|
+
protected readonly _data: T;
|
|
4
|
+
/**
|
|
5
|
+
* Will create empty node
|
|
6
|
+
*/
|
|
7
|
+
protected constructor(data: T, next?: AbstractLinkedNode<T> | null);
|
|
8
|
+
/**
|
|
9
|
+
* Get data of node
|
|
10
|
+
*/
|
|
11
|
+
get data(): T;
|
|
12
|
+
/**
|
|
13
|
+
* Get next node link
|
|
14
|
+
*/
|
|
15
|
+
get next(): AbstractLinkedNode<T> | null;
|
|
16
|
+
/**
|
|
17
|
+
* Set next node link
|
|
18
|
+
*/
|
|
19
|
+
set next(value: AbstractLinkedNode<T> | null);
|
|
20
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var AbstractLinkedNode = /** @class */ (function () {
|
|
4
|
+
/**
|
|
5
|
+
* Will create empty node
|
|
6
|
+
*/
|
|
7
|
+
function AbstractLinkedNode(data, next) {
|
|
8
|
+
if (next === void 0) { next = null; }
|
|
9
|
+
this._data = data;
|
|
10
|
+
this._next = next;
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(AbstractLinkedNode.prototype, "data", {
|
|
13
|
+
/**
|
|
14
|
+
* Get data of node
|
|
15
|
+
*/
|
|
16
|
+
get: function () {
|
|
17
|
+
return this._data;
|
|
18
|
+
},
|
|
19
|
+
enumerable: false,
|
|
20
|
+
configurable: true
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(AbstractLinkedNode.prototype, "next", {
|
|
23
|
+
/**
|
|
24
|
+
* Get next node link
|
|
25
|
+
*/
|
|
26
|
+
get: function () {
|
|
27
|
+
return this._next;
|
|
28
|
+
},
|
|
29
|
+
/**
|
|
30
|
+
* Set next node link
|
|
31
|
+
*/
|
|
32
|
+
set: function (value) {
|
|
33
|
+
this._next = value;
|
|
34
|
+
},
|
|
35
|
+
enumerable: false,
|
|
36
|
+
configurable: true
|
|
37
|
+
});
|
|
38
|
+
return AbstractLinkedNode;
|
|
39
|
+
}());
|
|
40
|
+
exports.default = AbstractLinkedNode;
|
|
41
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQWJzdHJhY3RMaW5rZWROb2RlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2RhdGEtc3RydWN0dXJlcy9MaW5rZWRMaXN0L0Fic3RyYWN0TGlua2VkTGlzdC9BYnN0cmFjdExpbmtlZE5vZGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQTtJQUlFOztPQUVHO0lBQ0gsNEJBQXNCLElBQU8sRUFBRSxJQUF5QztRQUF6QyxxQkFBQSxFQUFBLFdBQXlDO1FBQ3RFLElBQUksQ0FBQyxLQUFLLEdBQUcsSUFBSSxDQUFDO1FBQ2xCLElBQUksQ0FBQyxLQUFLLEdBQUcsSUFBSSxDQUFDO0lBQ3BCLENBQUM7SUFLRCxzQkFBVyxvQ0FBSTtRQUhmOztXQUVHO2FBQ0g7WUFDRSxPQUFPLElBQUksQ0FBQyxLQUFLLENBQUM7UUFDcEIsQ0FBQzs7O09BQUE7SUFLRCxzQkFBVyxvQ0FBSTtRQUhmOztXQUVHO2FBQ0g7WUFDRSxPQUFPLElBQUksQ0FBQyxLQUFLLENBQUM7UUFDcEIsQ0FBQztRQUVEOztXQUVHO2FBQ0gsVUFBZ0IsS0FBbUM7WUFDakQsSUFBSSxDQUFDLEtBQUssR0FBRyxLQUFLLENBQUM7UUFDckIsQ0FBQzs7O09BUEE7SUFRSCx5QkFBQztBQUFELENBQUMsQUFoQ0QsSUFnQ0MifQ==
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import IBiDirectIterator from "../../../types/IBiDirectIterator";
|
|
2
|
+
import IBiDirectIterable from "../../../types/IBiDirectIterable";
|
|
3
|
+
import AbstractLinkedList from "../AbstractLinkedList/AbstractLinkedList";
|
|
4
|
+
import DoubleLinkedNode from "./DoubleLinkedNode";
|
|
5
|
+
/**
|
|
6
|
+
* Linear data structure
|
|
7
|
+
* Each node has next and prev sibling
|
|
8
|
+
* Head and tail are linked to each other
|
|
9
|
+
*/
|
|
10
|
+
export default class DoubleLinkedList<T> extends AbstractLinkedList<T> implements IBiDirectIterable<T> {
|
|
11
|
+
/**
|
|
12
|
+
* Override types
|
|
13
|
+
*/
|
|
14
|
+
protected _head: DoubleLinkedNode<T> | null;
|
|
15
|
+
protected _tail: DoubleLinkedNode<T> | null;
|
|
16
|
+
/**
|
|
17
|
+
* @inheritDoc
|
|
18
|
+
*/
|
|
19
|
+
constructor(capacity?: number);
|
|
20
|
+
/**
|
|
21
|
+
* @inheritDoc
|
|
22
|
+
*/
|
|
23
|
+
protected createNode(value: T): DoubleLinkedNode<T>;
|
|
24
|
+
/**
|
|
25
|
+
* @inheritDoc
|
|
26
|
+
*/
|
|
27
|
+
protected insertNodeBetweenTwoNodesImpl(targetNode: DoubleLinkedNode<T>, leftNode: DoubleLinkedNode<T>, rightNode: DoubleLinkedNode<T>): void;
|
|
28
|
+
/**
|
|
29
|
+
* @inheritDoc
|
|
30
|
+
*/
|
|
31
|
+
protected deleteNodeImpl(node: DoubleLinkedNode<T>): void;
|
|
32
|
+
/**
|
|
33
|
+
* @inheritDoc
|
|
34
|
+
*/
|
|
35
|
+
protected popImpl(): void;
|
|
36
|
+
/**
|
|
37
|
+
* @inheritDoc
|
|
38
|
+
*/
|
|
39
|
+
protected shiftImpl(): void;
|
|
40
|
+
/**
|
|
41
|
+
* @inheritDoc
|
|
42
|
+
*/
|
|
43
|
+
reverse(): void;
|
|
44
|
+
/**
|
|
45
|
+
* List iterator
|
|
46
|
+
*/
|
|
47
|
+
iterator(fromIndex?: number): IBiDirectIterator<T>;
|
|
48
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
extendStatics(d, b);
|
|
11
|
+
function __() { this.constructor = d; }
|
|
12
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
var AbstractLinkedList_1 = require("../AbstractLinkedList/AbstractLinkedList");
|
|
17
|
+
var DoubleLinkedNode_1 = require("./DoubleLinkedNode");
|
|
18
|
+
var IsNotFoundException_1 = require("../../../exceptions/IsNotFoundException");
|
|
19
|
+
/**
|
|
20
|
+
* Linear data structure
|
|
21
|
+
* Each node has next and prev sibling
|
|
22
|
+
* Head and tail are linked to each other
|
|
23
|
+
*/
|
|
24
|
+
var DoubleLinkedList = /** @class */ (function (_super) {
|
|
25
|
+
__extends(DoubleLinkedList, _super);
|
|
26
|
+
/**
|
|
27
|
+
* @inheritDoc
|
|
28
|
+
*/
|
|
29
|
+
function DoubleLinkedList(capacity) {
|
|
30
|
+
var _this = _super.call(this, capacity) || this;
|
|
31
|
+
_this._head = null;
|
|
32
|
+
_this._tail = null;
|
|
33
|
+
return _this;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritDoc
|
|
37
|
+
*/
|
|
38
|
+
DoubleLinkedList.prototype.createNode = function (value) {
|
|
39
|
+
return new DoubleLinkedNode_1.default(value);
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* @inheritDoc
|
|
43
|
+
*/
|
|
44
|
+
DoubleLinkedList.prototype.insertNodeBetweenTwoNodesImpl = function (targetNode, leftNode, rightNode) {
|
|
45
|
+
targetNode.next = rightNode;
|
|
46
|
+
targetNode.prev = leftNode;
|
|
47
|
+
if (targetNode.prev) {
|
|
48
|
+
targetNode.prev.next = targetNode;
|
|
49
|
+
}
|
|
50
|
+
if (targetNode.next) {
|
|
51
|
+
targetNode.next.prev = targetNode;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* @inheritDoc
|
|
56
|
+
*/
|
|
57
|
+
DoubleLinkedList.prototype.deleteNodeImpl = function (node) {
|
|
58
|
+
node.prev.next = node.next;
|
|
59
|
+
node.next.prev = node.prev;
|
|
60
|
+
node.next = null;
|
|
61
|
+
node.prev = null;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* @inheritDoc
|
|
65
|
+
*/
|
|
66
|
+
DoubleLinkedList.prototype.popImpl = function () {
|
|
67
|
+
var _a;
|
|
68
|
+
this._head = ((_a = this._tail) === null || _a === void 0 ? void 0 : _a.prev) || null;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* @inheritDoc
|
|
72
|
+
*/
|
|
73
|
+
DoubleLinkedList.prototype.shiftImpl = function () {
|
|
74
|
+
var _a;
|
|
75
|
+
this._tail = ((_a = this._head) === null || _a === void 0 ? void 0 : _a.next) || null;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* @inheritDoc
|
|
79
|
+
*/
|
|
80
|
+
DoubleLinkedList.prototype.reverse = function () {
|
|
81
|
+
var currentNode = this._tail;
|
|
82
|
+
var i = 0;
|
|
83
|
+
while (currentNode && i < this._length) {
|
|
84
|
+
var newPrev = currentNode.next;
|
|
85
|
+
var newNext = currentNode.prev;
|
|
86
|
+
currentNode.prev = newPrev;
|
|
87
|
+
currentNode.next = newNext;
|
|
88
|
+
i++;
|
|
89
|
+
currentNode = newNext;
|
|
90
|
+
}
|
|
91
|
+
if (currentNode) {
|
|
92
|
+
this._tail = currentNode.next;
|
|
93
|
+
this._head = currentNode;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* List iterator
|
|
98
|
+
*/
|
|
99
|
+
DoubleLinkedList.prototype.iterator = function (fromIndex) {
|
|
100
|
+
if (fromIndex === void 0) { fromIndex = 0; }
|
|
101
|
+
var head = this._head;
|
|
102
|
+
var tail = this._tail;
|
|
103
|
+
var activeNode = this.getNodeByIndex(fromIndex);
|
|
104
|
+
var iterator = {
|
|
105
|
+
/**
|
|
106
|
+
* @inheritDoc
|
|
107
|
+
*/
|
|
108
|
+
current: function () {
|
|
109
|
+
return activeNode.data;
|
|
110
|
+
},
|
|
111
|
+
/**
|
|
112
|
+
* @inheritDoc
|
|
113
|
+
*/
|
|
114
|
+
hasNext: function () {
|
|
115
|
+
return Boolean(activeNode.next) && activeNode !== head;
|
|
116
|
+
},
|
|
117
|
+
/**
|
|
118
|
+
* @inheritDoc
|
|
119
|
+
*/
|
|
120
|
+
hasPrev: function () {
|
|
121
|
+
return Boolean(activeNode.prev) && activeNode !== tail;
|
|
122
|
+
},
|
|
123
|
+
/**
|
|
124
|
+
* @inheritDoc
|
|
125
|
+
* @throws when next element does not exist
|
|
126
|
+
*/
|
|
127
|
+
next: function () {
|
|
128
|
+
if (!iterator.hasNext()) {
|
|
129
|
+
throw new IsNotFoundException_1.default("Next element does not exist");
|
|
130
|
+
}
|
|
131
|
+
activeNode = activeNode.next;
|
|
132
|
+
return activeNode.data;
|
|
133
|
+
},
|
|
134
|
+
/**
|
|
135
|
+
* @inheritDoc
|
|
136
|
+
* @throws when prev element does not exists
|
|
137
|
+
*/
|
|
138
|
+
prev: function () {
|
|
139
|
+
if (!iterator.hasPrev()) {
|
|
140
|
+
throw new IsNotFoundException_1.default("Prev element does not exist");
|
|
141
|
+
}
|
|
142
|
+
activeNode = activeNode.prev;
|
|
143
|
+
return activeNode.data;
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
return iterator;
|
|
147
|
+
};
|
|
148
|
+
return DoubleLinkedList;
|
|
149
|
+
}(AbstractLinkedList_1.default));
|
|
150
|
+
exports.default = DoubleLinkedList;
|
|
151
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRG91YmxlTGlua2VkTGlzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9kYXRhLXN0cnVjdHVyZXMvTGlua2VkTGlzdC9Eb3VibGVMaW5rZWRMaXN0L0RvdWJsZUxpbmtlZExpc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7O0FBRUEsK0VBQTBFO0FBQzFFLHVEQUFrRDtBQUNsRCwrRUFBMEU7QUFFMUU7Ozs7R0FJRztBQUNIO0lBQ1Usb0NBQXFCO0lBUTdCOztPQUVHO0lBQ0gsMEJBQW1C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9BQU8sUUFBUSxDQUFDO0lBQ2xCLENBQUM7SUFDSCx1QkFBQztBQUFELENBQUMsQUFqSkQsQ0FDVSw0QkFBa0IsR0FnSjNCIn0=
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import AbstractLinkedNode from "../AbstractLinkedList/AbstractLinkedNode";
|
|
2
|
+
export default class DoubleLinkedNode<T> extends AbstractLinkedNode<T> {
|
|
3
|
+
protected _prev: DoubleLinkedNode<T> | null;
|
|
4
|
+
protected _next: DoubleLinkedNode<T> | null;
|
|
5
|
+
/**
|
|
6
|
+
* Will create empty node
|
|
7
|
+
*/
|
|
8
|
+
constructor(data: T, next?: DoubleLinkedNode<T> | null, prev?: DoubleLinkedNode<T> | null);
|
|
9
|
+
/**
|
|
10
|
+
* Set previous node link
|
|
11
|
+
*/
|
|
12
|
+
set prev(value: DoubleLinkedNode<T> | null);
|
|
13
|
+
/**
|
|
14
|
+
* Get previous node link
|
|
15
|
+
*/
|
|
16
|
+
get prev(): DoubleLinkedNode<T> | null;
|
|
17
|
+
/**
|
|
18
|
+
* @inheritDoc
|
|
19
|
+
*/
|
|
20
|
+
set next(value: DoubleLinkedNode<T> | null);
|
|
21
|
+
/**
|
|
22
|
+
* @inheritDoc
|
|
23
|
+
*/
|
|
24
|
+
get next(): DoubleLinkedNode<T> | null;
|
|
25
|
+
}
|