@gabrielrufino/cube 1.0.1 → 1.0.6

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 (123) hide show
  1. package/package.json +6 -6
  2. package/.github/workflows/cd.yml +0 -26
  3. package/.github/workflows/ci.yml +0 -23
  4. package/coverage/clover.xml +0 -706
  5. package/coverage/coverage-final.json +0 -19
  6. package/coverage/lcov-report/base.css +0 -224
  7. package/coverage/lcov-report/block-navigation.js +0 -87
  8. package/coverage/lcov-report/favicon.png +0 -0
  9. package/coverage/lcov-report/index.html +0 -327
  10. package/coverage/lcov-report/prettify.css +0 -1
  11. package/coverage/lcov-report/prettify.js +0 -2
  12. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  13. package/coverage/lcov-report/sorter.js +0 -196
  14. package/coverage/lcov-report/src/Algorithms/index.html +0 -117
  15. package/coverage/lcov-report/src/Algorithms/index.ts.html +0 -149
  16. package/coverage/lcov-report/src/Array/index.html +0 -117
  17. package/coverage/lcov-report/src/Array/index.ts.html +0 -290
  18. package/coverage/lcov-report/src/DataStructure/index.html +0 -117
  19. package/coverage/lcov-report/src/DataStructure/index.ts.html +0 -131
  20. package/coverage/lcov-report/src/Deck/index.html +0 -117
  21. package/coverage/lcov-report/src/Deck/index.ts.html +0 -239
  22. package/coverage/lcov-report/src/Dictionary/index.html +0 -117
  23. package/coverage/lcov-report/src/Dictionary/index.ts.html +0 -350
  24. package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +0 -113
  25. package/coverage/lcov-report/src/DoublyLinkedList/index.html +0 -132
  26. package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +0 -836
  27. package/coverage/lcov-report/src/HashTable/index.html +0 -117
  28. package/coverage/lcov-report/src/HashTable/index.ts.html +0 -320
  29. package/coverage/lcov-report/src/HashTableLinearProbing/index.html +0 -117
  30. package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +0 -95
  31. package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +0 -149
  32. package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +0 -132
  33. package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +0 -398
  34. package/coverage/lcov-report/src/LinkedList/Node.ts.html +0 -110
  35. package/coverage/lcov-report/src/LinkedList/index.html +0 -132
  36. package/coverage/lcov-report/src/LinkedList/index.ts.html +0 -641
  37. package/coverage/lcov-report/src/List/index.html +0 -117
  38. package/coverage/lcov-report/src/List/index.ts.html +0 -89
  39. package/coverage/lcov-report/src/Queue/index.html +0 -117
  40. package/coverage/lcov-report/src/Queue/index.ts.html +0 -218
  41. package/coverage/lcov-report/src/Set/index.html +0 -117
  42. package/coverage/lcov-report/src/Set/index.ts.html +0 -488
  43. package/coverage/lcov-report/src/Stack/index.html +0 -117
  44. package/coverage/lcov-report/src/Stack/index.ts.html +0 -215
  45. package/coverage/lcov-report/src/index.html +0 -117
  46. package/coverage/lcov-report/src/index.ts.html +0 -125
  47. package/coverage/lcov.info +0 -1346
  48. package/src/Algorithms/index.spec.ts +0 -15
  49. package/src/Algorithms/index.ts +0 -21
  50. package/src/Array/IArray.ts +0 -10
  51. package/src/Array/index.spec.ts +0 -151
  52. package/src/Array/index.ts +0 -68
  53. package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +0 -29
  54. package/src/BinarySearchTree/BinarySearchTreeNode.ts +0 -23
  55. package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +0 -6
  56. package/src/BinarySearchTree/IBinarySearchTree.ts +0 -16
  57. package/src/BinarySearchTree/IBinarySearchTreeData.ts +0 -9
  58. package/src/BinarySearchTree/index.spec.ts +0 -486
  59. package/src/BinarySearchTree/index.ts +0 -198
  60. package/src/DataStructure/index.spec.ts +0 -25
  61. package/src/DataStructure/index.ts +0 -15
  62. package/src/Deck/IDeck.ts +0 -10
  63. package/src/Deck/index.spec.ts +0 -131
  64. package/src/Deck/index.ts +0 -51
  65. package/src/Dictionary/IDictionary.ts +0 -18
  66. package/src/Dictionary/IDictionaryData.ts +0 -5
  67. package/src/Dictionary/index.spec.ts +0 -310
  68. package/src/Dictionary/index.ts +0 -88
  69. package/src/DoublyLinkedList/IDoublyLinkedList.ts +0 -18
  70. package/src/DoublyLinkedList/Node.ts +0 -9
  71. package/src/DoublyLinkedList/index.spec.ts +0 -478
  72. package/src/DoublyLinkedList/index.ts +0 -250
  73. package/src/Graph/GraphNodeNotFoundError.ts +0 -7
  74. package/src/Graph/GraphSearchNodeStates.ts +0 -8
  75. package/src/Graph/IGraph.ts +0 -15
  76. package/src/Graph/IGraphOptions.ts +0 -6
  77. package/src/Graph/index.spec.ts +0 -318
  78. package/src/Graph/index.ts +0 -170
  79. package/src/HashTable/IHashTable.ts +0 -11
  80. package/src/HashTable/IHashTableData.ts +0 -5
  81. package/src/HashTable/IHashTableInputs.ts +0 -5
  82. package/src/HashTable/IHashTableOptions.ts +0 -5
  83. package/src/HashTable/index.spec.ts +0 -136
  84. package/src/HashTable/index.ts +0 -78
  85. package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +0 -28
  86. package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +0 -18
  87. package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +0 -11
  88. package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +0 -7
  89. package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +0 -5
  90. package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +0 -5
  91. package/src/HashTableLinearProbing/index.spec.ts +0 -266
  92. package/src/HashTableLinearProbing/index.ts +0 -128
  93. package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +0 -21
  94. package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +0 -12
  95. package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +0 -7
  96. package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +0 -5
  97. package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +0 -5
  98. package/src/HashTableSeparateChaining/index.spec.ts +0 -173
  99. package/src/HashTableSeparateChaining/index.ts +0 -104
  100. package/src/LinkedList/ILinkedList.ts +0 -15
  101. package/src/LinkedList/ILinkedListItem.ts +0 -6
  102. package/src/LinkedList/Node.ts +0 -8
  103. package/src/LinkedList/index.spec.ts +0 -355
  104. package/src/LinkedList/index.ts +0 -185
  105. package/src/List/index.ts +0 -1
  106. package/src/MaxHeap/IMaxHeap.ts +0 -10
  107. package/src/MaxHeap/IMaxHeapOptions.ts +0 -6
  108. package/src/MaxHeap/index.spec.ts +0 -161
  109. package/src/MaxHeap/index.ts +0 -91
  110. package/src/MinHeap/IMinHeap.ts +0 -10
  111. package/src/MinHeap/IMinHeapOptions.ts +0 -6
  112. package/src/MinHeap/index.spec.ts +0 -161
  113. package/src/MinHeap/index.ts +0 -91
  114. package/src/Queue/IQueue.ts +0 -9
  115. package/src/Queue/index.spec.ts +0 -92
  116. package/src/Queue/index.ts +0 -44
  117. package/src/Set/ISet.ts +0 -17
  118. package/src/Set/index.spec.ts +0 -246
  119. package/src/Set/index.ts +0 -134
  120. package/src/Stack/IStack.ts +0 -9
  121. package/src/Stack/index.spec.ts +0 -108
  122. package/src/Stack/index.ts +0 -43
  123. package/src/index.ts +0 -17
@@ -1,478 +0,0 @@
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
- });
@@ -1,250 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
1
- export default class GraphNodeNotFoundError extends Error {
2
- constructor(node: string) {
3
- super(`Node ${node} not found`);
4
-
5
- this.name = 'GraphNodeNotFoundError';
6
- }
7
- }