data-structure-typed 0.8.18 → 1.3.0

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 (272) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +690 -2
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  7. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -18
  8. package/dist/data-structures/binary-tree/avl-tree.js +110 -37
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
  12. package/dist/data-structures/binary-tree/binary-tree.js +27 -979
  13. package/dist/data-structures/binary-tree/bst.d.ts +118 -28
  14. package/dist/data-structures/binary-tree/bst.js +162 -124
  15. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  16. package/dist/data-structures/binary-tree/index.js +1 -0
  17. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  18. package/dist/data-structures/binary-tree/rb-tree.js +40 -2
  19. package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
  20. package/dist/data-structures/binary-tree/segment-tree.js +80 -17
  21. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
  22. package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
  23. package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
  24. package/dist/data-structures/graph/abstract-graph.js +365 -92
  25. package/dist/data-structures/graph/directed-graph.d.ts +175 -26
  26. package/dist/data-structures/graph/directed-graph.js +249 -95
  27. package/dist/data-structures/graph/index.d.ts +1 -0
  28. package/dist/data-structures/graph/index.js +1 -0
  29. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  30. package/dist/data-structures/graph/map-graph.js +111 -0
  31. package/dist/data-structures/graph/undirected-graph.d.ts +111 -8
  32. package/dist/data-structures/graph/undirected-graph.js +154 -44
  33. package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
  34. package/dist/data-structures/hash/coordinate-map.js +44 -3
  35. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  36. package/dist/data-structures/hash/coordinate-set.js +34 -0
  37. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  38. package/dist/data-structures/hash/hash-table.js +4 -0
  39. package/dist/data-structures/hash/index.d.ts +5 -0
  40. package/dist/data-structures/hash/index.js +5 -0
  41. package/dist/data-structures/hash/pair.d.ts +2 -1
  42. package/dist/data-structures/hash/pair.js +4 -0
  43. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  44. package/dist/data-structures/hash/tree-map.js +4 -0
  45. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-set.js +4 -0
  47. package/dist/data-structures/heap/heap.d.ts +62 -51
  48. package/dist/data-structures/heap/heap.js +106 -63
  49. package/dist/data-structures/heap/max-heap.d.ts +13 -4
  50. package/dist/data-structures/heap/max-heap.js +10 -2
  51. package/dist/data-structures/heap/min-heap.d.ts +14 -4
  52. package/dist/data-structures/heap/min-heap.js +11 -2
  53. package/dist/data-structures/index.d.ts +1 -0
  54. package/dist/data-structures/index.js +1 -0
  55. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
  56. package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
  57. package/dist/data-structures/linked-list/index.d.ts +1 -0
  58. package/dist/data-structures/linked-list/index.js +1 -0
  59. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -319
  60. package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
  61. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  62. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  63. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  64. package/dist/data-structures/matrix/matrix.js +14 -0
  65. package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
  66. package/dist/data-structures/matrix/matrix2d.js +91 -8
  67. package/dist/data-structures/matrix/navigator.d.ts +37 -16
  68. package/dist/data-structures/matrix/navigator.js +28 -0
  69. package/dist/data-structures/matrix/vector2d.d.ts +156 -29
  70. package/dist/data-structures/matrix/vector2d.js +184 -55
  71. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
  72. package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
  73. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
  74. package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
  75. package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
  76. package/dist/data-structures/priority-queue/priority-queue.js +219 -75
  77. package/dist/data-structures/queue/deque.d.ts +141 -13
  78. package/dist/data-structures/queue/deque.js +151 -7
  79. package/dist/data-structures/queue/queue.d.ts +68 -42
  80. package/dist/data-structures/queue/queue.js +95 -51
  81. package/dist/data-structures/stack/stack.d.ts +30 -36
  82. package/dist/data-structures/stack/stack.js +31 -37
  83. package/dist/data-structures/tree/index.d.ts +1 -0
  84. package/dist/data-structures/tree/index.js +17 -0
  85. package/dist/data-structures/tree/tree.d.ts +14 -0
  86. package/dist/{types/utils.js → data-structures/tree/tree.js} +26 -19
  87. package/dist/data-structures/trie/trie.d.ts +39 -6
  88. package/dist/data-structures/trie/trie.js +81 -12
  89. package/dist/index.d.ts +3 -0
  90. package/dist/index.js +3 -0
  91. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  92. package/dist/interfaces/abstract-binary-tree.js +2 -0
  93. package/dist/interfaces/abstract-graph.d.ts +17 -0
  94. package/dist/interfaces/abstract-graph.js +2 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/avl-tree.js +2 -0
  97. package/dist/interfaces/binary-tree.d.ts +6 -0
  98. package/dist/interfaces/binary-tree.js +2 -0
  99. package/dist/interfaces/bst.d.ts +17 -0
  100. package/dist/interfaces/bst.js +2 -0
  101. package/dist/interfaces/directed-graph.d.ts +12 -0
  102. package/dist/interfaces/directed-graph.js +2 -0
  103. package/dist/interfaces/doubly-linked-list.js +2 -0
  104. package/dist/interfaces/heap.js +2 -0
  105. package/dist/interfaces/index.d.ts +15 -0
  106. package/dist/interfaces/index.js +31 -0
  107. package/dist/interfaces/navigator.js +2 -0
  108. package/dist/interfaces/priority-queue.js +2 -0
  109. package/dist/interfaces/rb-tree.d.ts +8 -0
  110. package/dist/interfaces/rb-tree.js +2 -0
  111. package/dist/interfaces/segment-tree.js +2 -0
  112. package/dist/interfaces/singly-linked-list.js +2 -0
  113. package/dist/interfaces/tree-multiset.d.ts +7 -0
  114. package/dist/interfaces/tree-multiset.js +2 -0
  115. package/dist/interfaces/undirected-graph.d.ts +5 -0
  116. package/dist/interfaces/undirected-graph.js +2 -0
  117. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  118. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  119. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  120. package/dist/types/data-structures/abstract-graph.js +2 -0
  121. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  122. package/dist/types/data-structures/avl-tree.js +2 -0
  123. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  124. package/dist/types/data-structures/binary-tree.js +2 -0
  125. package/dist/types/data-structures/bst.d.ts +13 -0
  126. package/dist/types/data-structures/bst.js +9 -0
  127. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  128. package/dist/types/data-structures/directed-graph.js +9 -0
  129. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  130. package/dist/types/data-structures/heap.d.ts +3 -0
  131. package/dist/types/data-structures/heap.js +2 -0
  132. package/dist/types/data-structures/index.d.ts +13 -7
  133. package/dist/types/data-structures/index.js +31 -0
  134. package/dist/types/data-structures/map-graph.d.ts +1 -0
  135. package/dist/types/data-structures/map-graph.js +2 -0
  136. package/dist/types/data-structures/navigator.d.ts +14 -0
  137. package/dist/types/data-structures/navigator.js +2 -0
  138. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  139. package/dist/types/data-structures/priority-queue.js +2 -0
  140. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  141. package/dist/types/data-structures/rb-tree.js +8 -0
  142. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  143. package/dist/types/data-structures/segment-tree.js +2 -0
  144. package/dist/types/data-structures/singly-linked-list.js +2 -0
  145. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  146. package/dist/types/data-structures/tree-multiset.js +2 -0
  147. package/dist/types/helpers.d.ts +1 -0
  148. package/dist/types/helpers.js +2 -0
  149. package/dist/types/index.d.ts +2 -0
  150. package/dist/types/index.js +2 -0
  151. package/dist/types/utils/index.d.ts +2 -0
  152. package/dist/types/utils/index.js +18 -0
  153. package/dist/types/utils/utils.d.ts +7 -0
  154. package/dist/types/utils/utils.js +2 -0
  155. package/dist/types/utils/validate-type.d.ts +19 -0
  156. package/dist/types/utils/validate-type.js +2 -0
  157. package/dist/utils/index.js +17 -0
  158. package/dist/utils/utils.d.ts +19 -0
  159. package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
  160. package/package.json +106 -55
  161. package/.idea/data-structure-typed.iml +0 -12
  162. package/.idea/modules.xml +0 -8
  163. package/.idea/vcs.xml +0 -6
  164. package/dist/data-structures/trampoline.d.ts +0 -25
  165. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  166. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  167. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  168. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  169. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  170. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  171. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  172. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  173. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  174. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  175. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  176. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  177. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  178. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  179. package/dist/types/data-structures/graph/index.d.ts +0 -3
  180. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  181. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  182. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  183. package/dist/types/data-structures/hash/index.d.ts +0 -1
  184. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  185. package/dist/types/data-structures/heap/index.d.ts +0 -3
  186. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  187. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  188. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  189. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  190. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  191. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  192. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  193. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  194. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  195. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  196. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  197. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  198. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  199. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  200. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  201. package/dist/types/data-structures/queue/index.d.ts +0 -1
  202. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  203. package/dist/types/data-structures/stack/index.d.ts +0 -1
  204. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  205. package/dist/types/data-structures/trampoline.d.ts +0 -25
  206. package/dist/types/data-structures/trie/index.d.ts +0 -1
  207. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  208. package/dist/types/types/utils.d.ts +0 -46
  209. package/dist/types/utils.d.ts +0 -46
  210. package/dist/utils.d.ts +0 -122
  211. package/dist/utils.js +0 -569
  212. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  213. package/src/data-structures/binary-tree/avl-tree.ts +0 -232
  214. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  215. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  216. package/src/data-structures/binary-tree/binary-tree.ts +0 -1088
  217. package/src/data-structures/binary-tree/bst.ts +0 -404
  218. package/src/data-structures/binary-tree/index.ts +0 -11
  219. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  220. package/src/data-structures/binary-tree/segment-tree.ts +0 -164
  221. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  222. package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
  223. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  224. package/src/data-structures/graph/abstract-graph.ts +0 -789
  225. package/src/data-structures/graph/directed-graph.ts +0 -322
  226. package/src/data-structures/graph/index.ts +0 -3
  227. package/src/data-structures/graph/undirected-graph.ts +0 -154
  228. package/src/data-structures/hash/coordinate-map.ts +0 -24
  229. package/src/data-structures/hash/coordinate-set.ts +0 -20
  230. package/src/data-structures/hash/hash-table.ts +0 -1
  231. package/src/data-structures/hash/index.ts +0 -1
  232. package/src/data-structures/heap/heap.ts +0 -136
  233. package/src/data-structures/heap/index.ts +0 -3
  234. package/src/data-structures/heap/max-heap.ts +0 -22
  235. package/src/data-structures/heap/min-heap.ts +0 -24
  236. package/src/data-structures/index.ts +0 -11
  237. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
  238. package/src/data-structures/linked-list/index.ts +0 -2
  239. package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
  240. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  241. package/src/data-structures/matrix/index.ts +0 -4
  242. package/src/data-structures/matrix/matrix.ts +0 -13
  243. package/src/data-structures/matrix/matrix2d.ts +0 -125
  244. package/src/data-structures/matrix/navigator.ts +0 -99
  245. package/src/data-structures/matrix/vector2d.ts +0 -189
  246. package/src/data-structures/priority-queue/index.ts +0 -3
  247. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -12
  248. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
  249. package/src/data-structures/priority-queue/priority-queue.ts +0 -208
  250. package/src/data-structures/queue/deque.ts +0 -139
  251. package/src/data-structures/queue/index.ts +0 -2
  252. package/src/data-structures/queue/queue.ts +0 -123
  253. package/src/data-structures/stack/index.ts +0 -1
  254. package/src/data-structures/stack/stack.ts +0 -104
  255. package/src/data-structures/trampoline.ts +0 -91
  256. package/src/data-structures/trie/index.ts +0 -1
  257. package/src/data-structures/trie/trie.ts +0 -153
  258. package/src/index.ts +0 -1
  259. package/src/types/index.ts +0 -1
  260. package/src/types/patches/index.d.ts +0 -0
  261. package/src/types/utils.ts +0 -158
  262. package/src/utils.ts +0 -605
  263. package/tsconfig.json +0 -53
  264. /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
  265. /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
  266. /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
  267. /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
  268. /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
  269. /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
  270. /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
  271. /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
  272. /package/dist/{types/types → utils}/index.d.ts +0 -0
@@ -2,659 +2,440 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
4
4
  /**
5
- * The class which represents one link or node in a linked list
6
- * ```ts
7
- * const node = new SinglyLinkedListNode(1, null, null, null);
8
- * ```
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
9
10
  */
10
11
  class SinglyLinkedListNode {
11
- constructor(
12
- /** Data stored on the node */
13
- val,
14
- /** The previous node in the list */
15
- prev,
16
- /** The next link in the list */
17
- next,
18
- /** The list this node belongs to */
19
- list) {
20
- this.val = val;
21
- this.prev = prev;
22
- this.next = next;
23
- this.list = list;
24
- }
25
12
  /**
26
- * Alias to .val
27
- * ```ts
28
- * new LinkedList(1, 2, 3).head.value; // 1
29
- * ```
13
+ * The constructor function initializes an instance of a class with a given value and sets the next property to null.
14
+ * @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
15
+ * will be stored in the node of a linked list.
30
16
  */
31
- get value() {
32
- return this.val;
17
+ constructor(val) {
18
+ this._val = val;
19
+ this._next = null;
33
20
  }
34
- /**
35
- * Get the index of this node
36
- * ```ts
37
- * new LinkedList(1, 2, 3).head.index; // 0
38
- * ```
39
- */
40
- get index() {
41
- if (!this.list) {
42
- return undefined;
43
- }
44
- return this.list.findIndex((value) => value === this.value);
21
+ get val() {
22
+ return this._val;
45
23
  }
46
- /**
47
- * Insert a new node before this one
48
- * ```ts
49
- * new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3
50
- * ```
51
- * @param val Data to save in the node
52
- */
53
- insertBefore(val) {
54
- return this.list !== null
55
- ? this.list.insertBefore(this, val)
56
- : new SinglyLinkedList(val, this.val);
24
+ set val(value) {
25
+ this._val = value;
57
26
  }
58
- /**
59
- * Insert new val after this node
60
- * ```ts
61
- * new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3
62
- * ```
63
- * @param val Data to be saved in the node
64
- */
65
- insertAfter(val) {
66
- return this.list !== null
67
- ? this.list.insertAfter(this, val)
68
- : new SinglyLinkedList(this.val, val);
27
+ get next() {
28
+ return this._next;
69
29
  }
70
- /**
71
- * Remove this node
72
- * ```ts
73
- * new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
74
- * ```
75
- */
76
- remove() {
77
- if (this.list === null) {
78
- throw new ReferenceError('Node does not belong to any list');
79
- }
80
- return this.list.removeNode(this);
30
+ set next(value) {
31
+ this._next = value;
81
32
  }
82
33
  }
83
34
  exports.SinglyLinkedListNode = SinglyLinkedListNode;
84
- /**
85
- * A doubly linked list
86
- * ```ts
87
- * const list = new LinkedList(1, 2, 3);
88
- * const listFromArray = LinkedList.from([1, 2, 3]);
89
- * ```
90
- */
91
35
  class SinglyLinkedList {
92
36
  /**
93
- * The length of the list
37
+ * The constructor initializes the linked list with an empty head, tail, and length.
94
38
  */
95
- get length() {
96
- return this.size;
39
+ constructor() {
40
+ this._head = null;
41
+ this._tail = null;
42
+ this._length = 0;
97
43
  }
98
- /**
99
- * Convert any iterable to a new linked list
100
- * ```javascript
101
- * const array = [1, 2, 3];
102
- * const list = LinkedList.from(array);
103
- * ```
104
- * @param iterable Any iterable datatype like Array or Map
105
- */
106
- static from(iterable) {
107
- return new SinglyLinkedList(...iterable);
108
- }
109
- constructor(...args) {
110
- this.head = null;
111
- this.tail = null;
112
- this.size = 0;
113
- for (let i = 0; i < arguments.length; i++) {
114
- this.append(args[i]);
115
- }
44
+ get head() {
45
+ return this._head;
116
46
  }
117
- /**
118
- * Get the node val at a specified index, zero based
119
- * ```ts
120
- * new LinkedList(1, 2, 3).get(0); // 1
121
- * ```
122
- * @param index to retrieve val at
123
- */
124
- get(index) {
125
- const node = this.getNode(index);
126
- return node !== undefined ? node.val : undefined;
47
+ set head(value) {
48
+ this._head = value;
127
49
  }
128
- /**
129
- * Get the node at index, zero based
130
- * ```ts
131
- * new LinkedList(1, 2, 3).getNode(0);
132
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
133
- * ```
134
- */
135
- getNode(index) {
136
- if (this.head === null || index < 0 || index >= this.length) {
137
- return undefined;
138
- }
139
- const asc = index < this.length / 2;
140
- const stopAt = asc ? index : this.length - index - 1;
141
- const nextNode = asc ? 'next' : 'prev';
142
- let currentNode = asc ? this.head : this.tail;
143
- // TODO after no-non-null-assertion not ensure the logic
144
- for (let currentIndex = 0; currentIndex < stopAt; currentIndex++) {
145
- if (currentNode) {
146
- currentNode = currentNode[nextNode];
147
- }
148
- }
149
- return currentNode || undefined;
50
+ get tail() {
51
+ return this._tail;
150
52
  }
151
- /**
152
- * Return the first node and its index in the list that
153
- * satisfies the testing function
154
- * ```ts
155
- * new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
156
- * // { node: SinglyLinkedListNode, index: 0 }
157
- * ```
158
- * @param f A function to be applied to the val of each node
159
- */
160
- findNodeIndex(f) {
161
- let currentIndex = 0;
162
- let currentNode = this.head;
163
- while (currentNode) {
164
- if (f(currentNode.val, currentIndex, this)) {
165
- return {
166
- index: currentIndex,
167
- node: currentNode,
168
- };
169
- }
170
- currentNode = currentNode.next;
171
- currentIndex += 1;
172
- }
173
- return undefined;
53
+ set tail(value) {
54
+ this._tail = value;
174
55
  }
175
- /**
176
- * Returns the first node in the list that
177
- * satisfies the provided testing function. Otherwise undefined is returned.
178
- * ```ts
179
- * new LinkedList(1, 2, 3).findNode(val => val === 1);
180
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
181
- * ```
182
- * @param f Function to test val against
183
- */
184
- findNode(f) {
185
- const nodeIndex = this.findNodeIndex(f);
186
- return nodeIndex !== undefined ? nodeIndex.node : undefined;
56
+ get length() {
57
+ return this._length;
187
58
  }
188
59
  /**
189
- * Returns the value of the first element in the list that
190
- * satisfies the provided testing function. Otherwise undefined is returned.
191
- * ```ts
192
- * new LinkedList(1, 2, 3).find(val => val === 1); // 1
193
- * ```
194
- * @param f Function to test val against
60
+ * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
61
+ * array.
62
+ * @param {T[]} data - The `data` parameter is an array of elements of type `T`.
63
+ * @returns The `fromArray` function returns a `SinglyLinkedList` object.
195
64
  */
196
- find(f) {
197
- const nodeIndex = this.findNodeIndex(f);
198
- return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
65
+ static fromArray(data) {
66
+ const singlyLinkedList = new SinglyLinkedList();
67
+ for (const item of data) {
68
+ singlyLinkedList.push(item);
69
+ }
70
+ return singlyLinkedList;
199
71
  }
200
- /**
201
- * Returns the index of the first node in the list that
202
- * satisfies the provided testing function. Ohterwise -1 is returned.
203
- * ```ts
204
- * new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
205
- * ```
206
- * @param f Function to test val against
207
- */
208
- findIndex(f) {
209
- const nodeIndex = this.findNodeIndex(f);
210
- return nodeIndex !== undefined ? nodeIndex.index : -1;
72
+ getLength() {
73
+ return this._length;
211
74
  }
212
75
  /**
213
- * Append one or any number of nodes to the end of the list.
214
- * This modifies the list in place and returns the list itself
215
- * to make this method chainable.
216
- * ```ts
217
- * new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
218
- * ```
219
- * @param args Data to be stored in the node, takes any number of arguments
76
+ * The `push` function adds a new node with the given data to the end of a singly linked list.
77
+ * @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
78
+ * any type (T) as specified in the generic type declaration of the class or function.
220
79
  */
221
- append(...args) {
222
- for (const val of args) {
223
- const node = new SinglyLinkedListNode(val, this.tail, null, this);
224
- if (this.head === null) {
225
- this.head = node;
226
- }
227
- if (this.tail !== null) {
228
- this.tail.next = node;
229
- }
230
- this.tail = node;
231
- this.size += 1;
80
+ push(data) {
81
+ const newNode = new SinglyLinkedListNode(data);
82
+ if (!this.head) {
83
+ this.head = newNode;
84
+ this.tail = newNode;
232
85
  }
233
- return this;
234
- }
235
- /**
236
- * Synonym for append
237
- * ```ts
238
- * new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4
239
- * ```
240
- * @param args Data to be stored, takes any number of arguments
241
- */
242
- push(...args) {
243
- this.append(...args);
244
- return this.length;
86
+ else {
87
+ this.tail.next = newNode;
88
+ this.tail = newNode;
89
+ }
90
+ this._length++;
245
91
  }
246
92
  /**
247
- * Prepend any number of val arguments to the list. The
248
- * argument list is prepended as a block to reduce confusion:
249
- * ```javascript
250
- * new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]
251
- * ```
252
- * @param args Data to be stored in the node, accepts any number of arguments
93
+ * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
94
+ * pointers accordingly.
95
+ * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
96
+ * the linked list is empty, it returns `null`.
253
97
  */
254
- prepend(...args) {
255
- const reverseArgs = Array.from(args).reverse();
256
- for (const val of reverseArgs) {
257
- const node = new SinglyLinkedListNode(val, null, this.head, this);
258
- if (this.tail === null) {
259
- this.tail = node;
260
- }
261
- if (this.head !== null) {
262
- this.head.prev = node;
263
- }
264
- this.head = node;
265
- this.size += 1;
98
+ pop() {
99
+ if (!this.head)
100
+ return undefined;
101
+ if (this.head === this.tail) {
102
+ const val = this.head.val;
103
+ this.head = null;
104
+ this.tail = null;
105
+ this._length--;
106
+ return val;
107
+ }
108
+ let current = this.head;
109
+ while (current.next !== this.tail) {
110
+ current = current.next;
266
111
  }
267
- return this;
112
+ const val = this.tail.val;
113
+ current.next = null;
114
+ this.tail = current;
115
+ this._length--;
116
+ return val;
268
117
  }
269
118
  /**
270
- * Insert a new node at a given index position. If index is
271
- * out of bounds, the node is appended, if index is negative
272
- * or 0, it will be prepended.
273
- * ```ts
274
- * new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
275
- * ```
276
- * @param index The index to insert the new node at
277
- * @param val Data to be stored on the new node
119
+ * The `shift()` function removes and returns the value of the first node in a linked list.
120
+ * @returns The value of the node that is being removed from the beginning of the linked list.
278
121
  */
279
- insertAt(index, val) {
280
- if (this.head === null) {
281
- return this.append(val);
282
- }
283
- if (index <= 0) {
284
- return this.prepend(val);
285
- }
286
- let currentNode = this.head;
287
- let currentIndex = 0;
288
- while (currentIndex < index - 1 && currentNode.next !== null) {
289
- currentIndex += 1;
290
- currentNode = currentNode.next;
291
- }
292
- currentNode.insertAfter(val);
293
- return this;
122
+ shift() {
123
+ if (!this.head)
124
+ return undefined;
125
+ const removedNode = this.head;
126
+ this.head = this.head.next;
127
+ this._length--;
128
+ return removedNode.val;
294
129
  }
295
130
  /**
296
- * Remove the specified node from the list and return the removed
297
- * node afterwards.
298
- * ```ts
299
- * const list = new LinkedList(1, 2, 3);
300
- * list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
301
- * ```
302
- * @param node The node to be removed
131
+ * The unshift function adds a new node with the given value to the beginning of a singly linked list.
132
+ * @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
133
+ * linked list.
303
134
  */
304
- removeNode(node) {
305
- if (node.list !== this) {
306
- throw new ReferenceError('Node does not belong to this list');
307
- }
308
- if (node.prev !== null) {
309
- node.prev.next = node.next;
135
+ unshift(val) {
136
+ const newNode = new SinglyLinkedListNode(val);
137
+ if (!this.head) {
138
+ this.head = newNode;
139
+ this.tail = newNode;
310
140
  }
311
- if (node.next !== null) {
312
- node.next.prev = node.prev;
313
- }
314
- if (this.head === node) {
315
- this.head = node.next;
316
- }
317
- if (this.tail === node) {
318
- this.tail = node.prev;
141
+ else {
142
+ newNode.next = this.head;
143
+ this.head = newNode;
319
144
  }
320
- this.size -= 1;
321
- node.next = null;
322
- node.prev = null;
323
- node.list = null;
324
- return node;
145
+ this._length++;
325
146
  }
326
147
  /**
327
- * Remove the node at the specified index
328
- * ```ts
329
- * new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }
330
- * ```
331
- * @param index Index at which to remove
148
+ * The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
149
+ * @param {number} index - The index parameter is a number that represents the position of the element we want to
150
+ * retrieve from the list.
151
+ * @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
152
+ * `null` if the index is out of bounds.
332
153
  */
333
- removeAt(index) {
334
- const node = this.getNode(index);
335
- return node !== undefined ? this.removeNode(node) : undefined;
154
+ getAt(index) {
155
+ if (index < 0 || index >= this.length)
156
+ return null;
157
+ let current = this.head;
158
+ for (let i = 0; i < index; i++) {
159
+ current = current.next;
160
+ }
161
+ return current.val;
336
162
  }
337
163
  /**
338
- * Insert a new node before the reference node
339
- * ```ts
340
- * const list = new LinkedList(1, 3);
341
- * list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3
342
- * ```
343
- * @param referenceNode The node reference
344
- * @param val Data to save in the node
164
+ * The function `getNodeAt` returns the node at a given index in a singly linked list.
165
+ * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
166
+ * retrieve from the linked list. It indicates the zero-based index of the node we want to access.
167
+ * @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
168
+ * specified index exists, or `null` if the index is out of bounds.
345
169
  */
346
- insertBefore(referenceNode, val) {
347
- const node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
348
- if (referenceNode.prev === null) {
349
- this.head = node;
350
- }
351
- if (referenceNode.prev !== null) {
352
- referenceNode.prev.next = node;
170
+ getNodeAt(index) {
171
+ let current = this.head;
172
+ for (let i = 0; i < index; i++) {
173
+ current = current.next;
353
174
  }
354
- referenceNode.prev = node;
355
- this.size += 1;
356
- return this;
175
+ return current;
357
176
  }
358
177
  /**
359
- * Sorts the linked list using the provided compare function
360
- * @param compare A function used to compare the val of two nodes. It should return
361
- * a boolean. True will insert a before b, false will insert b before a.
362
- * (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1,
363
- * the sort order will be ascending.
178
+ * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
179
+ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
180
+ * data structure. It is of type number.
181
+ * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
182
+ * bounds.
364
183
  */
365
- sort(compare) {
366
- if (this.head === null || this.tail === null) {
367
- return this;
368
- }
369
- if (this.length < 2) {
370
- return this;
184
+ deleteAt(index) {
185
+ if (index < 0 || index >= this.length)
186
+ return undefined;
187
+ if (index === 0)
188
+ return this.shift();
189
+ if (index === this.length - 1)
190
+ return this.pop();
191
+ const prevNode = this.getNodeAt(index - 1);
192
+ const removedNode = prevNode.next;
193
+ prevNode.next = removedNode.next;
194
+ this._length--;
195
+ return removedNode.val;
196
+ }
197
+ /**
198
+ * The delete function removes a node with a specific value from a singly linked list.
199
+ * @param {T | SinglyLinkedListNode<T>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `T`
200
+ * or a `SinglyLinkedListNode<T>` object.
201
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
202
+ * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
203
+ */
204
+ delete(valueOrNode) {
205
+ let value;
206
+ if (valueOrNode instanceof SinglyLinkedListNode) {
207
+ value = valueOrNode.val;
371
208
  }
372
- const quicksort = (start, end) => {
373
- if (start === end) {
374
- return;
375
- }
376
- const pivotData = end.val;
377
- let current = start;
378
- let split = start;
379
- while (current && current !== end) {
380
- const sort = compare(current.val, pivotData);
381
- if (sort) {
382
- if (current !== split) {
383
- const temp = split.val;
384
- split.val = current.val;
385
- current.val = temp;
209
+ else {
210
+ value = valueOrNode;
211
+ }
212
+ let current = this.head, prev = null;
213
+ while (current) {
214
+ if (current.val === value) {
215
+ if (prev === null) {
216
+ this.head = current.next;
217
+ if (current === this.tail) {
218
+ this.tail = null;
386
219
  }
387
- // TODO after no-non-null-assertion not ensure the logic
388
- if (split.next) {
389
- split = split.next;
220
+ }
221
+ else {
222
+ prev.next = current.next;
223
+ if (current === this.tail) {
224
+ this.tail = prev;
390
225
  }
391
226
  }
392
- current = current.next;
393
- }
394
- end.val = split.val;
395
- split.val = pivotData;
396
- if (start.next === end.prev) {
397
- return;
398
- }
399
- if (split.prev && split !== start) {
400
- quicksort(start, split.prev);
401
- }
402
- if (split.next && split !== end) {
403
- quicksort(split.next, end);
227
+ this._length--;
228
+ return true;
404
229
  }
405
- };
406
- quicksort(this.head, this.tail);
407
- return this;
230
+ prev = current;
231
+ current = current.next;
232
+ }
233
+ return false;
408
234
  }
409
235
  /**
410
- * Insert a new node after this one
411
- * ```ts
412
- * const list = new LinkedList(2, 3);
413
- * list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3
414
- * ```
415
- * @param referenceNode The reference node
416
- * @param val Data to be saved in the node
236
+ * The `insertAt` function inserts a value at a specified index in a singly linked list.
237
+ * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
238
+ * linked list. It is of type number.
239
+ * @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
240
+ * specified index.
241
+ * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
242
+ * if the index is out of bounds.
417
243
  */
418
- insertAfter(referenceNode, val) {
419
- const node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
420
- if (referenceNode.next === null) {
421
- this.tail = node;
244
+ insertAt(index, val) {
245
+ if (index < 0 || index > this.length)
246
+ return false;
247
+ if (index === 0) {
248
+ this.unshift(val);
249
+ return true;
422
250
  }
423
- if (referenceNode.next !== null) {
424
- referenceNode.next.prev = node;
251
+ if (index === this.length) {
252
+ this.push(val);
253
+ return true;
425
254
  }
426
- referenceNode.next = node;
427
- this.size += 1;
428
- return this;
255
+ const newNode = new SinglyLinkedListNode(val);
256
+ const prevNode = this.getNodeAt(index - 1);
257
+ newNode.next = prevNode.next;
258
+ prevNode.next = newNode;
259
+ this._length++;
260
+ return true;
429
261
  }
430
262
  /**
431
- * Remove the first node from the list and return the val of the removed node
432
- * or undefined
433
- * ```ts
434
- * new LinkedList(1, 2, 3).shift(); // 1
435
- * ```
263
+ * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
264
+ * whether it is empty or not.
265
+ * @returns A boolean value indicating whether the length of the object is equal to 0.
436
266
  */
437
- shift() {
438
- return this.removeFromAnyEnd(this.head);
267
+ isEmpty() {
268
+ return this.length === 0;
439
269
  }
440
270
  /**
441
- * Remove the last node from the list and return the val of the removed node
442
- * or undefined if the list was empty
443
- * ```ts
444
- * new LinkedList(1, 2, 3).pop(); // 3
445
- * ```
446
- */
447
- pop() {
448
- return this.removeFromAnyEnd(this.tail);
449
- }
450
- /**
451
- * Merge the current list with another. Both lists will be
452
- * equal after merging.
453
- * ```ts
454
- * const list = new LinkedList(1, 2);
455
- * const otherList = new LinkedList(3);
456
- * list.merge(otherList);
457
- * (list === otherList); // true
458
- * ```
459
- * @param list The list to be merged
460
- */
461
- merge(list) {
462
- if (this.tail !== null) {
463
- this.tail.next = list.head;
464
- }
465
- if (list.head !== null) {
466
- list.head.prev = this.tail;
467
- }
468
- this.head = this.head || list.head;
469
- this.tail = list.tail || this.tail;
470
- this.size += list.size;
471
- list.size = this.size;
472
- list.head = this.head;
473
- list.tail = this.tail;
474
- }
475
- /**
476
- * Removes all nodes from a list
477
- *
478
- * ```ts
479
- * list.clear();
480
- * ```
271
+ * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
481
272
  */
482
273
  clear() {
483
- this.head = null;
484
- this.tail = null;
485
- this.size = 0;
486
- return this;
274
+ this._head = null;
275
+ this._tail = null;
276
+ this._length = 0;
487
277
  }
488
278
  /**
489
- * The slice() method returns a shallow copy of a
490
- * portion of a list into a new list object selected
491
- * from start to end (end not included).
492
- * The original list will not be modified.
493
- * ```ts
494
- * const list = new LinkedList(1, 2, 3, 4, 5);
495
- * const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
496
- * ```
497
- * @param start Start index
498
- * @param end End index, optional
279
+ * The `toArray` function converts a linked list into an array.
280
+ * @returns The `toArray()` method is returning an array of type `T[]`.
499
281
  */
500
- // eslint-disable-next-line @typescript-eslint/ban-types
501
- slice(start, end) {
502
- const list = new SinglyLinkedList();
503
- let finish = end;
504
- if (this.head === null || this.tail === null) {
505
- return list;
506
- }
507
- if (finish === undefined || finish < start) {
508
- finish = this.length;
509
- }
510
- let head = this.getNode(start);
511
- for (let i = 0; i < finish - start && head !== null && head !== undefined; i++) {
512
- list.append(head.val);
513
- head = head.next;
282
+ toArray() {
283
+ const array = [];
284
+ let current = this.head;
285
+ while (current) {
286
+ array.push(current.val);
287
+ current = current.next;
514
288
  }
515
- return list;
289
+ return array;
516
290
  }
517
291
  /**
518
- * The reverse() function reverses the list in place and returns the list
519
- * itself.
520
- * ```ts
521
- * new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
522
- * ```
292
+ * The `reverse` function reverses the order of the nodes in a singly linked list.
293
+ * @returns The reverse() method does not return anything. It has a return type of void.
523
294
  */
524
295
  reverse() {
525
- let currentNode = this.head;
526
- while (currentNode) {
527
- const next = currentNode.next;
528
- currentNode.next = currentNode.prev;
529
- currentNode.prev = next;
530
- currentNode = currentNode.prev;
296
+ if (!this.head || this.head === this.tail)
297
+ return;
298
+ let prev = null;
299
+ let current = this.head;
300
+ let next = null;
301
+ while (current) {
302
+ next = current.next;
303
+ current.next = prev;
304
+ prev = current;
305
+ current = next;
306
+ }
307
+ [this.head, this.tail] = [this.tail, this.head];
308
+ }
309
+ /**
310
+ * The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
311
+ * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
312
+ * function is used to determine whether a particular value in the linked list satisfies a certain condition.
313
+ * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
314
+ * the callback function. If no element satisfies the condition, it returns `null`.
315
+ */
316
+ find(callback) {
317
+ let current = this.head;
318
+ while (current) {
319
+ if (callback(current.val)) {
320
+ return current.val;
321
+ }
322
+ current = current.next;
531
323
  }
532
- const tail = this.tail;
533
- this.tail = this.head;
534
- this.head = tail;
535
- return this;
324
+ return null;
536
325
  }
537
326
  /**
538
- * The forEach() method executes a provided function once for each list node.
539
- * ```ts
540
- * new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
541
- * ```
542
- * @param f Function to execute for each element, taking up to three arguments.
543
- * @param reverse Indicates if the list should be walked in reverse order, default is false
327
+ * The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
328
+ * @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
329
+ * @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
330
+ * value is not found, it returns -1.
544
331
  */
545
- forEach(f, reverse = false) {
546
- let currentIndex = reverse ? this.length - 1 : 0;
547
- let currentNode = reverse ? this.tail : this.head;
548
- const modifier = reverse ? -1 : 1;
549
- const nextNode = reverse ? 'prev' : 'next';
550
- while (currentNode) {
551
- f(currentNode.val, currentIndex, this);
552
- currentNode = currentNode[nextNode];
553
- currentIndex += modifier;
332
+ indexOf(value) {
333
+ let index = 0;
334
+ let current = this.head;
335
+ while (current) {
336
+ if (current.val === value) {
337
+ return index;
338
+ }
339
+ index++;
340
+ current = current.next;
554
341
  }
342
+ return -1;
555
343
  }
556
344
  /**
557
- * The map() method creates a new list with the results of
558
- * calling a provided function on every node in the calling list.
559
- * ```ts
560
- * new LinkedList(1, 2, 3).map(val => val + 10); // 11 <=> 12 <=> 13
561
- * ```
562
- * @param f Function that produces an node of the new list, taking up to three arguments
563
- * @param reverse Indicates if the list should be mapped in reverse order, default is false
564
- */
565
- // eslint-disable-next-line @typescript-eslint/ban-types
566
- map(f, reverse = false) {
567
- const list = new SinglyLinkedList();
568
- this.forEach((val, index) => list.append(f(val, index, this)), reverse);
569
- return list;
570
- }
571
- /**
572
- * The filter() method creates a new list with all nodes
573
- * that pass the test implemented by the provided function.
574
- * ```ts
575
- * new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
576
- * ```
577
- * @param f Function to test each node val in the list. Return true to keep the node
578
- * @param reverse Indicates if the list should be filtered in reverse order, default is false
345
+ * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
346
+ * null.
347
+ * @param {T} value - The value parameter is the value that we want to search for in the linked list.
348
+ * @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
349
+ * the specified value is found, the function returns `null`.
579
350
  */
580
- // eslint-disable-next-line @typescript-eslint/ban-types
581
- filter(f, reverse = false) {
582
- const list = new SinglyLinkedList();
583
- this.forEach((val, index) => {
584
- if (f(val, index, this)) {
585
- list.append(val);
351
+ findNode(value) {
352
+ let current = this.head;
353
+ while (current) {
354
+ if (current.val === value) {
355
+ return current;
586
356
  }
587
- }, reverse);
588
- return list;
357
+ current = current.next;
358
+ }
359
+ return null;
589
360
  }
590
361
  /**
591
- * Reduce over each node in the list
592
- * ```ts
593
- * new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
594
- * ```
595
- * @param f A reducer function
596
- * @param start An initial value
597
- * @returns The final state of the accumulator
362
+ * The `insertBefore` function inserts a new value before an existing value in a singly linked list.
363
+ * @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node that you want to insert the
364
+ * new value before. It can be either the value itself or a node containing the value in the linked list.
365
+ * @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
366
+ * @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
367
+ * inserted before the existing value, and `false` otherwise.
598
368
  */
599
- reduce(f, start, reverse = false) {
600
- let currentIndex = reverse ? this.length - 1 : 0;
601
- const modifier = reverse ? -1 : 1;
602
- const nextNode = reverse ? 'prev' : 'next';
603
- let currentElement = reverse ? this.tail : this.head;
604
- let result;
605
- if (start !== undefined) {
606
- result = start;
607
- }
608
- else if (currentElement) {
609
- result = currentElement.val;
610
- currentElement = currentElement[nextNode];
369
+ insertBefore(existingValueOrNode, newValue) {
370
+ if (!this.head)
371
+ return false;
372
+ let existingValue;
373
+ if (existingValueOrNode instanceof SinglyLinkedListNode) {
374
+ existingValue = existingValueOrNode.val;
611
375
  }
612
376
  else {
613
- throw new TypeError('Reduce of empty LinkedList with no initial value');
614
- }
615
- while (currentElement) {
616
- result = f(result, currentElement.val, currentIndex, this);
617
- currentIndex += modifier;
618
- currentElement = currentElement[nextNode];
377
+ existingValue = existingValueOrNode;
378
+ }
379
+ if (this.head.val === existingValue) {
380
+ this.unshift(newValue);
381
+ return true;
382
+ }
383
+ let current = this.head;
384
+ while (current.next) {
385
+ if (current.next.val === existingValue) {
386
+ const newNode = new SinglyLinkedListNode(newValue);
387
+ newNode.next = current.next;
388
+ current.next = newNode;
389
+ this._length++;
390
+ return true;
391
+ }
392
+ current = current.next;
619
393
  }
620
- return result;
621
- }
622
- /**
623
- * Convert the linked list to an array
624
- * ```ts
625
- * new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
626
- * ```
627
- */
628
- toArray() {
629
- return [...this];
394
+ return false;
630
395
  }
631
396
  /**
632
- * Convert a linked list to string
633
- * ```ts
634
- * new LinkedList('one', 'two', 'three').toString(' <=> ') === 'one <=> two <=> three';
635
- * ```
636
- * @param separator Optional string to be placed in between val nodes, default is one space
397
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
398
+ * @param {T | SinglyLinkedListNode<T>} existingValueOrNode - The existing value or node in the linked list after which
399
+ * the new value will be inserted. It can be either the value of the existing node or the existing node itself.
400
+ * @param {T} newValue - The value that you want to insert into the linked list after the existing value or node.
401
+ * @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
402
+ * existing value or node, and false if the existing value or node was not found in the linked list.
637
403
  */
638
- toString(separator = ' ') {
639
- return this.reduce((s, val) => `${s}${separator}${val}`);
404
+ insertAfter(existingValueOrNode, newValue) {
405
+ let existingNode;
406
+ if (existingValueOrNode instanceof SinglyLinkedListNode) {
407
+ existingNode = existingValueOrNode;
408
+ }
409
+ else {
410
+ existingNode = this.findNode(existingValueOrNode);
411
+ }
412
+ if (existingNode) {
413
+ const newNode = new SinglyLinkedListNode(newValue);
414
+ newNode.next = existingNode.next;
415
+ existingNode.next = newNode;
416
+ if (existingNode === this.tail) {
417
+ this.tail = newNode;
418
+ }
419
+ this._length++;
420
+ return true;
421
+ }
422
+ return false;
640
423
  }
641
424
  /**
642
- * The iterator implementation
643
- * ```ts
644
- * const list = new LinkedList(1, 2, 3);
645
- * for (const val of list) { log(val); } // 1 2 3
646
- * ```
425
+ * The function counts the number of occurrences of a given value in a linked list.
426
+ * @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
427
+ * @returns The count of occurrences of the given value in the linked list.
647
428
  */
648
- *[Symbol.iterator]() {
649
- let element = this.head;
650
- while (element !== null) {
651
- yield element.val;
652
- element = element.next;
429
+ countOccurrences(value) {
430
+ let count = 0;
431
+ let current = this.head;
432
+ while (current) {
433
+ if (current.val === value) {
434
+ count++;
435
+ }
436
+ current = current.next;
653
437
  }
654
- }
655
- /** Private helper function to reduce duplication of pop() and shift() methods */
656
- removeFromAnyEnd(node) {
657
- return node !== null ? this.removeNode(node).val : undefined;
438
+ return count;
658
439
  }
659
440
  }
660
441
  exports.SinglyLinkedList = SinglyLinkedList;