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,794 +1,441 @@
1
1
  "use strict";
2
- var __generator = (this && this.__generator) || function (thisArg, body) {
3
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
5
- function verb(n) { return function (v) { return step([n, v]); }; }
6
- function step(op) {
7
- if (f) throw new TypeError("Generator is already executing.");
8
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
9
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
10
- if (y = 0, t) op = [op[0] & 2, t.value];
11
- switch (op[0]) {
12
- case 0: case 1: t = op; break;
13
- case 4: _.label++; return { value: op[1], done: false };
14
- case 5: _.label++; y = op[1]; op = [0]; continue;
15
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
16
- default:
17
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
18
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
19
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
20
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
21
- if (t[2]) _.ops.pop();
22
- _.trys.pop(); continue;
23
- }
24
- op = body.call(thisArg, _);
25
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
27
- }
28
- };
29
- var __read = (this && this.__read) || function (o, n) {
30
- var m = typeof Symbol === "function" && o[Symbol.iterator];
31
- if (!m) return o;
32
- var i = m.call(o), r, ar = [], e;
33
- try {
34
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
35
- }
36
- catch (error) { e = { error: error }; }
37
- finally {
38
- try {
39
- if (r && !r.done && (m = i["return"])) m.call(i);
40
- }
41
- finally { if (e) throw e.error; }
42
- }
43
- return ar;
44
- };
45
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
46
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
47
- if (ar || !(i in from)) {
48
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
49
- ar[i] = from[i];
50
- }
51
- }
52
- return to.concat(ar || Array.prototype.slice.call(from));
53
- };
54
- var __values = (this && this.__values) || function(o) {
55
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
56
- if (m) return m.call(o);
57
- if (o && typeof o.length === "number") return {
58
- next: function () {
59
- if (o && i >= o.length) o = void 0;
60
- return { value: o && o[i++], done: !o };
61
- }
62
- };
63
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
64
- };
65
2
  Object.defineProperty(exports, "__esModule", { value: true });
66
3
  exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
67
4
  /**
68
- * The class which represents one link or node in a linked list
69
- * ```ts
70
- * const node = new SinglyLinkedListNode(1, null, null, null);
71
- * ```
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
72
10
  */
73
- var SinglyLinkedListNode = /** @class */ (function () {
74
- function SinglyLinkedListNode(
75
- /** Data stored on the node */
76
- val,
77
- /** The previous node in the list */
78
- prev,
79
- /** The next link in the list */
80
- next,
81
- /** The list this node belongs to */
82
- list) {
83
- this.val = val;
84
- this.prev = prev;
85
- this.next = next;
86
- this.list = list;
87
- }
88
- Object.defineProperty(SinglyLinkedListNode.prototype, "value", {
89
- /**
90
- * Alias to .val
91
- * ```ts
92
- * new LinkedList(1, 2, 3).head.value; // 1
93
- * ```
94
- */
95
- get: function () {
96
- return this.val;
97
- },
98
- enumerable: false,
99
- configurable: true
100
- });
101
- Object.defineProperty(SinglyLinkedListNode.prototype, "index", {
102
- /**
103
- * Get the index of this node
104
- * ```ts
105
- * new LinkedList(1, 2, 3).head.index; // 0
106
- * ```
107
- */
108
- get: function () {
109
- var _this = this;
110
- if (!this.list) {
111
- return undefined;
112
- }
113
- return this.list.findIndex(function (value) { return value === _this.value; });
114
- },
115
- enumerable: false,
116
- configurable: true
117
- });
11
+ class SinglyLinkedListNode {
118
12
  /**
119
- * Insert a new node before this one
120
- * ```ts
121
- * new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3
122
- * ```
123
- * @param val Data to save in the node
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.
124
16
  */
125
- SinglyLinkedListNode.prototype.insertBefore = function (val) {
126
- return this.list !== null
127
- ? this.list.insertBefore(this, val)
128
- : new SinglyLinkedList(val, this.val);
129
- };
17
+ constructor(val) {
18
+ this._val = val;
19
+ this._next = null;
20
+ }
21
+ get val() {
22
+ return this._val;
23
+ }
24
+ set val(value) {
25
+ this._val = value;
26
+ }
27
+ get next() {
28
+ return this._next;
29
+ }
30
+ set next(value) {
31
+ this._next = value;
32
+ }
33
+ }
34
+ exports.SinglyLinkedListNode = SinglyLinkedListNode;
35
+ class SinglyLinkedList {
130
36
  /**
131
- * Insert new val after this node
132
- * ```ts
133
- * new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3
134
- * ```
135
- * @param val Data to be saved in the node
37
+ * The constructor initializes the linked list with an empty head, tail, and length.
136
38
  */
137
- SinglyLinkedListNode.prototype.insertAfter = function (val) {
138
- return this.list !== null
139
- ? this.list.insertAfter(this, val)
140
- : new SinglyLinkedList(this.val, val);
141
- };
39
+ constructor() {
40
+ this._head = null;
41
+ this._tail = null;
42
+ this._length = 0;
43
+ }
44
+ get head() {
45
+ return this._head;
46
+ }
47
+ set head(value) {
48
+ this._head = value;
49
+ }
50
+ get tail() {
51
+ return this._tail;
52
+ }
53
+ set tail(value) {
54
+ this._tail = value;
55
+ }
56
+ get length() {
57
+ return this._length;
58
+ }
142
59
  /**
143
- * Remove this node
144
- * ```ts
145
- * new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3
146
- * ```
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.
147
64
  */
148
- SinglyLinkedListNode.prototype.remove = function () {
149
- if (this.list === null) {
150
- throw new ReferenceError('Node does not belong to any list');
151
- }
152
- return this.list.removeNode(this);
153
- };
154
- return SinglyLinkedListNode;
155
- }());
156
- exports.SinglyLinkedListNode = SinglyLinkedListNode;
157
- /**
158
- * A doubly linked list
159
- * ```ts
160
- * const list = new LinkedList(1, 2, 3);
161
- * const listFromArray = LinkedList.from([1, 2, 3]);
162
- * ```
163
- */
164
- var SinglyLinkedList = /** @class */ (function () {
165
- function SinglyLinkedList() {
166
- var args = [];
167
- for (var _i = 0; _i < arguments.length; _i++) {
168
- args[_i] = arguments[_i];
169
- }
170
- this.head = null;
171
- this.tail = null;
172
- this.size = 0;
173
- for (var i = 0; i < arguments.length; i++) {
174
- this.append(args[i]);
65
+ static fromArray(data) {
66
+ const singlyLinkedList = new SinglyLinkedList();
67
+ for (const item of data) {
68
+ singlyLinkedList.push(item);
175
69
  }
70
+ return singlyLinkedList;
71
+ }
72
+ getLength() {
73
+ return this._length;
176
74
  }
177
- Object.defineProperty(SinglyLinkedList.prototype, "length", {
178
- /**
179
- * The length of the list
180
- */
181
- get: function () {
182
- return this.size;
183
- },
184
- enumerable: false,
185
- configurable: true
186
- });
187
- /**
188
- * Convert any iterable to a new linked list
189
- * ```javascript
190
- * const array = [1, 2, 3];
191
- * const list = LinkedList.from(array);
192
- * ```
193
- * @param iterable Any iterable datatype like Array or Map
194
- */
195
- SinglyLinkedList.from = function (iterable) {
196
- return new (SinglyLinkedList.bind.apply(SinglyLinkedList, __spreadArray([void 0], __read(iterable), false)))();
197
- };
198
- /**
199
- * Get the node val at a specified index, zero based
200
- * ```ts
201
- * new LinkedList(1, 2, 3).get(0); // 1
202
- * ```
203
- * @param index to retrieve val at
204
- */
205
- SinglyLinkedList.prototype.get = function (index) {
206
- var node = this.getNode(index);
207
- return node !== undefined ? node.val : undefined;
208
- };
209
75
  /**
210
- * Get the node at index, zero based
211
- * ```ts
212
- * new LinkedList(1, 2, 3).getNode(0);
213
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
214
- * ```
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.
215
79
  */
216
- SinglyLinkedList.prototype.getNode = function (index) {
217
- if (this.head === null || index < 0 || index >= this.length) {
218
- return undefined;
219
- }
220
- var asc = index < this.length / 2;
221
- var stopAt = asc ? index : this.length - index - 1;
222
- var nextNode = asc ? 'next' : 'prev';
223
- var currentNode = asc ? this.head : this.tail;
224
- // TODO after no-non-null-assertion not ensure the logic
225
- for (var currentIndex = 0; currentIndex < stopAt; currentIndex++) {
226
- if (currentNode) {
227
- currentNode = currentNode[nextNode];
228
- }
80
+ push(data) {
81
+ const newNode = new SinglyLinkedListNode(data);
82
+ if (!this.head) {
83
+ this.head = newNode;
84
+ this.tail = newNode;
229
85
  }
230
- return currentNode || undefined;
231
- };
232
- /**
233
- * Return the first node and its index in the list that
234
- * satisfies the testing function
235
- * ```ts
236
- * new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
237
- * // { node: SinglyLinkedListNode, index: 0 }
238
- * ```
239
- * @param f A function to be applied to the val of each node
240
- */
241
- SinglyLinkedList.prototype.findNodeIndex = function (f) {
242
- var currentIndex = 0;
243
- var currentNode = this.head;
244
- while (currentNode) {
245
- if (f(currentNode.val, currentIndex, this)) {
246
- return {
247
- index: currentIndex,
248
- node: currentNode,
249
- };
250
- }
251
- currentNode = currentNode.next;
252
- currentIndex += 1;
86
+ else {
87
+ this.tail.next = newNode;
88
+ this.tail = newNode;
253
89
  }
254
- return undefined;
255
- };
256
- /**
257
- * Returns the first node in the list that
258
- * satisfies the provided testing function. Otherwise undefined is returned.
259
- * ```ts
260
- * new LinkedList(1, 2, 3).findNode(val => val === 1);
261
- * // { prev: null, val: 1, next: SinglyLinkedListNode }
262
- * ```
263
- * @param f Function to test val against
264
- */
265
- SinglyLinkedList.prototype.findNode = function (f) {
266
- var nodeIndex = this.findNodeIndex(f);
267
- return nodeIndex !== undefined ? nodeIndex.node : undefined;
268
- };
269
- /**
270
- * Returns the value of the first element in the list that
271
- * satisfies the provided testing function. Otherwise undefined is returned.
272
- * ```ts
273
- * new LinkedList(1, 2, 3).find(val => val === 1); // 1
274
- * ```
275
- * @param f Function to test val against
276
- */
277
- SinglyLinkedList.prototype.find = function (f) {
278
- var nodeIndex = this.findNodeIndex(f);
279
- return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
280
- };
281
- /**
282
- * Returns the index of the first node in the list that
283
- * satisfies the provided testing function. Ohterwise -1 is returned.
284
- * ```ts
285
- * new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
286
- * ```
287
- * @param f Function to test val against
288
- */
289
- SinglyLinkedList.prototype.findIndex = function (f) {
290
- var nodeIndex = this.findNodeIndex(f);
291
- return nodeIndex !== undefined ? nodeIndex.index : -1;
292
- };
90
+ this._length++;
91
+ }
293
92
  /**
294
- * Append one or any number of nodes to the end of the list.
295
- * This modifies the list in place and returns the list itself
296
- * to make this method chainable.
297
- * ```ts
298
- * new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
299
- * ```
300
- * @param args Data to be stored in the node, takes 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`.
301
97
  */
302
- SinglyLinkedList.prototype.append = function () {
303
- var e_1, _a;
304
- var args = [];
305
- for (var _i = 0; _i < arguments.length; _i++) {
306
- args[_i] = arguments[_i];
307
- }
308
- try {
309
- for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
310
- var val = args_1_1.value;
311
- var node = new SinglyLinkedListNode(val, this.tail, null, this);
312
- if (this.head === null) {
313
- this.head = node;
314
- }
315
- if (this.tail !== null) {
316
- this.tail.next = node;
317
- }
318
- this.tail = node;
319
- this.size += 1;
320
- }
321
- }
322
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
323
- finally {
324
- try {
325
- if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
326
- }
327
- finally { if (e_1) throw e_1.error; }
328
- }
329
- return this;
330
- };
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;
111
+ }
112
+ const val = this.tail.val;
113
+ current.next = null;
114
+ this.tail = current;
115
+ this._length--;
116
+ return val;
117
+ }
331
118
  /**
332
- * Synonym for append
333
- * ```ts
334
- * new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4
335
- * ```
336
- * @param args Data to be stored, takes any number of arguments
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.
337
121
  */
338
- SinglyLinkedList.prototype.push = function () {
339
- var args = [];
340
- for (var _i = 0; _i < arguments.length; _i++) {
341
- args[_i] = arguments[_i];
342
- }
343
- this.append.apply(this, __spreadArray([], __read(args), false));
344
- return this.length;
345
- };
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;
129
+ }
346
130
  /**
347
- * Prepend any number of val arguments to the list. The
348
- * argument list is prepended as a block to reduce confusion:
349
- * ```javascript
350
- * new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]
351
- * ```
352
- * @param args Data to be stored in the node, accepts any number of arguments
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.
353
134
  */
354
- SinglyLinkedList.prototype.prepend = function () {
355
- var e_2, _a;
356
- var args = [];
357
- for (var _i = 0; _i < arguments.length; _i++) {
358
- args[_i] = arguments[_i];
135
+ unshift(val) {
136
+ const newNode = new SinglyLinkedListNode(val);
137
+ if (!this.head) {
138
+ this.head = newNode;
139
+ this.tail = newNode;
359
140
  }
360
- var reverseArgs = Array.from(args).reverse();
361
- try {
362
- for (var reverseArgs_1 = __values(reverseArgs), reverseArgs_1_1 = reverseArgs_1.next(); !reverseArgs_1_1.done; reverseArgs_1_1 = reverseArgs_1.next()) {
363
- var val = reverseArgs_1_1.value;
364
- var node = new SinglyLinkedListNode(val, null, this.head, this);
365
- if (this.tail === null) {
366
- this.tail = node;
367
- }
368
- if (this.head !== null) {
369
- this.head.prev = node;
370
- }
371
- this.head = node;
372
- this.size += 1;
373
- }
374
- }
375
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
376
- finally {
377
- try {
378
- if (reverseArgs_1_1 && !reverseArgs_1_1.done && (_a = reverseArgs_1.return)) _a.call(reverseArgs_1);
379
- }
380
- finally { if (e_2) throw e_2.error; }
141
+ else {
142
+ newNode.next = this.head;
143
+ this.head = newNode;
381
144
  }
382
- return this;
383
- };
145
+ this._length++;
146
+ }
384
147
  /**
385
- * Insert a new node at a given index position. If index is
386
- * out of bounds, the node is appended, if index is negative
387
- * or 0, it will be prepended.
388
- * ```ts
389
- * new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
390
- * ```
391
- * @param index The index to insert the new node at
392
- * @param val Data to be stored on the new node
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.
393
153
  */
394
- SinglyLinkedList.prototype.insertAt = function (index, val) {
395
- if (this.head === null) {
396
- return this.append(val);
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;
397
160
  }
398
- if (index <= 0) {
399
- return this.prepend(val);
400
- }
401
- var currentNode = this.head;
402
- var currentIndex = 0;
403
- while (currentIndex < index - 1 && currentNode.next !== null) {
404
- currentIndex += 1;
405
- currentNode = currentNode.next;
406
- }
407
- currentNode.insertAfter(val);
408
- return this;
409
- };
161
+ return current.val;
162
+ }
410
163
  /**
411
- * Remove the specified node from the list and return the removed
412
- * node afterwards.
413
- * ```ts
414
- * const list = new LinkedList(1, 2, 3);
415
- * list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
416
- * ```
417
- * @param node The node to be removed
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.
418
169
  */
419
- SinglyLinkedList.prototype.removeNode = function (node) {
420
- if (node.list !== this) {
421
- throw new ReferenceError('Node does not belong to this list');
422
- }
423
- if (node.prev !== null) {
424
- node.prev.next = node.next;
170
+ getNodeAt(index) {
171
+ let current = this.head;
172
+ for (let i = 0; i < index; i++) {
173
+ current = current.next;
425
174
  }
426
- if (node.next !== null) {
427
- node.next.prev = node.prev;
428
- }
429
- if (this.head === node) {
430
- this.head = node.next;
431
- }
432
- if (this.tail === node) {
433
- this.tail = node.prev;
434
- }
435
- this.size -= 1;
436
- node.next = null;
437
- node.prev = null;
438
- node.list = null;
439
- return node;
440
- };
441
- /**
442
- * Remove the node at the specified index
443
- * ```ts
444
- * new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }
445
- * ```
446
- * @param index Index at which to remove
447
- */
448
- SinglyLinkedList.prototype.removeAt = function (index) {
449
- var node = this.getNode(index);
450
- return node !== undefined ? this.removeNode(node) : undefined;
451
- };
175
+ return current;
176
+ }
452
177
  /**
453
- * Insert a new node before the reference node
454
- * ```ts
455
- * const list = new LinkedList(1, 3);
456
- * list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3
457
- * ```
458
- * @param referenceNode The node reference
459
- * @param val Data to save in the node
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.
460
183
  */
461
- SinglyLinkedList.prototype.insertBefore = function (referenceNode, val) {
462
- var node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
463
- if (referenceNode.prev === null) {
464
- this.head = node;
465
- }
466
- if (referenceNode.prev !== null) {
467
- referenceNode.prev.next = node;
468
- }
469
- referenceNode.prev = node;
470
- this.size += 1;
471
- return this;
472
- };
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
+ }
473
197
  /**
474
- * Sorts the linked list using the provided compare function
475
- * @param compare A function used to compare the val of two nodes. It should return
476
- * a boolean. True will insert a before b, false will insert b before a.
477
- * (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1,
478
- * the sort order will be ascending.
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.
479
203
  */
480
- SinglyLinkedList.prototype.sort = function (compare) {
481
- if (this.head === null || this.tail === null) {
482
- return this;
483
- }
484
- if (this.length < 2) {
485
- return this;
204
+ delete(valueOrNode) {
205
+ let value;
206
+ if (valueOrNode instanceof SinglyLinkedListNode) {
207
+ value = valueOrNode.val;
486
208
  }
487
- var quicksort = function (start, end) {
488
- if (start === end) {
489
- return;
490
- }
491
- var pivotData = end.val;
492
- var current = start;
493
- var split = start;
494
- while (current && current !== end) {
495
- var sort = compare(current.val, pivotData);
496
- if (sort) {
497
- if (current !== split) {
498
- var temp = split.val;
499
- split.val = current.val;
500
- 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;
501
219
  }
502
- // TODO after no-non-null-assertion not ensure the logic
503
- if (split.next) {
504
- split = split.next;
220
+ }
221
+ else {
222
+ prev.next = current.next;
223
+ if (current === this.tail) {
224
+ this.tail = prev;
505
225
  }
506
226
  }
507
- current = current.next;
508
- }
509
- end.val = split.val;
510
- split.val = pivotData;
511
- if (start.next === end.prev) {
512
- return;
227
+ this._length--;
228
+ return true;
513
229
  }
514
- if (split.prev && split !== start) {
515
- quicksort(start, split.prev);
516
- }
517
- if (split.next && split !== end) {
518
- quicksort(split.next, end);
519
- }
520
- };
521
- quicksort(this.head, this.tail);
522
- return this;
523
- };
524
- /**
525
- * Insert a new node after this one
526
- * ```ts
527
- * const list = new LinkedList(2, 3);
528
- * list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3
529
- * ```
530
- * @param referenceNode The reference node
531
- * @param val Data to be saved in the node
532
- */
533
- SinglyLinkedList.prototype.insertAfter = function (referenceNode, val) {
534
- var node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
535
- if (referenceNode.next === null) {
536
- this.tail = node;
230
+ prev = current;
231
+ current = current.next;
537
232
  }
538
- if (referenceNode.next !== null) {
539
- referenceNode.next.prev = node;
540
- }
541
- referenceNode.next = node;
542
- this.size += 1;
543
- return this;
544
- };
233
+ return false;
234
+ }
235
+ /**
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.
243
+ */
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;
250
+ }
251
+ if (index === this.length) {
252
+ this.push(val);
253
+ return true;
254
+ }
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;
261
+ }
545
262
  /**
546
- * Remove the first node from the list and return the val of the removed node
547
- * or undefined
548
- * ```ts
549
- * new LinkedList(1, 2, 3).shift(); // 1
550
- * ```
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.
551
266
  */
552
- SinglyLinkedList.prototype.shift = function () {
553
- return this.removeFromAnyEnd(this.head);
554
- };
267
+ isEmpty() {
268
+ return this.length === 0;
269
+ }
555
270
  /**
556
- * Remove the last node from the list and return the val of the removed node
557
- * or undefined if the list was empty
558
- * ```ts
559
- * new LinkedList(1, 2, 3).pop(); // 3
560
- * ```
271
+ * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
561
272
  */
562
- SinglyLinkedList.prototype.pop = function () {
563
- return this.removeFromAnyEnd(this.tail);
564
- };
273
+ clear() {
274
+ this._head = null;
275
+ this._tail = null;
276
+ this._length = 0;
277
+ }
565
278
  /**
566
- * Merge the current list with another. Both lists will be
567
- * equal after merging.
568
- * ```ts
569
- * const list = new LinkedList(1, 2);
570
- * const otherList = new LinkedList(3);
571
- * list.merge(otherList);
572
- * (list === otherList); // true
573
- * ```
574
- * @param list The list to be merged
279
+ * The `toArray` function converts a linked list into an array.
280
+ * @returns The `toArray()` method is returning an array of type `T[]`.
575
281
  */
576
- SinglyLinkedList.prototype.merge = function (list) {
577
- if (this.tail !== null) {
578
- this.tail.next = list.head;
579
- }
580
- if (list.head !== null) {
581
- list.head.prev = this.tail;
282
+ toArray() {
283
+ const array = [];
284
+ let current = this.head;
285
+ while (current) {
286
+ array.push(current.val);
287
+ current = current.next;
582
288
  }
583
- this.head = this.head || list.head;
584
- this.tail = list.tail || this.tail;
585
- this.size += list.size;
586
- list.size = this.size;
587
- list.head = this.head;
588
- list.tail = this.tail;
589
- };
289
+ return array;
290
+ }
590
291
  /**
591
- * Removes all nodes from a list
592
- *
593
- * ```ts
594
- * list.clear();
595
- * ```
596
- */
597
- SinglyLinkedList.prototype.clear = function () {
598
- this.head = null;
599
- this.tail = null;
600
- this.size = 0;
601
- return this;
602
- };
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.
294
+ */
295
+ reverse() {
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
+ }
603
309
  /**
604
- * The slice() method returns a shallow copy of a
605
- * portion of a list into a new list object selected
606
- * from start to end (end not included).
607
- * The original list will not be modified.
608
- * ```ts
609
- * const list = new LinkedList(1, 2, 3, 4, 5);
610
- * const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
611
- * ```
612
- * @param start Start index
613
- * @param end End index, optional
614
- */
615
- // eslint-disable-next-line @typescript-eslint/ban-types
616
- SinglyLinkedList.prototype.slice = function (start, end) {
617
- var list = new SinglyLinkedList();
618
- var finish = end;
619
- if (this.head === null || this.tail === null) {
620
- return list;
621
- }
622
- if (finish === undefined || finish < start) {
623
- finish = this.length;
624
- }
625
- var head = this.getNode(start);
626
- for (var i = 0; i < finish - start && head !== null && head !== undefined; i++) {
627
- list.append(head.val);
628
- head = head.next;
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;
629
323
  }
630
- return list;
631
- };
324
+ return null;
325
+ }
632
326
  /**
633
- * The reverse() function reverses the list in place and returns the list
634
- * itself.
635
- * ```ts
636
- * new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
637
- * ```
638
- */
639
- SinglyLinkedList.prototype.reverse = function () {
640
- var currentNode = this.head;
641
- while (currentNode) {
642
- var next = currentNode.next;
643
- currentNode.next = currentNode.prev;
644
- currentNode.prev = next;
645
- currentNode = currentNode.prev;
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.
331
+ */
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;
646
341
  }
647
- var tail = this.tail;
648
- this.tail = this.head;
649
- this.head = tail;
650
- return this;
651
- };
342
+ return -1;
343
+ }
652
344
  /**
653
- * The forEach() method executes a provided function once for each list node.
654
- * ```ts
655
- * new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
656
- * ```
657
- * @param f Function to execute for each element, taking up to three arguments.
658
- * @param reverse Indicates if the list should be walked in reverse order, default is false
659
- */
660
- SinglyLinkedList.prototype.forEach = function (f, reverse) {
661
- if (reverse === void 0) { reverse = false; }
662
- var currentIndex = reverse ? this.length - 1 : 0;
663
- var currentNode = reverse ? this.tail : this.head;
664
- var modifier = reverse ? -1 : 1;
665
- var nextNode = reverse ? 'prev' : 'next';
666
- while (currentNode) {
667
- f(currentNode.val, currentIndex, this);
668
- currentNode = currentNode[nextNode];
669
- currentIndex += modifier;
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`.
350
+ */
351
+ findNode(value) {
352
+ let current = this.head;
353
+ while (current) {
354
+ if (current.val === value) {
355
+ return current;
356
+ }
357
+ current = current.next;
670
358
  }
671
- };
672
- /**
673
- * The map() method creates a new list with the results of
674
- * calling a provided function on every node in the calling list.
675
- * ```ts
676
- * new LinkedList(1, 2, 3).map(val => val + 10); // 11 <=> 12 <=> 13
677
- * ```
678
- * @param f Function that produces an node of the new list, taking up to three arguments
679
- * @param reverse Indicates if the list should be mapped in reverse order, default is false
680
- */
681
- // eslint-disable-next-line @typescript-eslint/ban-types
682
- SinglyLinkedList.prototype.map = function (f, reverse) {
683
- var _this = this;
684
- if (reverse === void 0) { reverse = false; }
685
- var list = new SinglyLinkedList();
686
- this.forEach(function (val, index) { return list.append(f(val, index, _this)); }, reverse);
687
- return list;
688
- };
359
+ return null;
360
+ }
689
361
  /**
690
- * The filter() method creates a new list with all nodes
691
- * that pass the test implemented by the provided function.
692
- * ```ts
693
- * new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
694
- * ```
695
- * @param f Function to test each node val in the list. Return true to keep the node
696
- * @param reverse Indicates if the list should be filtered in reverse order, default is false
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.
697
368
  */
698
- // eslint-disable-next-line @typescript-eslint/ban-types
699
- SinglyLinkedList.prototype.filter = function (f, reverse) {
700
- var _this = this;
701
- if (reverse === void 0) { reverse = false; }
702
- var list = new SinglyLinkedList();
703
- this.forEach(function (val, index) {
704
- if (f(val, index, _this)) {
705
- list.append(val);
369
+ insertBefore(existingValueOrNode, newValue) {
370
+ if (!this.head)
371
+ return false;
372
+ let existingValue;
373
+ if (existingValueOrNode instanceof SinglyLinkedListNode) {
374
+ existingValue = existingValueOrNode.val;
375
+ }
376
+ else {
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;
706
391
  }
707
- }, reverse);
708
- return list;
709
- };
392
+ current = current.next;
393
+ }
394
+ return false;
395
+ }
710
396
  /**
711
- * Reduce over each node in the list
712
- * ```ts
713
- * new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
714
- * ```
715
- * @param f A reducer function
716
- * @param start An initial value
717
- * @returns The final state of the accumulator
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.
718
403
  */
719
- SinglyLinkedList.prototype.reduce = function (f, start, reverse) {
720
- if (reverse === void 0) { reverse = false; }
721
- var currentIndex = reverse ? this.length - 1 : 0;
722
- var modifier = reverse ? -1 : 1;
723
- var nextNode = reverse ? 'prev' : 'next';
724
- var currentElement = reverse ? this.tail : this.head;
725
- var result;
726
- if (start !== undefined) {
727
- result = start;
728
- }
729
- else if (currentElement) {
730
- result = currentElement.val;
731
- currentElement = currentElement[nextNode];
404
+ insertAfter(existingValueOrNode, newValue) {
405
+ let existingNode;
406
+ if (existingValueOrNode instanceof SinglyLinkedListNode) {
407
+ existingNode = existingValueOrNode;
732
408
  }
733
409
  else {
734
- throw new TypeError('Reduce of empty LinkedList with no initial value');
735
- }
736
- while (currentElement) {
737
- result = f(result, currentElement.val, currentIndex, this);
738
- currentIndex += modifier;
739
- currentElement = currentElement[nextNode];
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;
740
421
  }
741
- return result;
742
- };
743
- /**
744
- * Convert the linked list to an array
745
- * ```ts
746
- * new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]
747
- * ```
748
- */
749
- SinglyLinkedList.prototype.toArray = function () {
750
- return __spreadArray([], __read(this), false);
751
- };
752
- /**
753
- * Convert a linked list to string
754
- * ```ts
755
- * new LinkedList('one', 'two', 'three').toString(' <=> ') === 'one <=> two <=> three';
756
- * ```
757
- * @param separator Optional string to be placed in between val nodes, default is one space
758
- */
759
- SinglyLinkedList.prototype.toString = function (separator) {
760
- if (separator === void 0) { separator = ' '; }
761
- return this.reduce(function (s, val) { return "".concat(s).concat(separator).concat(val); });
762
- };
422
+ return false;
423
+ }
763
424
  /**
764
- * The iterator implementation
765
- * ```ts
766
- * const list = new LinkedList(1, 2, 3);
767
- * for (const val of list) { log(val); } // 1 2 3
768
- * ```
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.
769
428
  */
770
- SinglyLinkedList.prototype[Symbol.iterator] = function () {
771
- var element;
772
- return __generator(this, function (_a) {
773
- switch (_a.label) {
774
- case 0:
775
- element = this.head;
776
- _a.label = 1;
777
- case 1:
778
- if (!(element !== null)) return [3 /*break*/, 3];
779
- return [4 /*yield*/, element.val];
780
- case 2:
781
- _a.sent();
782
- element = element.next;
783
- return [3 /*break*/, 1];
784
- case 3: return [2 /*return*/];
429
+ countOccurrences(value) {
430
+ let count = 0;
431
+ let current = this.head;
432
+ while (current) {
433
+ if (current.val === value) {
434
+ count++;
785
435
  }
786
- });
787
- };
788
- /** Private helper function to reduce duplication of pop() and shift() methods */
789
- SinglyLinkedList.prototype.removeFromAnyEnd = function (node) {
790
- return node !== null ? this.removeNode(node).val : undefined;
791
- };
792
- return SinglyLinkedList;
793
- }());
436
+ current = current.next;
437
+ }
438
+ return count;
439
+ }
440
+ }
794
441
  exports.SinglyLinkedList = SinglyLinkedList;