@raikuxq/alg-ds 1.1.2 → 1.1.5

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.
Files changed (257) hide show
  1. package/README.md +26 -2
  2. package/lib/algorithms/binary-search.d.ts +5 -0
  3. package/lib/algorithms/binary-search.js +27 -0
  4. package/lib/algorithms/factorial.d.ts +9 -0
  5. package/lib/algorithms/factorial.js +17 -0
  6. package/lib/algorithms/fibonacci.d.ts +9 -0
  7. package/lib/algorithms/fibonacci.js +17 -0
  8. package/lib/algorithms/memoize.d.ts +5 -0
  9. package/lib/algorithms/memoize.js +22 -0
  10. package/lib/algorithms/sorts/bubble-sort.d.ts +9 -0
  11. package/lib/algorithms/sorts/bubble-sort.js +23 -0
  12. package/lib/algorithms/sorts/insertion-sort.d.ts +9 -0
  13. package/lib/algorithms/sorts/insertion-sort.js +25 -0
  14. package/lib/algorithms/sorts/merge-sort.d.ts +9 -0
  15. package/lib/algorithms/sorts/merge-sort.js +61 -0
  16. package/lib/algorithms/sorts/quick-sort.d.ts +9 -0
  17. package/lib/algorithms/sorts/quick-sort.js +45 -0
  18. package/lib/algorithms/sorts/select-sort.d.ts +9 -0
  19. package/lib/algorithms/sorts/select-sort.js +20 -0
  20. package/lib/algorithms/transpose-matrix.d.ts +5 -0
  21. package/lib/algorithms/transpose-matrix.js +20 -0
  22. package/lib/constants.d.ts +2 -0
  23. package/lib/constants.js +6 -0
  24. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.d.ts +15 -0
  25. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.js +53 -0
  26. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.d.ts +60 -0
  27. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.js +36 -0
  28. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.d.ts +13 -0
  29. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.js +59 -0
  30. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.d.ts +70 -0
  31. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.js +268 -0
  32. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.d.ts +16 -0
  33. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.js +70 -0
  34. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.d.ts +57 -0
  35. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.js +234 -0
  36. package/lib/data-structures/Graph/AbstractGraph.d.ts +84 -0
  37. package/lib/data-structures/Graph/AbstractGraph.js +141 -0
  38. package/lib/data-structures/Graph/DirectedGraph.d.ts +24 -0
  39. package/lib/data-structures/Graph/DirectedGraph.js +85 -0
  40. package/lib/data-structures/Graph/GraphEdge.d.ts +16 -0
  41. package/lib/data-structures/Graph/GraphEdge.js +43 -0
  42. package/lib/data-structures/Graph/UndirectedGraph.d.ts +28 -0
  43. package/lib/data-structures/Graph/UndirectedGraph.js +102 -0
  44. package/lib/data-structures/Graph/demo/generateRandomGraph.d.ts +4 -0
  45. package/lib/data-structures/Graph/demo/generateRandomGraph.js +72 -0
  46. package/lib/data-structures/Graph/iterator/AbstractGraphIterator.d.ts +35 -0
  47. package/lib/data-structures/Graph/iterator/AbstractGraphIterator.js +90 -0
  48. package/lib/data-structures/Graph/iterator/GraphIteratorBFS.d.ts +28 -0
  49. package/lib/data-structures/Graph/iterator/GraphIteratorBFS.js +70 -0
  50. package/lib/data-structures/Graph/iterator/GraphIteratorDFS.d.ts +28 -0
  51. package/lib/data-structures/Graph/iterator/GraphIteratorDFS.js +70 -0
  52. package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.d.ts +32 -0
  53. package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.js +99 -0
  54. package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.d.ts +19 -0
  55. package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.js +28 -0
  56. package/{src/data-structures/Graph/presenter/presenterAdjacencyMatrix.ts → lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.d.ts} +32 -51
  57. package/lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.js +48 -0
  58. package/lib/data-structures/Graph/searching/hasPath.d.ts +9 -0
  59. package/lib/data-structures/Graph/searching/hasPath.js +29 -0
  60. package/lib/data-structures/Graph/searching/shortestPath.d.ts +9 -0
  61. package/lib/data-structures/Graph/searching/shortestPath.js +29 -0
  62. package/lib/data-structures/Graph/strategy/BFSIterationStrategy.d.ts +6 -0
  63. package/lib/data-structures/Graph/strategy/BFSIterationStrategy.js +13 -0
  64. package/lib/data-structures/Graph/strategy/DFSIterationStrategy.d.ts +6 -0
  65. package/lib/data-structures/Graph/strategy/DFSIterationStrategy.js +13 -0
  66. package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.d.ts +6 -0
  67. package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.js +13 -0
  68. package/lib/data-structures/Graph/transposing/transposeDirectedGraph.d.ts +2 -0
  69. package/lib/data-structures/Graph/transposing/transposeDirectedGraph.js +14 -0
  70. package/lib/data-structures/HashTable/HashTable.d.ts +73 -0
  71. package/lib/data-structures/HashTable/HashTable.js +169 -0
  72. package/lib/data-structures/HashTable/HashTableNode.d.ts +11 -0
  73. package/lib/data-structures/HashTable/HashTableNode.js +39 -0
  74. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.d.ts +125 -0
  75. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.js +236 -0
  76. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.d.ts +20 -0
  77. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.js +41 -0
  78. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.d.ts +48 -0
  79. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.js +150 -0
  80. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.d.ts +25 -0
  81. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.js +65 -0
  82. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.d.ts +52 -0
  83. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.js +137 -0
  84. package/{src/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.ts → lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.d.ts} +7 -10
  85. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.js +29 -0
  86. package/lib/data-structures/LoopedArray/LoopedArray.d.ts +86 -0
  87. package/lib/data-structures/LoopedArray/LoopedArray.js +161 -0
  88. package/lib/data-structures/Queue/Queue.d.ts +50 -0
  89. package/lib/data-structures/Queue/Queue.js +83 -0
  90. package/lib/data-structures/Stack/Stack.d.ts +50 -0
  91. package/lib/data-structures/Stack/Stack.js +83 -0
  92. package/lib/exports/algorithms.d.ts +16 -0
  93. package/lib/exports/algorithms.js +36 -0
  94. package/lib/exports/constants.d.ts +2 -0
  95. package/lib/exports/constants.js +7 -0
  96. package/lib/exports/data-structures.d.ts +11 -0
  97. package/lib/exports/data-structures.js +24 -0
  98. package/lib/exports/helpers.d.ts +6 -0
  99. package/lib/exports/helpers.js +14 -0
  100. package/lib/exports/sorts.d.ts +6 -0
  101. package/lib/exports/sorts.js +14 -0
  102. package/lib/exports/utils.d.ts +3 -0
  103. package/lib/exports/utils.js +14 -0
  104. package/lib/exports.d.ts +44 -0
  105. package/lib/exports.js +89 -0
  106. package/lib/helpers/createBinaryTree.d.ts +6 -0
  107. package/lib/helpers/createBinaryTree.js +22 -0
  108. package/lib/helpers/createGraph.d.ts +6 -0
  109. package/lib/helpers/createGraph.js +24 -0
  110. package/lib/helpers/createGraphFromMatrix.d.ts +7 -0
  111. package/lib/helpers/createGraphFromMatrix.js +37 -0
  112. package/lib/helpers/createLinkedList.d.ts +3 -0
  113. package/lib/helpers/createLinkedList.js +21 -0
  114. package/lib/index.d.ts +3 -0
  115. package/lib/index.js +6 -0
  116. package/lib/types/ArrayMatrix.d.ts +1 -0
  117. package/lib/types/ArrayMatrix.js +3 -0
  118. package/lib/types/EnumBinarySearchTreeType.d.ts +4 -0
  119. package/lib/types/EnumBinarySearchTreeType.js +9 -0
  120. package/lib/types/EnumGraphType.d.ts +4 -0
  121. package/lib/types/EnumGraphType.js +9 -0
  122. package/lib/types/EnumLinkedListType.d.ts +4 -0
  123. package/lib/types/EnumLinkedListType.js +9 -0
  124. package/lib/types/EnumRandomGenerationFormat.d.ts +4 -0
  125. package/lib/types/EnumRandomGenerationFormat.js +9 -0
  126. package/lib/types/EnumTreeTraversalType.d.ts +5 -0
  127. package/lib/types/EnumTreeTraversalType.js +10 -0
  128. package/lib/types/FnCompareTwo.d.ts +1 -0
  129. package/lib/types/FnCompareTwo.js +3 -0
  130. package/lib/types/FnToMemoize.d.ts +1 -0
  131. package/lib/types/FnToMemoize.js +3 -0
  132. package/{src/types/ILinkedList.ts → lib/types/IArrayFacade.d.ts} +4 -6
  133. package/lib/types/IArrayFacade.js +3 -0
  134. package/{src/types/IBiDirectIterable.ts → lib/types/IBiDirectIterable.d.ts} +5 -6
  135. package/lib/types/IBiDirectIterable.js +3 -0
  136. package/lib/types/IBiDirectIterator.d.ts +11 -0
  137. package/lib/types/IBiDirectIterator.js +3 -0
  138. package/lib/types/IBinaryTree.d.ts +12 -0
  139. package/lib/types/IBinaryTree.js +3 -0
  140. package/lib/types/IConvertableToArray.d.ts +4 -0
  141. package/lib/types/IConvertableToArray.js +3 -0
  142. package/lib/types/IGraph.d.ts +14 -0
  143. package/lib/types/IGraph.js +3 -0
  144. package/{src/types/IGraphIterationStrategy.ts → lib/types/IGraphIterationStrategy.d.ts} +5 -6
  145. package/lib/types/IGraphIterationStrategy.js +3 -0
  146. package/lib/types/IGraphIterator.d.ts +11 -0
  147. package/lib/types/IGraphIterator.js +3 -0
  148. package/{src/types/IIterable.ts → lib/types/IIterable.d.ts} +4 -5
  149. package/lib/types/IIterable.js +3 -0
  150. package/lib/types/IIterator.d.ts +14 -0
  151. package/lib/types/IIterator.js +3 -0
  152. package/lib/types/IKeyValueStorage.d.ts +8 -0
  153. package/lib/types/IKeyValueStorage.js +3 -0
  154. package/lib/types/ILinearStorage.d.ts +11 -0
  155. package/lib/types/ILinearStorage.js +3 -0
  156. package/{src/types/ILinearStorageRA.ts → lib/types/ILinearStorageRA.d.ts} +13 -14
  157. package/lib/types/ILinearStorageRA.js +3 -0
  158. package/{src/types/IArrayFacade.ts → lib/types/ILinkedList.d.ts} +4 -6
  159. package/lib/types/ILinkedList.js +3 -0
  160. package/lib/utils.d.ts +29 -0
  161. package/lib/utils.js +95 -0
  162. package/package.json +9 -3
  163. package/.idea/algorythmes.iml +0 -15
  164. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  165. package/.idea/deployment.xml +0 -14
  166. package/.idea/inspectionProfiles/Project_Default.xml +0 -7
  167. package/.idea/jsLinters/eslint.xml +0 -6
  168. package/.idea/misc.xml +0 -6
  169. package/.idea/modules.xml +0 -8
  170. package/.idea/vcs.xml +0 -6
  171. package/lib/algotirhms.ts +0 -35
  172. package/lib/constants.ts +0 -3
  173. package/lib/data-structures.ts +0 -23
  174. package/lib/helpers.ts +0 -13
  175. package/lib/sorts.ts +0 -7
  176. package/lib/types.ts +0 -53
  177. package/lib/utils.ts +0 -21
  178. package/src/algorithms/binary-search.ts +0 -28
  179. package/src/algorithms/factorial.ts +0 -18
  180. package/src/algorithms/fibonacci.ts +0 -18
  181. package/src/algorithms/memoize.ts +0 -21
  182. package/src/algorithms/sorts/bubble-sort.ts +0 -21
  183. package/src/algorithms/sorts/insertion-sort.ts +0 -25
  184. package/src/algorithms/sorts/merge-sort.ts +0 -74
  185. package/src/algorithms/sorts/quick-sort.ts +0 -54
  186. package/src/algorithms/sorts/select-sort.ts +0 -19
  187. package/src/algorithms/transpose-matrix.ts +0 -19
  188. package/src/constants.ts +0 -2
  189. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.ts +0 -45
  190. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts +0 -80
  191. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts +0 -38
  192. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts +0 -286
  193. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.ts +0 -48
  194. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.ts +0 -228
  195. package/src/data-structures/Graph/AbstractGraph.ts +0 -189
  196. package/src/data-structures/Graph/DirectedGraph.ts +0 -84
  197. package/src/data-structures/Graph/GraphEdge.ts +0 -33
  198. package/src/data-structures/Graph/UndirectedGraph.ts +0 -108
  199. package/src/data-structures/Graph/demo/generateRandomGraph.ts +0 -93
  200. package/src/data-structures/Graph/iterator/AbstractGraphIterator.ts +0 -99
  201. package/src/data-structures/Graph/iterator/GraphIteratorBFS.ts +0 -60
  202. package/src/data-structures/Graph/iterator/GraphIteratorDFS.ts +0 -60
  203. package/src/data-structures/Graph/iterator/GraphIteratorDijkstra.ts +0 -94
  204. package/src/data-structures/Graph/presenter/presenterAdjacencyLists.ts +0 -29
  205. package/src/data-structures/Graph/searching/hasPath.ts +0 -38
  206. package/src/data-structures/Graph/searching/shortestPath.ts +0 -38
  207. package/src/data-structures/Graph/strategy/BFSIterationStrategy.ts +0 -11
  208. package/src/data-structures/Graph/strategy/DFSIterationStrategy.ts +0 -11
  209. package/src/data-structures/Graph/strategy/DijkstraIterationStrategy.ts +0 -11
  210. package/src/data-structures/Graph/transposing/transposeDirectedGraph.ts +0 -19
  211. package/src/data-structures/HashTable/HashTable.ts +0 -202
  212. package/src/data-structures/HashTable/HashTableNode.ts +0 -31
  213. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.ts +0 -310
  214. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.ts +0 -33
  215. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.ts +0 -156
  216. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.ts +0 -47
  217. package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.ts +0 -147
  218. package/src/data-structures/LoopedArray/LoopedArray.ts +0 -182
  219. package/src/data-structures/Queue/Queue.ts +0 -92
  220. package/src/data-structures/Stack/Stack.ts +0 -92
  221. package/src/demo/demo.bst.ts +0 -67
  222. package/src/demo/demo.graph.ts +0 -246
  223. package/src/demo/demo.hashtable.ts +0 -28
  224. package/src/demo/demo.linked-list.ts +0 -78
  225. package/src/demo/demo.looped-array.ts +0 -104
  226. package/src/demo/demo.queue.ts +0 -40
  227. package/src/demo/demo.stack.ts +0 -40
  228. package/src/demo/performance/bst-compare.ts +0 -35
  229. package/src/demo/performance/ds-compare.ts +0 -58
  230. package/src/demo/performance/hash-table.compare.ts +0 -40
  231. package/src/demo/performance/sort-compare.ts +0 -60
  232. package/src/helpers/createBinaryTree.ts +0 -24
  233. package/src/helpers/createGraph.ts +0 -24
  234. package/src/helpers/createGraphFromMatrix.ts +0 -47
  235. package/src/helpers/createLinkedList.ts +0 -24
  236. package/src/index.ts +0 -44
  237. package/src/types/ArrayMatrix.ts +0 -1
  238. package/src/types/EnumBinarySearchTreeType.ts +0 -4
  239. package/src/types/EnumGraphTraversalType.ts +0 -5
  240. package/src/types/EnumGraphType.ts +0 -4
  241. package/src/types/EnumLinkedListType.ts +0 -4
  242. package/src/types/EnumRandomGenerationFormat.ts +0 -4
  243. package/src/types/EnumSortType.ts +0 -7
  244. package/src/types/EnumTreeTraversalType.ts +0 -5
  245. package/src/types/FnCompareTwo.ts +0 -1
  246. package/src/types/FnSort.ts +0 -1
  247. package/src/types/FnToMemoize.ts +0 -1
  248. package/src/types/IBiDirectIterator.ts +0 -12
  249. package/src/types/IBinaryTree.ts +0 -13
  250. package/src/types/IConvertableToArray.ts +0 -4
  251. package/src/types/IGraph.ts +0 -16
  252. package/src/types/IGraphCreator.ts +0 -5
  253. package/src/types/IGraphIterator.ts +0 -13
  254. package/src/types/IIterator.ts +0 -14
  255. package/src/types/IKeyValueStorage.ts +0 -8
  256. package/src/types/ILinearStorage.ts +0 -11
  257. package/src/utils.ts +0 -65
@@ -1,310 +0,0 @@
1
- import AbstractLinkedNode from "./AbstractLinkedNode";
2
- import ILinkedList from "../../../types/ILinkedList";
3
-
4
- export default abstract class AbstractLinkedList<T> implements ILinkedList<T> {
5
- protected readonly _capacity: number;
6
- protected _length: number;
7
- protected _head: AbstractLinkedNode<T> | null;
8
- protected _tail: AbstractLinkedNode<T> | null;
9
-
10
- /**
11
- * Create empty instance
12
- */
13
- protected constructor(capacity?: number) {
14
- this._capacity = AbstractLinkedList.calculateCapacity(capacity);
15
- this._head = null;
16
- this._tail = null;
17
- this._length = 0;
18
- }
19
-
20
- /**
21
- * Will calculate real capacity value
22
- * @throws when capacity <= 0
23
- */
24
- private static calculateCapacity(capacity?: number) {
25
- if (capacity === undefined) {
26
- return Number.MAX_VALUE;
27
- }
28
- if (capacity <= 0) {
29
- throw new Error("Capacity must be larger than 0");
30
- }
31
-
32
- return capacity;
33
- }
34
-
35
- /**
36
- * Will insert node between nodeLeft and nodeRight
37
- * @throws when list is full
38
- */
39
- private insertNodeBetweenTwoNodes(
40
- targetNode: AbstractLinkedNode<T>,
41
- leftNode: AbstractLinkedNode<T> | null,
42
- rightNode: AbstractLinkedNode<T> | null
43
- ): void {
44
- if (this.isFull()) {
45
- throw new Error("List is full, no more space available");
46
- }
47
- if (this._head === null) {
48
- this._head = targetNode;
49
- }
50
- if (this._tail === null) {
51
- this._tail = targetNode;
52
- }
53
- if (!leftNode) {
54
- leftNode = this._tail;
55
- }
56
- if (!rightNode) {
57
- rightNode = this._head;
58
- }
59
-
60
- this.insertNodeBetweenTwoNodesImpl(targetNode, leftNode, rightNode);
61
- this._length++;
62
- }
63
-
64
- /**
65
- * Will remove the node from its neighbors nodes links
66
- * @throws when node does not exist
67
- */
68
- private deleteNode(
69
- node: AbstractLinkedNode<T> | null
70
- ): AbstractLinkedNode<T> {
71
- if (node === null) {
72
- throw new Error("Node should be existed");
73
- }
74
- this.deleteNodeImpl(node);
75
- this._length--;
76
-
77
- if (this.isEmpty()) {
78
- this.clear();
79
- }
80
- return node;
81
- }
82
-
83
- /**
84
- * Will find node by its index
85
- * @throws when node was not found
86
- */
87
- protected getNodeByIndex(index: number): AbstractLinkedNode<T> {
88
- if (this.isEmpty()) {
89
- throw new Error("List is empty");
90
- }
91
- if (this._length < index) {
92
- throw new Error("Index exceed list length");
93
- }
94
-
95
- let currentNode = this._tail;
96
- let counter = 0;
97
-
98
- while (currentNode && counter < index) {
99
- currentNode = currentNode.next;
100
- counter++;
101
- }
102
-
103
- if (currentNode === null) {
104
- throw new Error("Node does not exist");
105
- }
106
-
107
- return currentNode;
108
- }
109
-
110
- /**
111
- * Will set links between target, left and right siblings
112
- */
113
- protected abstract insertNodeBetweenTwoNodesImpl(
114
- nodeToPush: AbstractLinkedNode<T>,
115
- nodeLeft: AbstractLinkedNode<T>,
116
- nodeRight: AbstractLinkedNode<T>
117
- ): void;
118
-
119
- /**
120
- * Will unset itself links and its neighbors links
121
- */
122
- protected abstract deleteNodeImpl(node: AbstractLinkedNode<T>): void;
123
-
124
- /**
125
- * Update head link
126
- */
127
- protected abstract popImpl(): void;
128
-
129
- /**
130
- * Update tail link
131
- */
132
- protected abstract shiftImpl(): void;
133
-
134
- /**
135
- * Will create empty node instance
136
- */
137
- protected abstract createNode(value: T): AbstractLinkedNode<T>;
138
-
139
- /**
140
- * Push into start
141
- */
142
- public unshift(value: T): void {
143
- const node = this.createNode(value);
144
- this.insertNodeBetweenTwoNodes(node, this._head, this._tail);
145
- this._tail = node;
146
- }
147
-
148
- /**
149
- * Push into end
150
- */
151
- public push(value: T): void {
152
- const node = this.createNode(value);
153
- this.insertNodeBetweenTwoNodes(node, this._head, this._tail);
154
- this._head = node;
155
- }
156
-
157
- /**
158
- * Push from index
159
- */
160
- public pushFromIndex(value: T, fromIndex: number): void {
161
- const isIndexNotInRange = fromIndex < 0 || fromIndex > this._length;
162
- const shouldPushAsFirst = this.isEmpty() && fromIndex === 0;
163
-
164
- if (isIndexNotInRange) {
165
- throw new Error("index must be in range between 0 and list length");
166
- }
167
- if (shouldPushAsFirst) {
168
- this.push(value);
169
- } else {
170
- const node = this.createNode(value);
171
- const nodeLeft = this.getNodeByIndex(fromIndex - 1);
172
- const nodeRight = this.getNodeByIndex(fromIndex);
173
- this.insertNodeBetweenTwoNodes(node, nodeLeft, nodeRight);
174
- }
175
- }
176
-
177
- /**
178
- * Delete node from list's end
179
- */
180
- public pop(): T {
181
- const deletedNode = this.deleteNode(this._head);
182
- this.popImpl();
183
- return deletedNode.data;
184
- }
185
-
186
- /**
187
- * Delete node from list's start and get its data
188
- */
189
- public shift(): T {
190
- const deletedNode = this.deleteNode(this._tail);
191
- this.shiftImpl();
192
- return deletedNode.data;
193
- }
194
-
195
- /**
196
- * Delete node from list by index from start
197
- */
198
- public deleteFromIndex(fromIndex: number): T {
199
- const nodeToDelete = this.getNodeByIndex(fromIndex);
200
- const deletedNode = this.deleteNode(nodeToDelete);
201
- return deletedNode.data;
202
- }
203
-
204
- /**
205
- * List length
206
- */
207
- public length(): number {
208
- return this._length;
209
- }
210
-
211
- /**
212
- * Is list empty
213
- */
214
- public isEmpty(): boolean {
215
- return this._length === 0;
216
- }
217
-
218
- /**
219
- * Is list full
220
- */
221
- public isFull(): boolean {
222
- return this._length >= this._capacity;
223
- }
224
-
225
- /**
226
- * Check if element exists in list
227
- */
228
- public has(item: T): boolean {
229
- return this.getAsArray().includes(item);
230
- }
231
-
232
- /**
233
- * Get head element data
234
- * @throws Error when head does not exist
235
- */
236
- public peek(): T {
237
- if (!this._head) {
238
- throw new Error("Head does not exist");
239
- }
240
-
241
- return this._head.data;
242
- }
243
-
244
- /**
245
- * Get tail element data
246
- * @throws Error when tail does not exists
247
- */
248
- public peekFromStart(): T {
249
- if (!this._tail) {
250
- throw new Error("Tail does not exist");
251
- }
252
-
253
- return this._tail.data;
254
- }
255
-
256
- /**
257
- * Get list element by index from start
258
- * @throws when element does not exist
259
- */
260
- public peekByIndex(index: number): T {
261
- const node = this.getNodeByIndex(index);
262
- return node.data;
263
- }
264
-
265
- /**
266
- * Remove all elements from list
267
- */
268
- public clear(): void {
269
- this._head = null;
270
- this._tail = null;
271
- this._length = 0;
272
- }
273
-
274
- /**
275
- * Get elements as an array
276
- */
277
- public getAsArray(): Array<T> {
278
- const array: Array<T> = [];
279
- let currentNode = this._tail;
280
- let counter = 0;
281
-
282
- while (currentNode && counter < this._length) {
283
- if (currentNode) array.push(currentNode.data);
284
-
285
- currentNode = currentNode.next;
286
- counter++;
287
- }
288
-
289
- return array;
290
- }
291
-
292
- /**
293
- * Add elements to list from array
294
- * @throws when list is full
295
- * */
296
- public pushFromArray(elements: Array<T>): void {
297
- elements.forEach((element: T) => {
298
- if (this.isFull()) {
299
- throw new Error("List is full, no more space available");
300
- }
301
- this.push(element);
302
- });
303
- }
304
-
305
- /**
306
- * Reverse list nodes links and swap head with tail
307
- * @example "4>7>10" will be reversed to "10>7>4"
308
- */
309
- public abstract reverse(): void;
310
- }
@@ -1,33 +0,0 @@
1
- export default abstract class AbstractLinkedNode<T> {
2
- protected _next: AbstractLinkedNode<T> | null;
3
- protected readonly _data: T;
4
-
5
- /**
6
- * Will create empty node
7
- */
8
- protected constructor(data: T, next: AbstractLinkedNode<T> | null = null) {
9
- this._data = data;
10
- this._next = next;
11
- }
12
-
13
- /**
14
- * Get data of node
15
- */
16
- public get data(): T {
17
- return this._data;
18
- }
19
-
20
- /**
21
- * Get next node link
22
- */
23
- public get next(): AbstractLinkedNode<T> | null {
24
- return this._next;
25
- }
26
-
27
- /**
28
- * Set next node link
29
- */
30
- public set next(value: AbstractLinkedNode<T> | null) {
31
- this._next = value;
32
- }
33
- }
@@ -1,156 +0,0 @@
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
- /**
7
- * Linear data structure
8
- * Each node has next and prev sibling
9
- * Head and tail are linked to each other
10
- */
11
- export default class DoubleLinkedList<T>
12
- extends AbstractLinkedList<T>
13
- implements IBiDirectIterable<T> {
14
- /**
15
- * Override types
16
- */
17
- protected _head: DoubleLinkedNode<T> | null;
18
- protected _tail: DoubleLinkedNode<T> | null;
19
-
20
- /**
21
- * @inheritDoc
22
- */
23
- public constructor(capacity?: number) {
24
- super(capacity);
25
- this._head = null;
26
- this._tail = null;
27
- }
28
-
29
- /**
30
- * @inheritDoc
31
- */
32
- protected createNode(value: T): DoubleLinkedNode<T> {
33
- return new DoubleLinkedNode<T>(value);
34
- }
35
-
36
- /**
37
- * @inheritDoc
38
- */
39
- protected insertNodeBetweenTwoNodesImpl(
40
- targetNode: DoubleLinkedNode<T>,
41
- leftNode: DoubleLinkedNode<T>,
42
- rightNode: DoubleLinkedNode<T>
43
- ): void {
44
- targetNode.next = rightNode;
45
- targetNode.prev = leftNode;
46
-
47
- if (targetNode.prev) {
48
- targetNode.prev.next = targetNode;
49
- }
50
- if (targetNode.next) {
51
- targetNode.next.prev = targetNode;
52
- }
53
- }
54
-
55
- /**
56
- * @inheritDoc
57
- */
58
- protected deleteNodeImpl(node: DoubleLinkedNode<T>): void {
59
- node!.prev!.next = node!.next;
60
- node!.next!.prev = node!.prev;
61
- node!.next = null;
62
- node!.prev = null;
63
- }
64
-
65
- /**
66
- * @inheritDoc
67
- */
68
- protected popImpl(): void {
69
- this._head = this._tail?.prev || null;
70
- }
71
-
72
- /**
73
- * @inheritDoc
74
- */
75
- protected shiftImpl(): void {
76
- this._tail = this._head?.next || null;
77
- }
78
-
79
- /**
80
- * @inheritDoc
81
- */
82
- public reverse(): void {
83
- let currentNode = this._tail;
84
- let i = 0;
85
-
86
- while (currentNode && i < this._length) {
87
- const newPrev = currentNode.next;
88
- const newNext = currentNode.prev;
89
-
90
- currentNode.prev = newPrev;
91
- currentNode.next = newNext;
92
-
93
- i++;
94
- currentNode = newNext;
95
- }
96
-
97
- if (currentNode) {
98
- this._tail = currentNode.next;
99
- this._head = currentNode;
100
- }
101
- }
102
-
103
- /**
104
- * List iterator
105
- */
106
- public iterator(fromIndex = 0): IBiDirectIterator<T> {
107
- const head = this._head;
108
- const tail = this._tail;
109
- let activeNode = this.getNodeByIndex(fromIndex) as DoubleLinkedNode<T>;
110
-
111
- const iterator: IBiDirectIterator<T> = {
112
- /**
113
- * @inheritDoc
114
- */
115
- current: () => {
116
- return activeNode.data;
117
- },
118
- /**
119
- * @inheritDoc
120
- */
121
- hasNext(): boolean {
122
- return Boolean(activeNode.next) && activeNode !== head;
123
- },
124
- /**
125
- * @inheritDoc
126
- */
127
- hasPrev(): boolean {
128
- return Boolean(activeNode.prev) && activeNode !== tail;
129
- },
130
- /**
131
- * @inheritDoc
132
- * @throws when next element does not exist
133
- */
134
- next: (): T => {
135
- if (!iterator.hasNext()) {
136
- throw new Error("Next element does not exist");
137
- }
138
- activeNode = activeNode.next!;
139
- return activeNode.data;
140
- },
141
- /**
142
- * @inheritDoc
143
- * @throws when prev element does not exists
144
- */
145
- prev: (): T => {
146
- if (!iterator.hasPrev()) {
147
- throw new Error("Prev element does not exist");
148
- }
149
- activeNode = activeNode.prev!;
150
- return activeNode.data;
151
- },
152
- };
153
-
154
- return iterator;
155
- }
156
- }
@@ -1,47 +0,0 @@
1
- import AbstractLinkedNode from "../AbstractLinkedList/AbstractLinkedNode";
2
-
3
- export default class DoubleLinkedNode<T> extends AbstractLinkedNode<T> {
4
- protected _prev: DoubleLinkedNode<T> | null;
5
- protected _next: DoubleLinkedNode<T> | null;
6
-
7
- /**
8
- * Will create empty node
9
- */
10
- public constructor(
11
- data: T,
12
- next: DoubleLinkedNode<T> | null = null,
13
- prev: DoubleLinkedNode<T> | null = null
14
- ) {
15
- super(data);
16
- this._prev = prev;
17
- this._next = next;
18
- }
19
-
20
- /**
21
- * Set previous node link
22
- */
23
- public set prev(value: DoubleLinkedNode<T> | null) {
24
- this._prev = value;
25
- }
26
-
27
- /**
28
- * Get previous node link
29
- */
30
- public get prev(): DoubleLinkedNode<T> | null {
31
- return this._prev;
32
- }
33
-
34
- /**
35
- * @inheritDoc
36
- */
37
- public set next(value: DoubleLinkedNode<T> | null) {
38
- this._next = value;
39
- }
40
-
41
- /**
42
- * @inheritDoc
43
- */
44
- public get next(): DoubleLinkedNode<T> | null {
45
- return this._next;
46
- }
47
- }
@@ -1,147 +0,0 @@
1
- import IIterator from "../../../types/IIterator";
2
- import IIterable from "../../../types/IIterable";
3
- import AbstractLinkedList from "../AbstractLinkedList/AbstractLinkedList";
4
- import SingleLinkedNode from "./SingleLinkedNode";
5
-
6
- /**
7
- * Linear data structure
8
- * Each node has next
9
- * Head's next node is tail
10
- */
11
- export default class SingleLinkedList<T>
12
- extends AbstractLinkedList<T>
13
- implements IIterable<T> {
14
- /**
15
- * Override types
16
- */
17
- protected _head: SingleLinkedNode<T> | null;
18
- protected _tail: SingleLinkedNode<T> | null;
19
-
20
- /**
21
- * @inheritDoc
22
- */
23
- public constructor(capacity?: number) {
24
- super(capacity);
25
- this._head = null;
26
- this._tail = null;
27
- }
28
-
29
- /**
30
- * Find previous sibling of given node
31
- */
32
- private getPrevNode(
33
- node: SingleLinkedNode<T> | null
34
- ): SingleLinkedNode<T> | null {
35
- let currentNode = this._tail;
36
- while (currentNode?.next !== node) {
37
- currentNode = currentNode?.next || null;
38
- }
39
- return currentNode;
40
- }
41
-
42
- /**
43
- * @inheritDoc
44
- */
45
- protected createNode(value: T): SingleLinkedNode<T> {
46
- return new SingleLinkedNode<T>(value);
47
- }
48
-
49
- /**
50
- * @inheritDoc
51
- */
52
- protected insertNodeBetweenTwoNodesImpl(
53
- targetNode: SingleLinkedNode<T>,
54
- leftNode: SingleLinkedNode<T>,
55
- rightNode: SingleLinkedNode<T>
56
- ): void {
57
- targetNode.next = rightNode;
58
-
59
- if (leftNode) {
60
- leftNode.next = targetNode;
61
- }
62
- }
63
-
64
- /**
65
- * @inheritDoc
66
- */
67
- protected deleteNodeImpl(node: SingleLinkedNode<T>): void {
68
- this.getPrevNode(node)!.next = node.next;
69
- node.next = null;
70
- }
71
-
72
- /**
73
- * @inheritDoc
74
- */
75
- protected popImpl(): void {
76
- this._head = this.getPrevNode(this._tail);
77
- }
78
-
79
- /**
80
- * @inheritDoc
81
- */
82
- protected shiftImpl(): void {
83
- this._tail = this._head?.next || null;
84
- }
85
-
86
- /**
87
- * @inheritDoc
88
- */
89
- public reverse(): void {
90
- let currentNode: SingleLinkedNode<T> | null = this._tail;
91
- let prevNode: SingleLinkedNode<T> | null = this._head;
92
- let index = 0;
93
-
94
- while (index < this._length) {
95
- const next = currentNode?.next || null;
96
-
97
- if (currentNode) {
98
- currentNode.next = prevNode;
99
- }
100
-
101
- index++;
102
- prevNode = currentNode;
103
- currentNode = next;
104
- }
105
-
106
- if (currentNode) {
107
- this._head = currentNode;
108
- this._tail = currentNode.next;
109
- }
110
- }
111
-
112
- /**
113
- * List iterator
114
- */
115
- public iterator(fromIndex = 0): IIterator<T> {
116
- const head = this._head;
117
- let activeNode: SingleLinkedNode<T> = this.getNodeByIndex(fromIndex);
118
-
119
- const iterator: IIterator<T> = {
120
- /**
121
- * @inheritDoc
122
- */
123
- current: () => {
124
- return activeNode.data;
125
- },
126
- /**
127
- * @inheritDoc
128
- */
129
- hasNext(): boolean {
130
- return Boolean(activeNode.next) && activeNode !== head;
131
- },
132
- /**
133
- * @inheritDoc
134
- * @throws when next element does not exist
135
- */
136
- next: (): T => {
137
- if (!iterator.hasNext()) {
138
- throw new Error("Next element does not exist");
139
- }
140
- activeNode = activeNode.next!;
141
- return activeNode.data;
142
- },
143
- };
144
-
145
- return iterator;
146
- }
147
- }