data-structure-typed 0.9.16 → 1.3.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 (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +134 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,262 +1,526 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DoublyLinkedList = exports.DoublyLinkedListNode = void 0;
4
- var DoublyLinkedListNode = /** @class */ (function () {
5
- function DoublyLinkedListNode(nodeValue) {
6
- this.val = nodeValue;
7
- this.next = null;
8
- this.prev = null;
9
- }
10
- return DoublyLinkedListNode;
11
- }());
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
11
+ class DoublyLinkedListNode {
12
+ /**
13
+ * The constructor function initializes the value, next, and previous properties of an object.
14
+ * @param {T} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
15
+ * is defined as a generic type "T".
16
+ */
17
+ constructor(val) {
18
+ this._val = val;
19
+ this._next = null;
20
+ this._prev = null;
21
+ }
22
+ get val() {
23
+ return this._val;
24
+ }
25
+ set val(value) {
26
+ this._val = value;
27
+ }
28
+ get next() {
29
+ return this._next;
30
+ }
31
+ set next(value) {
32
+ this._next = value;
33
+ }
34
+ get prev() {
35
+ return this._prev;
36
+ }
37
+ set prev(value) {
38
+ this._prev = value;
39
+ }
40
+ }
12
41
  exports.DoublyLinkedListNode = DoublyLinkedListNode;
13
- var DoublyLinkedList = /** @class */ (function () {
14
- function DoublyLinkedList() {
15
- this._first = null;
16
- this._last = null;
17
- this._size = 0;
18
- // --- end extra methods ---
19
- }
20
- Object.defineProperty(DoublyLinkedList.prototype, "size", {
21
- get: function () {
22
- return this._size;
23
- },
24
- set: function (v) {
25
- this._size = v;
26
- },
27
- enumerable: false,
28
- configurable: true
29
- });
42
+ class DoublyLinkedList {
30
43
  /**
31
- * Adds a node at the beginning of the linked list
32
- * @param val Value to be stored at the beginning of the linked list
44
+ * The constructor initializes the linked list with an empty head, tail, and length.
33
45
  */
34
- DoublyLinkedList.prototype.offerFirst = function (val) {
35
- var newNode = new DoublyLinkedListNode(val);
36
- if (this._size === 0) {
37
- this._first = newNode;
38
- this._last = newNode;
39
- }
40
- else {
41
- if (this._first)
42
- this._first.prev = newNode;
43
- newNode.next = this._first;
44
- this._first = newNode;
46
+ constructor() {
47
+ this._head = null;
48
+ this._tail = null;
49
+ this._length = 0;
50
+ }
51
+ get head() {
52
+ return this._head;
53
+ }
54
+ set head(value) {
55
+ this._head = value;
56
+ }
57
+ get tail() {
58
+ return this._tail;
59
+ }
60
+ set tail(value) {
61
+ this._tail = value;
62
+ }
63
+ get length() {
64
+ return this._length;
65
+ }
66
+ /**
67
+ * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
68
+ * given array.
69
+ * @param {T[]} data - The `data` parameter is an array of elements of type `T`.
70
+ * @returns The `fromArray` function returns a DoublyLinkedList object.
71
+ */
72
+ static fromArray(data) {
73
+ const doublyLinkedList = new DoublyLinkedList();
74
+ for (const item of data) {
75
+ doublyLinkedList.push(item);
45
76
  }
46
- this._size++;
47
- return true;
48
- };
77
+ return doublyLinkedList;
78
+ }
49
79
  /**
50
- * Adds a node to the end of the linked list
51
- * @param val Value to be stored in the Doubly linked list node
80
+ * The push function adds a new node with the given value to the end of the doubly linked list.
81
+ * @param {T} val - The value to be added to the linked list.
52
82
  */
53
- DoublyLinkedList.prototype.offerLast = function (val) {
54
- var newNode = new DoublyLinkedListNode(val);
55
- if (this._size === 0) {
56
- this._first = newNode;
57
- this._last = newNode;
83
+ push(val) {
84
+ const newNode = new DoublyLinkedListNode(val);
85
+ if (!this.head) {
86
+ this.head = newNode;
87
+ this.tail = newNode;
58
88
  }
59
89
  else {
60
- if (this._last)
61
- this._last.next = newNode;
62
- newNode.prev = this._last;
63
- this._last = newNode;
90
+ newNode.prev = this.tail;
91
+ this.tail.next = newNode;
92
+ this.tail = newNode;
64
93
  }
65
- this._size++;
66
- return true;
67
- };
68
- DoublyLinkedList.prototype.peekFirst = function (by) {
69
- var _a, _b, _c, _d, _e;
70
- switch (by) {
71
- case 'node':
72
- return (_a = this._first) !== null && _a !== void 0 ? _a : null;
73
- case 'val':
74
- return (_c = (_b = this._first) === null || _b === void 0 ? void 0 : _b.val) !== null && _c !== void 0 ? _c : null;
75
- default:
76
- return (_e = (_d = this._first) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
77
- }
78
- };
79
- DoublyLinkedList.prototype.peekLast = function (by) {
80
- var _a, _b, _c, _d, _e;
81
- if (by === void 0) { by = 'val'; }
82
- switch (by) {
83
- case 'node':
84
- return (_a = this._last) !== null && _a !== void 0 ? _a : null;
85
- case 'val':
86
- return (_c = (_b = this._last) === null || _b === void 0 ? void 0 : _b.val) !== null && _c !== void 0 ? _c : null;
87
- default:
88
- return (_e = (_d = this._last) === null || _d === void 0 ? void 0 : _d.val) !== null && _e !== void 0 ? _e : null;
89
- }
90
- };
94
+ this._length++;
95
+ }
91
96
  /**
92
- * Removes a node form the beginning of the linked list and will return the node val
97
+ * The `pop()` function removes and returns the value of the last node in a doubly linked list.
98
+ * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
99
+ * list is empty, it returns null.
93
100
  */
94
- DoublyLinkedList.prototype.pollFirst = function (by) {
95
- var _a, _b, _c;
96
- if (by === void 0) { by = 'val'; }
97
- if (this._size === 0)
101
+ pop() {
102
+ if (!this.tail)
98
103
  return null;
99
- var oldHead = this._first;
100
- if (this._size === 1) {
101
- this._first = null;
102
- this._last = null;
104
+ const removedNode = this.tail;
105
+ if (this.head === this.tail) {
106
+ this.head = null;
107
+ this.tail = null;
103
108
  }
104
109
  else {
105
- this._first = (_a = oldHead === null || oldHead === void 0 ? void 0 : oldHead.next) !== null && _a !== void 0 ? _a : null;
106
- if (this._first)
107
- this._first.prev = null;
108
- if (oldHead)
109
- oldHead.next = null;
110
- }
111
- this._size--;
112
- switch (by) {
113
- case 'node':
114
- return oldHead !== null && oldHead !== void 0 ? oldHead : null;
115
- case 'val':
116
- return (_b = oldHead === null || oldHead === void 0 ? void 0 : oldHead.val) !== null && _b !== void 0 ? _b : null;
117
- default:
118
- return (_c = oldHead === null || oldHead === void 0 ? void 0 : oldHead.val) !== null && _c !== void 0 ? _c : null;
119
- }
120
- };
110
+ this.tail = removedNode.prev;
111
+ this.tail.next = null;
112
+ }
113
+ this._length--;
114
+ return removedNode.val;
115
+ }
121
116
  /**
122
- * Removes a node at the end of the linked list and will return the node value
117
+ * The `shift()` function removes and returns the value of the first node in a doubly linked list.
118
+ * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
119
+ * list.
123
120
  */
124
- DoublyLinkedList.prototype.pollLast = function (by) {
125
- var _a, _b, _c;
126
- if (by === void 0) { by = 'val'; }
127
- if (this._size === 0)
121
+ shift() {
122
+ if (!this.head)
128
123
  return null;
129
- var polled = this._last;
130
- if (this._size === 1) {
131
- this._first = null;
132
- this._last = null;
124
+ const removedNode = this.head;
125
+ if (this.head === this.tail) {
126
+ this.head = null;
127
+ this.tail = null;
133
128
  }
134
129
  else {
135
- this._last = (_a = polled === null || polled === void 0 ? void 0 : polled.prev) !== null && _a !== void 0 ? _a : null;
136
- if (this._last)
137
- this._last.next = null;
138
- if (polled)
139
- polled.prev = null;
140
- }
141
- this._size--;
142
- switch (by) {
143
- case 'node':
144
- return polled !== null && polled !== void 0 ? polled : null;
145
- case 'val':
146
- return (_b = polled === null || polled === void 0 ? void 0 : polled.val) !== null && _b !== void 0 ? _b : null;
147
- default:
148
- return (_c = polled === null || polled === void 0 ? void 0 : polled.val) !== null && _c !== void 0 ? _c : null;
149
- }
150
- };
130
+ this.head = removedNode.next;
131
+ this.head.prev = null;
132
+ }
133
+ this._length--;
134
+ return removedNode.val;
135
+ }
151
136
  /**
152
- * Returns the node at the specified index of the linked list.
153
- * If index = 0; first element in the list is returned.
154
- * If index = 3; fourth element in the list is returned.
155
- * @param index Index of the node to be retrieved
156
- * @param by Return value type
137
+ * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
138
+ * @param {T} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
139
+ * doubly linked list.
157
140
  */
158
- DoublyLinkedList.prototype.get = function (index, by) {
159
- var _a, _b;
160
- if (by === void 0) { by = 'val'; }
161
- if (index < 0 || index >= this._size)
162
- return null;
163
- var count, current;
164
- if (index <= this._size / 2) {
165
- count = 0;
166
- current = this._first;
167
- while (count !== index) {
168
- current = current === null || current === void 0 ? void 0 : current.next;
169
- count++;
170
- }
141
+ unshift(val) {
142
+ const newNode = new DoublyLinkedListNode(val);
143
+ if (!this.head) {
144
+ this.head = newNode;
145
+ this.tail = newNode;
171
146
  }
172
147
  else {
173
- count = this._size - 1;
174
- current = this._last;
175
- while (count !== index) {
176
- current = current === null || current === void 0 ? void 0 : current.prev;
177
- count--;
178
- }
148
+ newNode.next = this.head;
149
+ this.head.prev = newNode;
150
+ this.head = newNode;
151
+ }
152
+ this._length++;
153
+ }
154
+ /**
155
+ * The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
156
+ * @param {number} index - The index parameter is a number that represents the position of the element we want to
157
+ * retrieve from the list.
158
+ * @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
159
+ * or the linked list is empty, it will return null.
160
+ */
161
+ getAt(index) {
162
+ if (index < 0 || index >= this.length)
163
+ return null;
164
+ let current = this.head;
165
+ for (let i = 0; i < index; i++) {
166
+ current = current.next;
179
167
  }
180
- switch (by) {
181
- case 'node':
182
- return current !== null && current !== void 0 ? current : null;
183
- case 'val':
184
- return (_a = current === null || current === void 0 ? void 0 : current.val) !== null && _a !== void 0 ? _a : null;
185
- default:
186
- return (_b = current === null || current === void 0 ? void 0 : current.val) !== null && _b !== void 0 ? _b : null;
168
+ return current.val;
169
+ }
170
+ /**
171
+ * The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
172
+ * range.
173
+ * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
174
+ * retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
175
+ * @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<T>` object if the index is within the
176
+ * valid range of the linked list, otherwise it returns `null`.
177
+ */
178
+ getNodeAt(index) {
179
+ if (index < 0 || index >= this.length)
180
+ return null;
181
+ let current = this.head;
182
+ for (let i = 0; i < index; i++) {
183
+ current = current.next;
187
184
  }
188
- };
185
+ return current;
186
+ }
189
187
  /**
190
- * Updates the value of the node at the specified index.
191
- * If index = 0; Value of the first element in the list is updated.
192
- * If index = 3; Value of the fourth element in the list is updated.
193
- * @param index Index of the node to be updated
194
- * @param val New value of the node
188
+ * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
189
+ * node if found, otherwise it returns null.
190
+ * @param {T} val - The `val` parameter is the value that we want to search for in the doubly linked list.
191
+ * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<T>` if a node with the specified value `val`
192
+ * is found in the linked list. If no such node is found, it returns `null`.
195
193
  */
196
- DoublyLinkedList.prototype.set = function (index, val) {
197
- var foundNode = this.get(index, 'node');
198
- if (foundNode !== null) {
199
- foundNode.val = val;
200
- return true;
194
+ findNode(val) {
195
+ let current = this.head;
196
+ while (current) {
197
+ if (current.val === val) {
198
+ return current;
199
+ }
200
+ current = current.next;
201
201
  }
202
- return false;
203
- };
204
- DoublyLinkedList.prototype.isEmpty = function () {
205
- return this._size === 0;
206
- };
207
- // --- start extra methods ---
202
+ return null;
203
+ }
208
204
  /**
209
- * Inserts a new node at the specified index.
210
- * @param index Index at which the new node has to be inserted
211
- * @param val Value of the new node to be inserted
205
+ * The `insert` function inserts a value at a specified index in a doubly linked list.
206
+ * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
207
+ * DoublyLinkedList. It is of type number.
208
+ * @param {T} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
209
+ * specified index.
210
+ * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
211
+ * if the index is out of bounds.
212
212
  */
213
- DoublyLinkedList.prototype.insert = function (index, val) {
214
- if (index < 0 || index > this._size)
213
+ insertAt(index, val) {
214
+ if (index < 0 || index > this.length)
215
215
  return false;
216
- if (index === 0)
217
- return !!this.offerFirst(val);
218
- if (index === this._size)
219
- return !!this.offerLast(val);
220
- var newNode = new DoublyLinkedListNode(val);
221
- var prevNode = this.get(index - 1, 'node');
222
- var nextNode = prevNode === null || prevNode === void 0 ? void 0 : prevNode.next;
223
- if (prevNode)
224
- prevNode.next = newNode;
216
+ if (index === 0) {
217
+ this.unshift(val);
218
+ return true;
219
+ }
220
+ if (index === this.length) {
221
+ this.push(val);
222
+ return true;
223
+ }
224
+ const newNode = new DoublyLinkedListNode(val);
225
+ const prevNode = this.getNodeAt(index - 1);
226
+ const nextNode = prevNode.next;
225
227
  newNode.prev = prevNode;
226
- newNode.next = nextNode !== null && nextNode !== void 0 ? nextNode : null;
227
- if (nextNode)
228
- nextNode.prev = newNode;
229
- this._size++;
228
+ newNode.next = nextNode;
229
+ prevNode.next = newNode;
230
+ nextNode.prev = newNode;
231
+ this._length++;
230
232
  return true;
231
- };
233
+ }
232
234
  /**
233
- * Removes a node at the specified index and returns its value.
234
- * @param index Index at which the node has to be removed.
235
+ * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
236
+ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
237
+ * data structure. It is of type number.
238
+ * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
239
+ * bounds.
235
240
  */
236
- DoublyLinkedList.prototype.remove = function (index) {
237
- var _a, _b, _c;
238
- if (index < 0 || index > this._size - 1)
241
+ deleteAt(index) {
242
+ if (index < 0 || index >= this.length)
239
243
  return null;
240
- else if (index === 0)
241
- return this.pollFirst();
242
- else if (index === this._size - 1)
243
- return (_b = (_a = this.pollLast('node')) === null || _a === void 0 ? void 0 : _a.val) !== null && _b !== void 0 ? _b : null;
244
+ if (index === 0)
245
+ return this.shift();
246
+ if (index === this.length - 1)
247
+ return this.pop();
248
+ const removedNode = this.getNodeAt(index);
249
+ const prevNode = removedNode.prev;
250
+ const nextNode = removedNode.next;
251
+ prevNode.next = nextNode;
252
+ nextNode.prev = prevNode;
253
+ this._length--;
254
+ return removedNode.val;
255
+ }
256
+ /**
257
+ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
258
+ * @param {T | DoublyLinkedListNode<T>} valOrNode - The `valOrNode` parameter can accept either a value of type `T` or
259
+ * a `DoublyLinkedListNode<T>` object.
260
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
261
+ * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
262
+ */
263
+ delete(valOrNode) {
264
+ let node;
265
+ if (valOrNode instanceof DoublyLinkedListNode) {
266
+ node = valOrNode;
267
+ }
244
268
  else {
245
- var prevNode = this.get(index - 1, 'node');
246
- var removeNode = prevNode === null || prevNode === void 0 ? void 0 : prevNode.next;
247
- var nextNode = removeNode === null || removeNode === void 0 ? void 0 : removeNode.next;
248
- if (prevNode)
249
- prevNode.next = nextNode !== null && nextNode !== void 0 ? nextNode : null;
250
- if (nextNode)
269
+ node = this.findNode(valOrNode);
270
+ }
271
+ if (node) {
272
+ if (node === this.head) {
273
+ this.shift();
274
+ }
275
+ else if (node === this.tail) {
276
+ this.pop();
277
+ }
278
+ else {
279
+ const prevNode = node.prev;
280
+ const nextNode = node.next;
281
+ prevNode.next = nextNode;
251
282
  nextNode.prev = prevNode;
252
- if (removeNode)
253
- removeNode.next = null;
254
- if (removeNode)
255
- removeNode.prev = null;
256
- this._size--;
257
- return (_c = removeNode === null || removeNode === void 0 ? void 0 : removeNode.val) !== null && _c !== void 0 ? _c : null;
258
- }
259
- };
260
- return DoublyLinkedList;
261
- }());
283
+ this._length--;
284
+ }
285
+ return true;
286
+ }
287
+ return false;
288
+ }
289
+ /**
290
+ * The `toArray` function converts a linked list into an array.
291
+ * @returns The `toArray()` method is returning an array of type `T[]`.
292
+ */
293
+ toArray() {
294
+ const array = [];
295
+ let current = this.head;
296
+ while (current) {
297
+ array.push(current.val);
298
+ current = current.next;
299
+ }
300
+ return array;
301
+ }
302
+ /**
303
+ * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
304
+ */
305
+ clear() {
306
+ this._head = null;
307
+ this._tail = null;
308
+ this._length = 0;
309
+ }
310
+ /**
311
+ * The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
312
+ * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
313
+ * function is used to determine whether a particular value in the linked list satisfies a certain condition.
314
+ * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
315
+ * the callback function. If no element satisfies the condition, it returns `null`.
316
+ */
317
+ find(callback) {
318
+ let current = this.head;
319
+ while (current) {
320
+ if (callback(current.val)) {
321
+ return current.val;
322
+ }
323
+ current = current.next;
324
+ }
325
+ return null;
326
+ }
327
+ /**
328
+ * The function returns the index of the first occurrence of a given value in a linked list.
329
+ * @param {T} val - The parameter `val` is of type `T`, which means it can be any data type. It represents the value
330
+ * that we are searching for in the linked list.
331
+ * @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
332
+ * list. If the value is not found, it returns -1.
333
+ */
334
+ indexOf(val) {
335
+ let index = 0;
336
+ let current = this.head;
337
+ while (current) {
338
+ if (current.val === val) {
339
+ return index;
340
+ }
341
+ index++;
342
+ current = current.next;
343
+ }
344
+ return -1;
345
+ }
346
+ /**
347
+ * The `findLast` function iterates through a linked list from the last node to the first node and returns the last
348
+ * value that satisfies the given callback function, or null if no value satisfies the callback.
349
+ * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
350
+ * function is used to determine whether a given value satisfies a certain condition.
351
+ * @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
352
+ * the callback function. If no value satisfies the condition, it returns `null`.
353
+ */
354
+ findLast(callback) {
355
+ let current = this.tail;
356
+ while (current) {
357
+ if (callback(current.val)) {
358
+ return current.val;
359
+ }
360
+ current = current.prev;
361
+ }
362
+ return null;
363
+ }
364
+ /**
365
+ * The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
366
+ * @returns The `toArrayReverse()` function returns an array of type `T[]`.
367
+ */
368
+ toArrayReverse() {
369
+ const array = [];
370
+ let current = this.tail;
371
+ while (current) {
372
+ array.push(current.val);
373
+ current = current.prev;
374
+ }
375
+ return array;
376
+ }
377
+ /**
378
+ * The `reverse` function reverses the order of the elements in a doubly linked list.
379
+ */
380
+ reverse() {
381
+ let current = this.head;
382
+ [this.head, this.tail] = [this.tail, this.head];
383
+ while (current) {
384
+ const next = current.next;
385
+ [current.prev, current.next] = [current.next, current.prev];
386
+ current = next;
387
+ }
388
+ }
389
+ /**
390
+ * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
391
+ * @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
392
+ * represents the value of the current node in the linked list, and the index argument represents the index of the
393
+ * current node in the linked list.
394
+ */
395
+ forEach(callback) {
396
+ let current = this.head;
397
+ let index = 0;
398
+ while (current) {
399
+ callback(current.val, index);
400
+ current = current.next;
401
+ index++;
402
+ }
403
+ }
404
+ /**
405
+ * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
406
+ * DoublyLinkedList with the transformed values.
407
+ * @param callback - The callback parameter is a function that takes a value of type T (the type of values stored in
408
+ * the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
409
+ * DoublyLinkedList).
410
+ * @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
411
+ */
412
+ map(callback) {
413
+ const mappedList = new DoublyLinkedList();
414
+ let current = this.head;
415
+ while (current) {
416
+ mappedList.push(callback(current.val));
417
+ current = current.next;
418
+ }
419
+ return mappedList;
420
+ }
421
+ /**
422
+ * The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
423
+ * elements that satisfy the given callback function.
424
+ * @param callback - The `callback` parameter is a function that takes a value of type `T` and returns a boolean value.
425
+ * It is used to determine whether a value should be included in the filtered list or not.
426
+ * @returns The filtered list, which is an instance of the DoublyLinkedList class.
427
+ */
428
+ filter(callback) {
429
+ const filteredList = new DoublyLinkedList();
430
+ let current = this.head;
431
+ while (current) {
432
+ if (callback(current.val)) {
433
+ filteredList.push(current.val);
434
+ }
435
+ current = current.next;
436
+ }
437
+ return filteredList;
438
+ }
439
+ /**
440
+ * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
441
+ * single value.
442
+ * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
443
+ * used to perform a specific operation on each element of the linked list.
444
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
445
+ * point for the reduction operation.
446
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
447
+ * elements in the linked list.
448
+ */
449
+ reduce(callback, initialValue) {
450
+ let accumulator = initialValue;
451
+ let current = this.head;
452
+ while (current) {
453
+ accumulator = callback(accumulator, current.val);
454
+ current = current.next;
455
+ }
456
+ return accumulator;
457
+ }
458
+ /**
459
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
460
+ * @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
461
+ * after which the new value will be inserted. It can be either the value of the existing node or the existing node
462
+ * itself.
463
+ * @param {T} newValue - The value that you want to insert into the doubly linked list.
464
+ * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
465
+ * existing value or node is not found in the doubly linked list.
466
+ */
467
+ insertAfter(existingValueOrNode, newValue) {
468
+ let existingNode;
469
+ if (existingValueOrNode instanceof DoublyLinkedListNode) {
470
+ existingNode = existingValueOrNode;
471
+ }
472
+ else {
473
+ existingNode = this.findNode(existingValueOrNode);
474
+ }
475
+ if (existingNode) {
476
+ const newNode = new DoublyLinkedListNode(newValue);
477
+ newNode.next = existingNode.next;
478
+ if (existingNode.next) {
479
+ existingNode.next.prev = newNode;
480
+ }
481
+ newNode.prev = existingNode;
482
+ existingNode.next = newNode;
483
+ if (existingNode === this.tail) {
484
+ this.tail = newNode;
485
+ }
486
+ this._length++;
487
+ return true;
488
+ }
489
+ return false;
490
+ }
491
+ /**
492
+ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
493
+ * @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
494
+ * before which the new value will be inserted. It can be either the value of the existing node or the existing node
495
+ * itself.
496
+ * @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
497
+ * list.
498
+ * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
499
+ * insertion fails.
500
+ */
501
+ insertBefore(existingValueOrNode, newValue) {
502
+ let existingNode;
503
+ if (existingValueOrNode instanceof DoublyLinkedListNode) {
504
+ existingNode = existingValueOrNode;
505
+ }
506
+ else {
507
+ existingNode = this.findNode(existingValueOrNode);
508
+ }
509
+ if (existingNode) {
510
+ const newNode = new DoublyLinkedListNode(newValue);
511
+ newNode.prev = existingNode.prev;
512
+ if (existingNode.prev) {
513
+ existingNode.prev.next = newNode;
514
+ }
515
+ newNode.next = existingNode;
516
+ existingNode.prev = newNode;
517
+ if (existingNode === this.head) {
518
+ this.head = newNode;
519
+ }
520
+ this._length++;
521
+ return true;
522
+ }
523
+ return false;
524
+ }
525
+ }
262
526
  exports.DoublyLinkedList = DoublyLinkedList;