@gabrielrufino/cube 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/.github/workflows/cd.yml +26 -0
  2. package/.github/workflows/ci.yml +23 -0
  3. package/LICENSE +24 -0
  4. package/README.md +1695 -0
  5. package/build/Algorithms/index.d.ts +3 -0
  6. package/build/Algorithms/index.js +24 -0
  7. package/build/Array/IArray.d.ts +9 -0
  8. package/build/Array/IArray.js +2 -0
  9. package/build/Array/index.d.ts +12 -0
  10. package/build/Array/index.js +79 -0
  11. package/build/BinarySearchTree/BinarySearchTreeNode.d.ts +7 -0
  12. package/build/BinarySearchTree/BinarySearchTreeNode.js +22 -0
  13. package/build/BinarySearchTree/IBinarySearchNodeOptions.d.ts +5 -0
  14. package/build/BinarySearchTree/IBinarySearchNodeOptions.js +2 -0
  15. package/build/BinarySearchTree/IBinarySearchTree.d.ts +14 -0
  16. package/build/BinarySearchTree/IBinarySearchTree.js +2 -0
  17. package/build/BinarySearchTree/IBinarySearchTreeData.d.ts +7 -0
  18. package/build/BinarySearchTree/IBinarySearchTreeData.js +2 -0
  19. package/build/BinarySearchTree/index.d.ts +24 -0
  20. package/build/BinarySearchTree/index.js +199 -0
  21. package/build/DataStructure/index.d.ts +6 -0
  22. package/build/DataStructure/index.js +32 -0
  23. package/build/Deck/IDeck.d.ts +9 -0
  24. package/build/Deck/IDeck.js +2 -0
  25. package/build/Deck/index.d.ts +12 -0
  26. package/build/Deck/index.js +76 -0
  27. package/build/Dictionary/IDictionary.d.ts +16 -0
  28. package/build/Dictionary/IDictionary.js +2 -0
  29. package/build/Dictionary/IDictionaryData.d.ts +4 -0
  30. package/build/Dictionary/IDictionaryData.js +2 -0
  31. package/build/Dictionary/index.d.ts +21 -0
  32. package/build/Dictionary/index.js +110 -0
  33. package/build/DoublyLinkedList/IDoublyLinkedList.d.ts +16 -0
  34. package/build/DoublyLinkedList/IDoublyLinkedList.js +2 -0
  35. package/build/DoublyLinkedList/Node.d.ts +6 -0
  36. package/build/DoublyLinkedList/Node.js +11 -0
  37. package/build/DoublyLinkedList/index.d.ts +32 -0
  38. package/build/DoublyLinkedList/index.js +221 -0
  39. package/build/Graph/GraphNodeNotFoundError.d.ts +3 -0
  40. package/build/Graph/GraphNodeNotFoundError.js +27 -0
  41. package/build/Graph/GraphSearchNodeStates.d.ts +6 -0
  42. package/build/Graph/GraphSearchNodeStates.js +10 -0
  43. package/build/Graph/IGraph.d.ts +16 -0
  44. package/build/Graph/IGraph.js +2 -0
  45. package/build/Graph/IGraphOptions.d.ts +7 -0
  46. package/build/Graph/IGraphOptions.js +2 -0
  47. package/build/Graph/index.d.ts +20 -0
  48. package/build/Graph/index.js +187 -0
  49. package/build/HashTable/IHashTable.d.ts +9 -0
  50. package/build/HashTable/IHashTable.js +2 -0
  51. package/build/HashTable/IHashTableData.d.ts +4 -0
  52. package/build/HashTable/IHashTableData.js +2 -0
  53. package/build/HashTable/IHashTableInputs.d.ts +4 -0
  54. package/build/HashTable/IHashTableInputs.js +2 -0
  55. package/build/HashTable/IHashTableOptions.d.ts +4 -0
  56. package/build/HashTable/IHashTableOptions.js +2 -0
  57. package/build/HashTable/index.d.ts +16 -0
  58. package/build/HashTable/index.js +83 -0
  59. package/build/HashTableLinearProbing/HashTableLinearProbingElement.d.ts +6 -0
  60. package/build/HashTableLinearProbing/HashTableLinearProbingElement.js +19 -0
  61. package/build/HashTableLinearProbing/IHashTableLinearProbing.d.ts +9 -0
  62. package/build/HashTableLinearProbing/IHashTableLinearProbing.js +2 -0
  63. package/build/HashTableLinearProbing/IHashTableLinearProbingData.d.ts +5 -0
  64. package/build/HashTableLinearProbing/IHashTableLinearProbingData.js +2 -0
  65. package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.d.ts +4 -0
  66. package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.js +2 -0
  67. package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.d.ts +4 -0
  68. package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.js +2 -0
  69. package/build/HashTableLinearProbing/index.d.ts +20 -0
  70. package/build/HashTableLinearProbing/index.js +120 -0
  71. package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.d.ts +6 -0
  72. package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.js +21 -0
  73. package/build/HashTableSeparateChaining/IHashTableSeparateChaining.d.ts +10 -0
  74. package/build/HashTableSeparateChaining/IHashTableSeparateChaining.js +2 -0
  75. package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.d.ts +5 -0
  76. package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.js +2 -0
  77. package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.d.ts +4 -0
  78. package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.js +2 -0
  79. package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.d.ts +4 -0
  80. package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.js +2 -0
  81. package/build/HashTableSeparateChaining/index.d.ts +19 -0
  82. package/build/HashTableSeparateChaining/index.js +104 -0
  83. package/build/LinkedList/ILinkedList.d.ts +13 -0
  84. package/build/LinkedList/ILinkedList.js +2 -0
  85. package/build/LinkedList/ILinkedListItem.d.ts +5 -0
  86. package/build/LinkedList/ILinkedListItem.js +2 -0
  87. package/build/LinkedList/Node.d.ts +5 -0
  88. package/build/LinkedList/Node.js +10 -0
  89. package/build/LinkedList/index.d.ts +24 -0
  90. package/build/LinkedList/index.js +169 -0
  91. package/build/List/index.d.ts +1 -0
  92. package/build/List/index.js +8 -0
  93. package/build/MaxHeap/IMaxHeap.d.ts +9 -0
  94. package/build/MaxHeap/IMaxHeap.js +2 -0
  95. package/build/MaxHeap/IMaxHeapOptions.d.ts +5 -0
  96. package/build/MaxHeap/IMaxHeapOptions.js +2 -0
  97. package/build/MaxHeap/index.d.ts +19 -0
  98. package/build/MaxHeap/index.js +102 -0
  99. package/build/MinHeap/IMinHeap.d.ts +9 -0
  100. package/build/MinHeap/IMinHeap.js +2 -0
  101. package/build/MinHeap/IMinHeapOptions.d.ts +5 -0
  102. package/build/MinHeap/IMinHeapOptions.js +2 -0
  103. package/build/MinHeap/index.d.ts +19 -0
  104. package/build/MinHeap/index.js +102 -0
  105. package/build/Queue/IQueue.d.ts +8 -0
  106. package/build/Queue/IQueue.js +2 -0
  107. package/build/Queue/index.d.ts +11 -0
  108. package/build/Queue/index.js +74 -0
  109. package/build/Set/ISet.d.ts +15 -0
  110. package/build/Set/ISet.js +2 -0
  111. package/build/Set/index.d.ts +21 -0
  112. package/build/Set/index.js +132 -0
  113. package/build/Stack/IStack.d.ts +8 -0
  114. package/build/Stack/IStack.js +2 -0
  115. package/build/Stack/index.d.ts +11 -0
  116. package/build/Stack/index.js +64 -0
  117. package/build/index.d.ts +17 -0
  118. package/build/index.js +40 -0
  119. package/coverage/clover.xml +706 -0
  120. package/coverage/coverage-final.json +19 -0
  121. package/coverage/lcov-report/base.css +224 -0
  122. package/coverage/lcov-report/block-navigation.js +87 -0
  123. package/coverage/lcov-report/favicon.png +0 -0
  124. package/coverage/lcov-report/index.html +327 -0
  125. package/coverage/lcov-report/prettify.css +1 -0
  126. package/coverage/lcov-report/prettify.js +2 -0
  127. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  128. package/coverage/lcov-report/sorter.js +196 -0
  129. package/coverage/lcov-report/src/Algorithms/index.html +117 -0
  130. package/coverage/lcov-report/src/Algorithms/index.ts.html +149 -0
  131. package/coverage/lcov-report/src/Array/index.html +117 -0
  132. package/coverage/lcov-report/src/Array/index.ts.html +290 -0
  133. package/coverage/lcov-report/src/DataStructure/index.html +117 -0
  134. package/coverage/lcov-report/src/DataStructure/index.ts.html +131 -0
  135. package/coverage/lcov-report/src/Deck/index.html +117 -0
  136. package/coverage/lcov-report/src/Deck/index.ts.html +239 -0
  137. package/coverage/lcov-report/src/Dictionary/index.html +117 -0
  138. package/coverage/lcov-report/src/Dictionary/index.ts.html +350 -0
  139. package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +113 -0
  140. package/coverage/lcov-report/src/DoublyLinkedList/index.html +132 -0
  141. package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +836 -0
  142. package/coverage/lcov-report/src/HashTable/index.html +117 -0
  143. package/coverage/lcov-report/src/HashTable/index.ts.html +320 -0
  144. package/coverage/lcov-report/src/HashTableLinearProbing/index.html +117 -0
  145. package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +95 -0
  146. package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +149 -0
  147. package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +132 -0
  148. package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +398 -0
  149. package/coverage/lcov-report/src/LinkedList/Node.ts.html +110 -0
  150. package/coverage/lcov-report/src/LinkedList/index.html +132 -0
  151. package/coverage/lcov-report/src/LinkedList/index.ts.html +641 -0
  152. package/coverage/lcov-report/src/List/index.html +117 -0
  153. package/coverage/lcov-report/src/List/index.ts.html +89 -0
  154. package/coverage/lcov-report/src/Queue/index.html +117 -0
  155. package/coverage/lcov-report/src/Queue/index.ts.html +218 -0
  156. package/coverage/lcov-report/src/Set/index.html +117 -0
  157. package/coverage/lcov-report/src/Set/index.ts.html +488 -0
  158. package/coverage/lcov-report/src/Stack/index.html +117 -0
  159. package/coverage/lcov-report/src/Stack/index.ts.html +215 -0
  160. package/coverage/lcov-report/src/index.html +117 -0
  161. package/coverage/lcov-report/src/index.ts.html +125 -0
  162. package/coverage/lcov.info +1346 -0
  163. package/package.json +42 -0
  164. package/src/Algorithms/index.spec.ts +15 -0
  165. package/src/Algorithms/index.ts +21 -0
  166. package/src/Array/IArray.ts +10 -0
  167. package/src/Array/index.spec.ts +151 -0
  168. package/src/Array/index.ts +68 -0
  169. package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +29 -0
  170. package/src/BinarySearchTree/BinarySearchTreeNode.ts +23 -0
  171. package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +6 -0
  172. package/src/BinarySearchTree/IBinarySearchTree.ts +16 -0
  173. package/src/BinarySearchTree/IBinarySearchTreeData.ts +9 -0
  174. package/src/BinarySearchTree/index.spec.ts +486 -0
  175. package/src/BinarySearchTree/index.ts +198 -0
  176. package/src/DataStructure/index.spec.ts +25 -0
  177. package/src/DataStructure/index.ts +15 -0
  178. package/src/Deck/IDeck.ts +10 -0
  179. package/src/Deck/index.spec.ts +131 -0
  180. package/src/Deck/index.ts +51 -0
  181. package/src/Dictionary/IDictionary.ts +18 -0
  182. package/src/Dictionary/IDictionaryData.ts +5 -0
  183. package/src/Dictionary/index.spec.ts +310 -0
  184. package/src/Dictionary/index.ts +88 -0
  185. package/src/DoublyLinkedList/IDoublyLinkedList.ts +18 -0
  186. package/src/DoublyLinkedList/Node.ts +9 -0
  187. package/src/DoublyLinkedList/index.spec.ts +478 -0
  188. package/src/DoublyLinkedList/index.ts +250 -0
  189. package/src/Graph/GraphNodeNotFoundError.ts +7 -0
  190. package/src/Graph/GraphSearchNodeStates.ts +8 -0
  191. package/src/Graph/IGraph.ts +15 -0
  192. package/src/Graph/IGraphOptions.ts +6 -0
  193. package/src/Graph/index.spec.ts +318 -0
  194. package/src/Graph/index.ts +170 -0
  195. package/src/HashTable/IHashTable.ts +11 -0
  196. package/src/HashTable/IHashTableData.ts +5 -0
  197. package/src/HashTable/IHashTableInputs.ts +5 -0
  198. package/src/HashTable/IHashTableOptions.ts +5 -0
  199. package/src/HashTable/index.spec.ts +136 -0
  200. package/src/HashTable/index.ts +78 -0
  201. package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +28 -0
  202. package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +18 -0
  203. package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +11 -0
  204. package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +7 -0
  205. package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +5 -0
  206. package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +5 -0
  207. package/src/HashTableLinearProbing/index.spec.ts +266 -0
  208. package/src/HashTableLinearProbing/index.ts +128 -0
  209. package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +21 -0
  210. package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +12 -0
  211. package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +7 -0
  212. package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +5 -0
  213. package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +5 -0
  214. package/src/HashTableSeparateChaining/index.spec.ts +173 -0
  215. package/src/HashTableSeparateChaining/index.ts +104 -0
  216. package/src/LinkedList/ILinkedList.ts +15 -0
  217. package/src/LinkedList/ILinkedListItem.ts +6 -0
  218. package/src/LinkedList/Node.ts +8 -0
  219. package/src/LinkedList/index.spec.ts +355 -0
  220. package/src/LinkedList/index.ts +185 -0
  221. package/src/List/index.ts +1 -0
  222. package/src/MaxHeap/IMaxHeap.ts +10 -0
  223. package/src/MaxHeap/IMaxHeapOptions.ts +6 -0
  224. package/src/MaxHeap/index.spec.ts +161 -0
  225. package/src/MaxHeap/index.ts +91 -0
  226. package/src/MinHeap/IMinHeap.ts +10 -0
  227. package/src/MinHeap/IMinHeapOptions.ts +6 -0
  228. package/src/MinHeap/index.spec.ts +161 -0
  229. package/src/MinHeap/index.ts +91 -0
  230. package/src/Queue/IQueue.ts +9 -0
  231. package/src/Queue/index.spec.ts +92 -0
  232. package/src/Queue/index.ts +44 -0
  233. package/src/Set/ISet.ts +17 -0
  234. package/src/Set/index.spec.ts +246 -0
  235. package/src/Set/index.ts +134 -0
  236. package/src/Stack/IStack.ts +9 -0
  237. package/src/Stack/index.spec.ts +108 -0
  238. package/src/Stack/index.ts +43 -0
  239. package/src/index.ts +17 -0
  240. package/tsconfig.json +104 -0
@@ -0,0 +1,478 @@
1
+ import {describe, it, expect} from '@jest/globals';
2
+
3
+ import DoublyLinkedList from './';
4
+
5
+ describe('DoublyLinkedList', () => {
6
+ it('Should create an empty doubly linked list with no problems', () => {
7
+ const doublyLinkedList = new DoublyLinkedList();
8
+
9
+ expect(doublyLinkedList.data).toEqual([]);
10
+ expect(doublyLinkedList.size).toBe(0);
11
+ });
12
+
13
+ it('Should creat a filled doubly linked list with no problems', () => {
14
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
15
+
16
+ expect(doublyLinkedList.data).toEqual([
17
+ {
18
+ previous: null,
19
+ value: 1,
20
+ next: 2,
21
+ },
22
+ {
23
+ previous: 1,
24
+ value: 2,
25
+ next: 3,
26
+ },
27
+ {
28
+ previous: 2,
29
+ value: 3,
30
+ next: 4,
31
+ },
32
+ {
33
+ previous: 3,
34
+ value: 4,
35
+ next: null,
36
+ },
37
+ ]);
38
+ expect(doublyLinkedList.size).toBe(4);
39
+ });
40
+
41
+ describe('.push()', () => {
42
+ it('Should push a new element in a filled doubly linked list', () => {
43
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
44
+ doublyLinkedList.push(5);
45
+
46
+ expect(doublyLinkedList.data).toEqual([
47
+ {
48
+ previous: null,
49
+ value: 1,
50
+ next: 2,
51
+ },
52
+ {
53
+ previous: 1,
54
+ value: 2,
55
+ next: 3,
56
+ },
57
+ {
58
+ previous: 2,
59
+ value: 3,
60
+ next: 4,
61
+ },
62
+ {
63
+ previous: 3,
64
+ value: 4,
65
+ next: 5,
66
+ },
67
+ {
68
+ previous: 4,
69
+ value: 5,
70
+ next: null,
71
+ },
72
+ ]);
73
+ expect(doublyLinkedList.size).toBe(5);
74
+ });
75
+
76
+ it('Should push a new element in an empty doubly linked list', () => {
77
+ const doublyLinkedList = new DoublyLinkedList();
78
+ doublyLinkedList.push(1);
79
+
80
+ expect(doublyLinkedList.data).toEqual([
81
+ {
82
+ previous: null,
83
+ value: 1,
84
+ next: null,
85
+ },
86
+ ]);
87
+ expect(doublyLinkedList.size).toBe(1);
88
+ });
89
+
90
+ it('Should return the new element', () => {
91
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
92
+ const returned = doublyLinkedList.push(5);
93
+
94
+ expect(returned).toBe(5);
95
+ });
96
+ });
97
+
98
+ describe('.getFromPosition()', () => {
99
+ it('Should return undefined when receive a position lower than zero', () => {
100
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
101
+ const returned = doublyLinkedList.getFromPosition(-1);
102
+
103
+ expect(returned).toBeUndefined();
104
+ });
105
+
106
+ it('Should return undefined when receive a position equals the size', () => {
107
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
108
+ const returned = doublyLinkedList.getFromPosition(4);
109
+
110
+ expect(returned).toBeUndefined();
111
+ });
112
+
113
+ it('Should return undefined when receive a position greater than the size', () => {
114
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
115
+ const returned = doublyLinkedList.getFromPosition(5);
116
+
117
+ expect(returned).toBeUndefined();
118
+ });
119
+
120
+ it('Should return the correct item when closest to the head', () => {
121
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
122
+ const returned = doublyLinkedList.getFromPosition(1);
123
+
124
+ expect(returned).toEqual({
125
+ previous: 1,
126
+ value: 2,
127
+ next: 3,
128
+ });
129
+ });
130
+
131
+ it('Should return the correct item when closest to the tail', () => {
132
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
133
+ const returned = doublyLinkedList.getFromPosition(2);
134
+
135
+ expect(returned).toEqual({
136
+ previous: 2,
137
+ value: 3,
138
+ next: 4,
139
+ });
140
+ });
141
+ });
142
+
143
+ describe('.positionOf()', () => {
144
+ it('Should return the position of an element', () => {
145
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
146
+ const returned = doublyLinkedList.positionOf(3);
147
+
148
+ expect(returned).toBe(2);
149
+ });
150
+
151
+ it('Should return undefined when the element is not in the doubly linked list', () => {
152
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
153
+ const returned = doublyLinkedList.positionOf(5);
154
+
155
+ expect(returned).toBeUndefined();
156
+ });
157
+ });
158
+
159
+ describe('.insertInPosition()', () => {
160
+ it('Should return undefined when receive a negative position', () => {
161
+ const doublyLinkedList = new DoublyLinkedList();
162
+ const returned = doublyLinkedList.insertInPosition(1, -1);
163
+
164
+ expect(returned).toBeUndefined();
165
+ expect(doublyLinkedList.data).toEqual([]);
166
+ expect(doublyLinkedList.size).toBe(0);
167
+ });
168
+
169
+ it('Should return undefined when receive a position greater than the size', () => {
170
+ const doublyLinkedList = new DoublyLinkedList();
171
+ const returned = doublyLinkedList.insertInPosition(1, doublyLinkedList.size + 1);
172
+
173
+ expect(returned).toBeUndefined();
174
+ expect(doublyLinkedList.data).toEqual([]);
175
+ expect(doublyLinkedList.size).toBe(0);
176
+ });
177
+
178
+ it('Should insert a new element in the first position', () => {
179
+ const doublyLinkedList = new DoublyLinkedList(2);
180
+ doublyLinkedList.insertInPosition(1, 0);
181
+
182
+ expect(doublyLinkedList.data).toEqual([
183
+ {
184
+ previous: null,
185
+ value: 1,
186
+ next: 2,
187
+ },
188
+ {
189
+ previous: 1,
190
+ value: 2,
191
+ next: null,
192
+ },
193
+ ]);
194
+ expect(doublyLinkedList.size).toBe(2);
195
+ });
196
+
197
+ it('Should insert a new element in the penultimate position', () => {
198
+ const doublyLinkedList = new DoublyLinkedList(1, 3);
199
+ doublyLinkedList.insertInPosition(2, 1);
200
+
201
+ expect(doublyLinkedList.data).toEqual([
202
+ {
203
+ previous: null,
204
+ value: 1,
205
+ next: 2,
206
+ },
207
+ {
208
+ previous: 1,
209
+ value: 2,
210
+ next: 3,
211
+ },
212
+ {
213
+ previous: 2,
214
+ value: 3,
215
+ next: null,
216
+ },
217
+ ]);
218
+ expect(doublyLinkedList.size).toBe(3);
219
+ });
220
+
221
+ it('Should insert a new element in the last position', () => {
222
+ const doublyLinkedList = new DoublyLinkedList(1, 2);
223
+ doublyLinkedList.insertInPosition(3, 2);
224
+
225
+ expect(doublyLinkedList.data).toEqual([
226
+ {
227
+ previous: null,
228
+ value: 1,
229
+ next: 2,
230
+ },
231
+ {
232
+ previous: 1,
233
+ value: 2,
234
+ next: 3,
235
+ },
236
+ {
237
+ previous: 2,
238
+ value: 3,
239
+ next: null,
240
+ },
241
+ ]);
242
+ expect(doublyLinkedList.size).toBe(3);
243
+ });
244
+
245
+ it('Should insert a new element in a position closest to the head', () => {
246
+ const doublyLinkedList = new DoublyLinkedList(1, 3, 4, 5);
247
+ doublyLinkedList.insertInPosition(2, 1);
248
+
249
+ expect(doublyLinkedList.data).toEqual([
250
+ {
251
+ previous: null,
252
+ value: 1,
253
+ next: 2,
254
+ },
255
+ {
256
+ previous: 1,
257
+ value: 2,
258
+ next: 3,
259
+ },
260
+ {
261
+ previous: 2,
262
+ value: 3,
263
+ next: 4,
264
+ },
265
+ {
266
+ previous: 3,
267
+ value: 4,
268
+ next: 5,
269
+ },
270
+ {
271
+ previous: 4,
272
+ value: 5,
273
+ next: null,
274
+ },
275
+ ]);
276
+ expect(doublyLinkedList.size).toBe(5);
277
+ });
278
+
279
+ it('Should insert a new element in a position closest to the head', () => {
280
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 4, 5);
281
+ doublyLinkedList.insertInPosition(3, 2);
282
+
283
+ expect(doublyLinkedList.data).toEqual([
284
+ {
285
+ previous: null,
286
+ value: 1,
287
+ next: 2,
288
+ },
289
+ {
290
+ previous: 1,
291
+ value: 2,
292
+ next: 3,
293
+ },
294
+ {
295
+ previous: 2,
296
+ value: 3,
297
+ next: 4,
298
+ },
299
+ {
300
+ previous: 3,
301
+ value: 4,
302
+ next: 5,
303
+ },
304
+ {
305
+ previous: 4,
306
+ value: 5,
307
+ next: null,
308
+ },
309
+ ]);
310
+ expect(doublyLinkedList.size).toBe(5);
311
+ });
312
+
313
+ it('Should return the inserted element', () => {
314
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 5);
315
+ const returned = doublyLinkedList.insertInPosition(4, 3);
316
+
317
+ expect(returned).toBe(4);
318
+ });
319
+ });
320
+
321
+ describe('.remove()', () => {
322
+ it('Should remove an element in the first position', () => {
323
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
324
+ doublyLinkedList.remove(1);
325
+
326
+ expect(doublyLinkedList.data).toEqual([
327
+ {
328
+ previous: null,
329
+ value: 2,
330
+ next: 3,
331
+ },
332
+ {
333
+ previous: 2,
334
+ value: 3,
335
+ next: 4,
336
+ },
337
+ {
338
+ previous: 3,
339
+ value: 4,
340
+ next: null,
341
+ },
342
+ ]);
343
+ expect(doublyLinkedList.size).toBe(3);
344
+ });
345
+
346
+ it('Should remove an element in the last position', () => {
347
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
348
+ doublyLinkedList.remove(4);
349
+
350
+ expect(doublyLinkedList.data).toEqual([
351
+ {
352
+ previous: null,
353
+ value: 1,
354
+ next: 2,
355
+ },
356
+ {
357
+ previous: 1,
358
+ value: 2,
359
+ next: 3,
360
+ },
361
+ {
362
+ previous: 2,
363
+ value: 3,
364
+ next: null,
365
+ },
366
+ ]);
367
+ expect(doublyLinkedList.size).toBe(3);
368
+ });
369
+
370
+ it('Should remove an element in a middle position', () => {
371
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
372
+ doublyLinkedList.remove(3);
373
+
374
+ expect(doublyLinkedList.data).toEqual([
375
+ {
376
+ previous: null,
377
+ value: 1,
378
+ next: 2,
379
+ },
380
+ {
381
+ previous: 1,
382
+ value: 2,
383
+ next: 4,
384
+ },
385
+ {
386
+ previous: 2,
387
+ value: 4,
388
+ next: null,
389
+ },
390
+ ]);
391
+ expect(doublyLinkedList.size).toBe(3);
392
+ });
393
+
394
+ it('Should return the removed element', () => {
395
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
396
+ const returned = doublyLinkedList.remove(2);
397
+
398
+ expect(returned).toBe(2);
399
+ });
400
+
401
+ it('Should return undefined when the element is not in the doubly linked list', () => {
402
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
403
+ const returned = doublyLinkedList.remove(5);
404
+
405
+ expect(returned).toBeUndefined();
406
+ });
407
+ });
408
+
409
+ describe('.removeFromPosition()', () => {
410
+ it('Should return undefined when receive a negative position', () => {
411
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
412
+ const returned = doublyLinkedList.removeFromPosition(-1);
413
+
414
+ expect(returned).toBeUndefined();
415
+ });
416
+
417
+ it('Should return undefined when receive a position equals the size', () => {
418
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
419
+ const returned = doublyLinkedList.removeFromPosition(doublyLinkedList.size);
420
+
421
+ expect(returned).toBeUndefined();
422
+ });
423
+
424
+ it('Should return undefined when receive a position greater than the size', () => {
425
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
426
+ const returned = doublyLinkedList.removeFromPosition(doublyLinkedList.size + 1);
427
+
428
+ expect(returned).toBeUndefined();
429
+ });
430
+
431
+ it('Should remove an element from position', () => {
432
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
433
+ doublyLinkedList.removeFromPosition(2);
434
+
435
+ expect(doublyLinkedList.data).toEqual([
436
+ {
437
+ previous: null,
438
+ value: 1,
439
+ next: 2,
440
+ },
441
+ {
442
+ previous: 1,
443
+ value: 2,
444
+ next: 4,
445
+ },
446
+ {
447
+ previous: 2,
448
+ value: 4,
449
+ next: null,
450
+ },
451
+ ]);
452
+ expect(doublyLinkedList.size).toBe(3);
453
+ });
454
+
455
+ it('Should return the removed element', () => {
456
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
457
+ const returned = doublyLinkedList.removeFromPosition(3);
458
+
459
+ expect(returned).toBe(4);
460
+ });
461
+ });
462
+
463
+ describe('Conversion to primitive', () => {
464
+ it('Should return double-arrow-separated elements in string conversion', () => {
465
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
466
+ const string = String(doublyLinkedList);
467
+
468
+ expect(string).toBe('[Head] 1 <=> 2 <=> 3 <=> 4 [Tail]');
469
+ });
470
+
471
+ it('Should return the doubly linked list size in number conversion', () => {
472
+ const doublyLinkedList = new DoublyLinkedList(1, 2, 3, 4);
473
+ const number = Number(doublyLinkedList);
474
+
475
+ expect(number).toBe(4);
476
+ });
477
+ });
478
+ });
@@ -0,0 +1,250 @@
1
+ import IDoublyLinkedList, {IDoublyLinkedListItem} from './IDoublyLinkedList';
2
+ import Node from './Node';
3
+
4
+ export default class DoublyLinkedList<T = number> implements IDoublyLinkedList<T> {
5
+ private _head: Node<T> | null = null;
6
+ private _tail: Node<T> | null = null;
7
+ private _size: number = 0;
8
+
9
+ constructor(...inputs: T[]) {
10
+ if (inputs.length) {
11
+ const nodes = inputs.map(input => new Node(input));
12
+
13
+ for (let i = 0; i < inputs.length; i++) {
14
+ nodes[i].previous = nodes[i - 1] || null;
15
+ nodes[i].next = nodes[i + 1] || null;
16
+ }
17
+
18
+ this._head = nodes[0];
19
+ this._tail = nodes[nodes.length - 1];
20
+ this._size = inputs.length;
21
+ }
22
+ }
23
+
24
+ public get data(): IDoublyLinkedListItem<T>[] {
25
+ let current = this._head;
26
+ const data = [];
27
+
28
+ while (current) {
29
+ data.push({
30
+ previous: current.previous?.value || null,
31
+ value: current.value,
32
+ next: current.next?.value || null,
33
+ });
34
+ current = current.next;
35
+ }
36
+
37
+ return data;
38
+ }
39
+
40
+ public get size(): number {
41
+ return this._size;
42
+ }
43
+
44
+ /**
45
+ * Complexity: O(1)
46
+ */
47
+ public push(element: T): T {
48
+ const node = new Node(element);
49
+
50
+ if (this._tail) {
51
+ this._tail.next = node;
52
+ node.previous = this._tail;
53
+ } else {
54
+ this._head = node;
55
+ this._tail = node;
56
+ }
57
+
58
+ this._size += 1;
59
+ return element;
60
+ }
61
+
62
+ /**
63
+ * Complexity: O(n/2)
64
+ */
65
+ public getFromPosition(position: number): IDoublyLinkedListItem<T> | undefined {
66
+ if (position < 0 || position >= this._size) {
67
+ return undefined;
68
+ }
69
+
70
+ const distanceToTheHead = position;
71
+ const distanceToTheTail = this.size - position - 1;
72
+ let current: Node<T> | null;
73
+
74
+ if (distanceToTheTail > distanceToTheHead) {
75
+ current = this._head;
76
+
77
+ for (let i = 0; i < position; i++) {
78
+ current = current?.next || null;
79
+ }
80
+ } else {
81
+ current = this._tail;
82
+
83
+ for (let i = this.size - 1; i > position; i--) {
84
+ current = current?.previous || null;
85
+ }
86
+ }
87
+
88
+ if (current?.value) {
89
+ return {
90
+ previous: current?.previous?.value || null,
91
+ value: current?.value,
92
+ next: current?.next?.value || null,
93
+ };
94
+ }
95
+ }
96
+
97
+ public positionOf(element: T): number | undefined {
98
+ let current = this._head;
99
+ let position = 0;
100
+
101
+ while (current && current.value !== element) {
102
+ current = current.next;
103
+ position += 1;
104
+ }
105
+
106
+ return position === this.size ? undefined : position;
107
+ }
108
+
109
+ /**
110
+ * Complexity: O(n)
111
+ */
112
+ public insertInPosition(element: T, position: number): T | undefined {
113
+ if (position < 0 || position > this.size) {
114
+ return undefined;
115
+ }
116
+
117
+ const node = new Node<T>(element);
118
+
119
+ if (position === 0 && this._head) {
120
+ node.next = this._head;
121
+ this._head.previous = node;
122
+ this._head = node;
123
+
124
+ this._size += 1;
125
+
126
+ return element;
127
+ }
128
+
129
+ if (position === this.size - 1 && this._tail && this._tail.previous) {
130
+ this._tail.previous.next = node;
131
+ node.previous = this._tail?.previous || null;
132
+ node.next = this._tail;
133
+ this._tail.previous = node;
134
+
135
+ this._size += 1;
136
+
137
+ return element;
138
+ }
139
+
140
+ if (position === this.size && this._tail) {
141
+ node.previous = this._tail;
142
+ this._tail.next = node;
143
+ this._tail = node;
144
+
145
+ this._size += 1;
146
+
147
+ return element;
148
+ }
149
+
150
+ const way = this._findFasterWayToPosition(position);
151
+ let current: Node<T> | null;
152
+
153
+ if (way === 'ASC') {
154
+ current = this._head;
155
+
156
+ for (let i = 0; i < position; i++) {
157
+ current = current?.next || null;
158
+ }
159
+ } else {
160
+ current = this._tail;
161
+
162
+ for (let i = this.size - 1; i > position; i--) {
163
+ current = current?.previous || null;
164
+ }
165
+ }
166
+
167
+ if (current && current.previous) {
168
+ current.previous.next = node;
169
+ node.previous = current.previous;
170
+ node.next = current;
171
+ current.previous = node;
172
+ }
173
+
174
+ this._size += 1;
175
+
176
+ return element;
177
+ }
178
+
179
+ /**
180
+ * Complexity: O(n)
181
+ */
182
+ public remove(element: T): T | undefined {
183
+ let current = this._head;
184
+
185
+ while (current && current.value !== element) {
186
+ current = current.next;
187
+ }
188
+
189
+ if (current) {
190
+ if (current.previous) {
191
+ current.previous.next = current.next;
192
+ }
193
+
194
+ if (current.next) {
195
+ current.next.previous = current.previous;
196
+ }
197
+
198
+ if (current === this._head) {
199
+ this._head = current.next;
200
+ }
201
+
202
+ if (current === this._tail) {
203
+ this._tail = current.previous;
204
+ }
205
+
206
+ this._size -= 1;
207
+
208
+ return current.value;
209
+ }
210
+
211
+ return undefined;
212
+ }
213
+
214
+ /**
215
+ * Complexity: O(n)
216
+ */
217
+ public removeFromPosition(position: number): T | undefined {
218
+ const item = this.getFromPosition(position);
219
+
220
+ if (item) {
221
+ this.remove(item.value);
222
+ return item.value;
223
+ }
224
+
225
+ return undefined;
226
+ }
227
+
228
+ private _findFasterWayToPosition(position: number): 'ASC' | 'DESC' {
229
+ const distanceToTheHead = position;
230
+ const distanceToTheTail = this.size - position - 1;
231
+
232
+ if (distanceToTheTail > distanceToTheHead) {
233
+ return 'ASC';
234
+ }
235
+
236
+ return 'DESC';
237
+ }
238
+
239
+ private [Symbol.toPrimitive](type: string): string | number | null {
240
+ if (type === 'string') {
241
+ return `[Head] ${this.data.map(({value}) => value).join(' <=> ')} [Tail]`;
242
+ }
243
+
244
+ if (type === 'number') {
245
+ return this.size;
246
+ }
247
+
248
+ return null;
249
+ }
250
+ }
@@ -0,0 +1,7 @@
1
+ export default class GraphNodeNotFoundError extends Error {
2
+ constructor(node: string) {
3
+ super(`Node ${node} not found`);
4
+
5
+ this.name = 'GraphNodeNotFoundError';
6
+ }
7
+ }