data-structure-typed 1.15.2 → 1.17.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 (212) hide show
  1. package/README.md +24 -24
  2. package/dist/data-structures/heap/heap.d.ts +30 -15
  3. package/dist/data-structures/heap/heap.js +60 -15
  4. package/dist/data-structures/heap/max-heap.d.ts +3 -3
  5. package/dist/data-structures/heap/min-heap.d.ts +3 -3
  6. package/dist/data-structures/index.d.ts +1 -0
  7. package/dist/data-structures/index.js +1 -0
  8. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +195 -68
  9. package/dist/data-structures/linked-list/doubly-linked-list.js +456 -244
  10. package/dist/data-structures/linked-list/singly-linked-list.d.ts +126 -241
  11. package/dist/data-structures/linked-list/singly-linked-list.js +331 -628
  12. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
  13. package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
  14. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
  15. package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
  16. package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
  17. package/dist/data-structures/priority-queue/priority-queue.js +28 -22
  18. package/dist/data-structures/tree/index.d.ts +1 -0
  19. package/dist/data-structures/tree/index.js +17 -0
  20. package/dist/data-structures/tree/tree.d.ts +9 -0
  21. package/dist/data-structures/tree/tree.js +52 -0
  22. package/dist/data-structures/types/binary-tree.d.ts +0 -5
  23. package/dist/data-structures/types/heap.d.ts +0 -4
  24. package/dist/utils/types/utils.d.ts +1 -0
  25. package/docs/.nojekyll +1 -0
  26. package/docs/assets/highlight.css +92 -0
  27. package/docs/assets/main.js +58 -0
  28. package/docs/assets/search.js +1 -0
  29. package/docs/assets/style.css +1367 -0
  30. package/docs/classes/AVLTree.html +2191 -0
  31. package/docs/classes/AVLTreeNode.html +573 -0
  32. package/docs/classes/AaTree.html +149 -0
  33. package/docs/classes/AbstractEdge.html +268 -0
  34. package/docs/classes/AbstractGraph.html +926 -0
  35. package/docs/classes/AbstractVertex.html +214 -0
  36. package/docs/classes/ArrayDeque.html +416 -0
  37. package/docs/classes/BST.html +2037 -0
  38. package/docs/classes/BSTNode.html +569 -0
  39. package/docs/classes/BTree.html +149 -0
  40. package/docs/classes/BinaryIndexedTree.html +288 -0
  41. package/docs/classes/BinaryTree.html +1826 -0
  42. package/docs/classes/BinaryTreeNode.html +532 -0
  43. package/docs/classes/Character.html +197 -0
  44. package/docs/classes/CoordinateMap.html +468 -0
  45. package/docs/classes/CoordinateSet.html +429 -0
  46. package/docs/classes/Deque.html +897 -0
  47. package/docs/classes/DirectedEdge.html +353 -0
  48. package/docs/classes/DirectedGraph.html +1242 -0
  49. package/docs/classes/DirectedVertex.html +225 -0
  50. package/docs/classes/DoublyLinkedList.html +891 -0
  51. package/docs/classes/DoublyLinkedListNode.html +274 -0
  52. package/docs/classes/Heap.html +481 -0
  53. package/docs/classes/HeapItem.html +232 -0
  54. package/docs/classes/Matrix2D.html +479 -0
  55. package/docs/classes/MatrixNTI2D.html +217 -0
  56. package/docs/classes/MaxHeap.html +499 -0
  57. package/docs/classes/MaxPriorityQueue.html +808 -0
  58. package/docs/classes/MinHeap.html +500 -0
  59. package/docs/classes/MinPriorityQueue.html +810 -0
  60. package/docs/classes/Navigator.html +290 -0
  61. package/docs/classes/ObjectDeque.html +422 -0
  62. package/docs/classes/PriorityQueue.html +732 -0
  63. package/docs/classes/Queue.html +369 -0
  64. package/docs/classes/RBTree.html +149 -0
  65. package/docs/classes/SegmentTree.html +344 -0
  66. package/docs/classes/SegmentTreeNode.html +417 -0
  67. package/docs/classes/SinglyLinkedList.html +718 -0
  68. package/docs/classes/SinglyLinkedListNode.html +247 -0
  69. package/docs/classes/SplayTree.html +149 -0
  70. package/docs/classes/Stack.html +345 -0
  71. package/docs/classes/TreeMultiSet.html +2035 -0
  72. package/docs/classes/TreeNode.html +235 -0
  73. package/docs/classes/Trie.html +349 -0
  74. package/docs/classes/TrieNode.html +257 -0
  75. package/docs/classes/TwoThreeTree.html +149 -0
  76. package/docs/classes/UndirectedEdge.html +312 -0
  77. package/docs/classes/UndirectedGraph.html +1079 -0
  78. package/docs/classes/UndirectedVertex.html +225 -0
  79. package/docs/classes/Vector2D.html +782 -0
  80. package/docs/enums/CP.html +158 -0
  81. package/docs/enums/FamilyPosition.html +158 -0
  82. package/docs/enums/LoopType.html +159 -0
  83. package/docs/index.html +493 -0
  84. package/docs/interfaces/AVLTreeDeleted.html +160 -0
  85. package/docs/interfaces/HeapOptions.html +166 -0
  86. package/docs/interfaces/IDirectedGraph.html +242 -0
  87. package/docs/interfaces/IGraph.html +426 -0
  88. package/docs/interfaces/NavigatorParams.html +196 -0
  89. package/docs/interfaces/PriorityQueueOptions.html +167 -0
  90. package/docs/modules.html +216 -0
  91. package/docs/types/BSTComparator.html +139 -0
  92. package/docs/types/BSTDeletedResult.html +136 -0
  93. package/docs/types/BinaryTreeDeleted.html +136 -0
  94. package/docs/types/BinaryTreeNodeId.html +124 -0
  95. package/docs/types/BinaryTreeNodePropertyName.html +124 -0
  96. package/docs/types/DFSOrderPattern.html +124 -0
  97. package/docs/types/DijkstraResult.html +144 -0
  98. package/docs/types/Direction.html +124 -0
  99. package/docs/types/DoublyLinkedListGetBy.html +124 -0
  100. package/docs/types/NodeOrPropertyName.html +124 -0
  101. package/docs/types/PriorityQueueComparator.html +144 -0
  102. package/docs/types/PriorityQueueDFSOrderPattern.html +124 -0
  103. package/docs/types/ResultByProperty.html +129 -0
  104. package/docs/types/ResultsByProperty.html +129 -0
  105. package/docs/types/SegmentTreeNodeVal.html +124 -0
  106. package/docs/types/SpecifyOptional.html +131 -0
  107. package/docs/types/Thunk.html +132 -0
  108. package/docs/types/ToThunkFn.html +132 -0
  109. package/docs/types/TopologicalStatus.html +124 -0
  110. package/docs/types/TreeMultiSetDeletedResult.html +136 -0
  111. package/docs/types/TrlAsyncFn.html +137 -0
  112. package/docs/types/TrlFn.html +137 -0
  113. package/docs/types/Turning.html +124 -0
  114. package/docs/types/VertexId.html +124 -0
  115. package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
  116. package/notes/note.md +23 -0
  117. package/package.json +1 -3
  118. package/.idea/data-structure-typed.iml +0 -14
  119. package/.idea/modules.xml +0 -8
  120. package/.idea/vcs.xml +0 -6
  121. package/src/assets/complexities-diff.jpg +0 -0
  122. package/src/assets/data-structure-complexities.jpg +0 -0
  123. package/src/assets/logo.png +0 -0
  124. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  125. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  126. package/src/data-structures/binary-tree/avl-tree.ts +0 -293
  127. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  128. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
  129. package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
  130. package/src/data-structures/binary-tree/bst.ts +0 -497
  131. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  132. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  133. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  134. package/src/data-structures/binary-tree/index.ts +0 -11
  135. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  136. package/src/data-structures/binary-tree/segment-tree.ts +0 -267
  137. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  138. package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
  139. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  140. package/src/data-structures/diagrams/README.md +0 -5
  141. package/src/data-structures/graph/abstract-graph.ts +0 -958
  142. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  143. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  148. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  149. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  150. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  151. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  152. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  153. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  154. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  155. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  156. package/src/data-structures/graph/directed-graph.ts +0 -429
  157. package/src/data-structures/graph/index.ts +0 -3
  158. package/src/data-structures/graph/undirected-graph.ts +0 -259
  159. package/src/data-structures/hash/coordinate-map.ts +0 -74
  160. package/src/data-structures/hash/coordinate-set.ts +0 -63
  161. package/src/data-structures/hash/hash-table.ts +0 -1
  162. package/src/data-structures/hash/index.ts +0 -6
  163. package/src/data-structures/hash/pair.ts +0 -1
  164. package/src/data-structures/hash/tree-map.ts +0 -1
  165. package/src/data-structures/hash/tree-set.ts +0 -1
  166. package/src/data-structures/heap/heap.ts +0 -162
  167. package/src/data-structures/heap/index.ts +0 -3
  168. package/src/data-structures/heap/max-heap.ts +0 -31
  169. package/src/data-structures/heap/min-heap.ts +0 -34
  170. package/src/data-structures/index.ts +0 -13
  171. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
  172. package/src/data-structures/linked-list/index.ts +0 -2
  173. package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
  174. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  175. package/src/data-structures/matrix/index.ts +0 -4
  176. package/src/data-structures/matrix/matrix.ts +0 -27
  177. package/src/data-structures/matrix/matrix2d.ts +0 -208
  178. package/src/data-structures/matrix/navigator.ts +0 -122
  179. package/src/data-structures/matrix/vector2d.ts +0 -316
  180. package/src/data-structures/priority-queue/index.ts +0 -3
  181. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
  182. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
  183. package/src/data-structures/priority-queue/priority-queue.ts +0 -349
  184. package/src/data-structures/queue/deque.ts +0 -251
  185. package/src/data-structures/queue/index.ts +0 -2
  186. package/src/data-structures/queue/queue.ts +0 -120
  187. package/src/data-structures/stack/index.ts +0 -1
  188. package/src/data-structures/stack/stack.ts +0 -98
  189. package/src/data-structures/trie/index.ts +0 -1
  190. package/src/data-structures/trie/trie.ts +0 -225
  191. package/src/data-structures/types/abstract-graph.ts +0 -51
  192. package/src/data-structures/types/avl-tree.ts +0 -6
  193. package/src/data-structures/types/binary-tree.ts +0 -15
  194. package/src/data-structures/types/bst.ts +0 -5
  195. package/src/data-structures/types/directed-graph.ts +0 -18
  196. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  197. package/src/data-structures/types/heap.ts +0 -8
  198. package/src/data-structures/types/index.ts +0 -13
  199. package/src/data-structures/types/navigator.ts +0 -13
  200. package/src/data-structures/types/priority-queue.ts +0 -9
  201. package/src/data-structures/types/segment-tree.ts +0 -1
  202. package/src/data-structures/types/singly-linked-list.ts +0 -1
  203. package/src/data-structures/types/tree-multiset.ts +0 -3
  204. package/src/index.ts +0 -1
  205. package/src/utils/index.ts +0 -2
  206. package/src/utils/types/index.ts +0 -1
  207. package/src/utils/types/utils.ts +0 -4
  208. package/src/utils/utils.ts +0 -78
  209. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  210. package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
  211. package/tests/unit/data-structures/graph/index.ts +0 -3
  212. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
@@ -1,37 +1,14 @@
1
1
  "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Tyler Zeng
6
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- var __generator = (this && this.__generator) || function (thisArg, body) {
10
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
11
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
12
- function verb(n) { return function (v) { return step([n, v]); }; }
13
- function step(op) {
14
- if (f) throw new TypeError("Generator is already executing.");
15
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
16
- 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;
17
- if (y = 0, t) op = [op[0] & 2, t.value];
18
- switch (op[0]) {
19
- case 0: case 1: t = op; break;
20
- case 4: _.label++; return { value: op[1], done: false };
21
- case 5: _.label++; y = op[1]; op = [0]; continue;
22
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
23
- default:
24
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
25
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
26
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
27
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
28
- if (t[2]) _.ops.pop();
29
- _.trys.pop(); continue;
30
- }
31
- op = body.call(thisArg, _);
32
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
33
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
34
- }
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
35
12
  };
36
13
  var __read = (this && this.__read) || function (o, n) {
37
14
  var m = typeof Symbol === "function" && o[Symbol.iterator];
@@ -49,36 +26,24 @@ var __read = (this && this.__read) || function (o, n) {
49
26
  }
50
27
  return ar;
51
28
  };
52
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
53
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
54
- if (ar || !(i in from)) {
55
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
56
- ar[i] = from[i];
57
- }
58
- }
59
- return to.concat(ar || Array.prototype.slice.call(from));
60
- };
61
- var __values = (this && this.__values) || function(o) {
62
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
63
- if (m) return m.call(o);
64
- if (o && typeof o.length === "number") return {
65
- next: function () {
66
- if (o && i >= o.length) o = void 0;
67
- return { value: o && o[i++], done: !o };
68
- }
69
- };
70
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
71
- };
72
29
  Object.defineProperty(exports, "__esModule", { value: true });
73
30
  exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
74
- /* The SinglyLinkedListNode class represents a node in a singly linked list and provides methods for inserting, removing,
75
- and accessing nodes. */
31
+ /**
32
+ * data-structure-typed
33
+ *
34
+ * @author Tyler Zeng
35
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
36
+ * @license MIT License
37
+ */
76
38
  var SinglyLinkedListNode = /** @class */ (function () {
77
- function SinglyLinkedListNode(val, prev, next, list) {
39
+ /**
40
+ * The constructor function initializes an instance of a class with a given value and sets the next property to null.
41
+ * @param {T} val - The "val" parameter is of type T, which means it can be any data type. It represents the value that
42
+ * will be stored in the node of a linked list.
43
+ */
44
+ function SinglyLinkedListNode(val) {
78
45
  this._val = val;
79
- this._prev = prev || null;
80
- this._next = next || null;
81
- this._list = list || null;
46
+ this._next = null;
82
47
  }
83
48
  Object.defineProperty(SinglyLinkedListNode.prototype, "val", {
84
49
  get: function () {
@@ -90,16 +55,6 @@ var SinglyLinkedListNode = /** @class */ (function () {
90
55
  enumerable: false,
91
56
  configurable: true
92
57
  });
93
- Object.defineProperty(SinglyLinkedListNode.prototype, "prev", {
94
- get: function () {
95
- return this._prev;
96
- },
97
- set: function (value) {
98
- this._prev = value;
99
- },
100
- enumerable: false,
101
- configurable: true
102
- });
103
58
  Object.defineProperty(SinglyLinkedListNode.prototype, "next", {
104
59
  get: function () {
105
60
  return this._next;
@@ -110,78 +65,17 @@ var SinglyLinkedListNode = /** @class */ (function () {
110
65
  enumerable: false,
111
66
  configurable: true
112
67
  });
113
- Object.defineProperty(SinglyLinkedListNode.prototype, "list", {
114
- get: function () {
115
- return this._list;
116
- },
117
- set: function (value) {
118
- this._list = value;
119
- },
120
- enumerable: false,
121
- configurable: true
122
- });
123
- Object.defineProperty(SinglyLinkedListNode.prototype, "index", {
124
- get: function () {
125
- var _this = this;
126
- if (!this.list) {
127
- return undefined;
128
- }
129
- return this.list.findIndex(function (value) { return value === _this.val; });
130
- },
131
- enumerable: false,
132
- configurable: true
133
- });
134
- /**
135
- * The `insertBefore` function inserts a new node with the given value before the current node in a singly linked list.
136
- * @param {NodeVal} val - The parameter "val" is of type "NodeVal". It represents the value of the node that you want
137
- * to insert before the current node.
138
- * @returns The method is returning a SinglyLinkedList<NodeVal>.
139
- */
140
- SinglyLinkedListNode.prototype.insertBefore = function (val) {
141
- return this.list !== null
142
- ? this.list.insertBefore(this, val)
143
- : new SinglyLinkedList(val, this.val);
144
- };
145
- /**
146
- * The function inserts a new node with the given value after the current node in a singly linked list.
147
- * @param {NodeVal} val - The parameter `val` is the value of the node that you want to insert after the current node.
148
- * @returns The method is returning a SinglyLinkedList<NodeVal>.
149
- */
150
- SinglyLinkedListNode.prototype.insertAfter = function (val) {
151
- return this.list !== null
152
- ? this.list.insertAfter(this, val)
153
- : new SinglyLinkedList(this.val, val);
154
- };
155
- /**
156
- * The `remove()` function removes a node from a singly linked list.
157
- * @returns The remove() method is returning a SinglyLinkedListNode<NodeVal> object.
158
- */
159
- SinglyLinkedListNode.prototype.remove = function () {
160
- if (this.list === null) {
161
- throw new ReferenceError('Node does not belong to any list');
162
- }
163
- return this.list.removeNode(this);
164
- };
165
68
  return SinglyLinkedListNode;
166
69
  }());
167
70
  exports.SinglyLinkedListNode = SinglyLinkedListNode;
168
71
  var SinglyLinkedList = /** @class */ (function () {
169
72
  /**
170
- * The constructor initializes a linked list with the given arguments as nodes.
171
- * @param {NodeVal[]} args - args is a rest parameter that allows the constructor to accept an arbitrary number of
172
- * arguments of type NodeVal.
73
+ * The constructor initializes the linked list with an empty head, tail, and length.
173
74
  */
174
75
  function SinglyLinkedList() {
175
- var args = [];
176
- for (var _i = 0; _i < arguments.length; _i++) {
177
- args[_i] = arguments[_i];
178
- }
179
76
  this._head = null;
180
77
  this._tail = null;
181
- this._size = 0;
182
- for (var i = 0; i < arguments.length; i++) {
183
- this.append(args[i]);
184
- }
78
+ this._length = 0;
185
79
  }
186
80
  Object.defineProperty(SinglyLinkedList.prototype, "head", {
187
81
  get: function () {
@@ -203,583 +97,392 @@ var SinglyLinkedList = /** @class */ (function () {
203
97
  enumerable: false,
204
98
  configurable: true
205
99
  });
206
- Object.defineProperty(SinglyLinkedList.prototype, "size", {
100
+ Object.defineProperty(SinglyLinkedList.prototype, "length", {
207
101
  get: function () {
208
- return this._size;
102
+ return this._length;
209
103
  },
210
104
  set: function (value) {
211
- this._size = value;
105
+ this._length = value;
212
106
  },
213
107
  enumerable: false,
214
108
  configurable: true
215
109
  });
216
110
  /**
217
- * The `from` function in TypeScript creates a new SinglyLinkedList instance from an iterable object.
218
- * @param iterable - The `iterable` parameter is an object that can be iterated over, such as an array or a string. It
219
- * contains a collection of elements of type `T`.
220
- * @returns The method is returning a new instance of the SinglyLinkedList class.
221
- */
222
- SinglyLinkedList.from = function (iterable) {
223
- return new (SinglyLinkedList.bind.apply(SinglyLinkedList, __spreadArray([void 0], __read(iterable), false)))();
224
- };
225
- /**
226
- * The `get` function returns the value of a node at a given index in a data structure.
227
- * @param {number} index - The index parameter is a number that represents the position of the node in the data
228
- * structure.
229
- * @returns The method is returning the value of the node at the specified index if the node exists, otherwise it
230
- * returns undefined.
231
- */
232
- SinglyLinkedList.prototype.get = function (index) {
233
- var node = this.getNode(index);
234
- return node !== undefined ? node.val : undefined;
235
- };
236
- /**
237
- * The function `getNode` returns the node at a given index in a singly linked list.
238
- * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
239
- * retrieve from the linked list.
240
- * @returns a SinglyLinkedListNode<NodeVal> object or undefined.
111
+ * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
112
+ * array.
113
+ * @param {T[]} data - The `data` parameter is an array of elements of type `T`.
114
+ * @returns The `fromArray` function returns a `SinglyLinkedList` object.
241
115
  */
242
- SinglyLinkedList.prototype.getNode = function (index) {
243
- if (this.head === null || index < 0 || index >= this.size) {
244
- return undefined;
245
- }
246
- var asc = index < this.size / 2;
247
- var stopAt = asc ? index : this.size - index - 1;
248
- var nextNode = asc ? 'next' : 'prev';
249
- var currentNode = asc ? this.head : this.tail;
250
- // TODO after no-non-null-assertion not ensure the logic
251
- for (var currentIndex = 0; currentIndex < stopAt; currentIndex++) {
252
- if (currentNode) {
253
- currentNode = currentNode[nextNode];
254
- }
255
- }
256
- return currentNode || undefined;
257
- };
258
- /**
259
- * The function `findNodeIndex` searches for a node in a singly linked list that satisfies a given condition and
260
- * returns its index and the node itself.
261
- * @param callbackFn - The callbackFn parameter is a function that takes three arguments: data, index, and list. It is
262
- * used to determine whether a node in the singly linked list matches a certain condition. The function should return a
263
- * boolean value indicating whether the condition is met for the given node.
264
- * @returns The function `findNodeIndex` returns an object with two properties: `node` and `index`. The `node` property
265
- * contains the node that matches the condition specified in the `callbackFn` function, and the `index` property
266
- * contains the index of that node in the linked list. If no node matches the condition, the function returns
267
- * `undefined`.
268
- */
269
- SinglyLinkedList.prototype.findNodeIndex = function (callbackFn) {
270
- var currentIndex = 0;
271
- var currentNode = this.head;
272
- while (currentNode) {
273
- if (callbackFn(currentNode.val, currentIndex, this)) {
274
- return {
275
- index: currentIndex,
276
- node: currentNode,
277
- };
278
- }
279
- currentNode = currentNode.next;
280
- currentIndex += 1;
281
- }
282
- return undefined;
283
- };
284
- /**
285
- * The findNode function searches for a node in a singly linked list based on a given callback function.
286
- * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
287
- * value indicating whether the current node matches the desired criteria.
288
- * @returns The function `findNode` returns a `SinglyLinkedListNode<NodeVal>` if a node satisfying the condition
289
- * specified by the `callbackFn` is found in the linked list. If no such node is found, it returns `undefined`.
290
- */
291
- SinglyLinkedList.prototype.findNode = function (callbackFn) {
292
- var nodeIndex = this.findNodeIndex(callbackFn);
293
- return nodeIndex !== undefined ? nodeIndex.node : undefined;
294
- };
295
- /**
296
- * The `find` function in TypeScript searches for a node in a singly linked list based on a given callback function and
297
- * returns the value of the found node.
298
- * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
299
- * value indicating whether the condition is met for a particular node in the linked list.
300
- * @returns The method `find` returns the `NodeVal` value of the first node in the linked list that satisfies the
301
- * condition specified by the `callbackFn` function. If no node satisfies the condition, it returns `undefined`.
302
- */
303
- SinglyLinkedList.prototype.find = function (callbackFn) {
304
- var nodeIndex = this.findNodeIndex(callbackFn);
305
- return nodeIndex !== undefined ? nodeIndex.node.val : undefined;
306
- };
307
- /**
308
- * The findIndex function returns the index of the first node in a singly linked list that satisfies a given condition,
309
- * or -1 if no such node is found.
310
- * @param callbackFn - A callback function that takes three parameters: data, index, and list. It returns a boolean
311
- * value indicating whether the condition is met for a particular node in the singly linked list.
312
- * @returns The method `findIndex` returns a number.
313
- */
314
- SinglyLinkedList.prototype.findIndex = function (callbackFn) {
315
- var nodeIndex = this.findNodeIndex(callbackFn);
316
- return nodeIndex !== undefined ? nodeIndex.index : -1;
317
- };
318
- /* The above code is a comment in TypeScript. It is using the triple hash symbol ( */
319
- SinglyLinkedList.prototype.append = function () {
116
+ SinglyLinkedList.fromArray = function (data) {
320
117
  var e_1, _a;
321
- var args = [];
322
- for (var _i = 0; _i < arguments.length; _i++) {
323
- args[_i] = arguments[_i];
324
- }
118
+ var singlyLinkedList = new SinglyLinkedList();
325
119
  try {
326
- for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
327
- var val = args_1_1.value;
328
- var node = new SinglyLinkedListNode(val, this.tail, null, this);
329
- if (this.head === null) {
330
- this.head = node;
331
- }
332
- if (this.tail !== null) {
333
- this.tail.next = node;
334
- }
335
- this.tail = node;
336
- this.size += 1;
120
+ for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
121
+ var item = data_1_1.value;
122
+ singlyLinkedList.push(item);
337
123
  }
338
124
  }
339
125
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
340
126
  finally {
341
127
  try {
342
- if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
128
+ if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
343
129
  }
344
130
  finally { if (e_1) throw e_1.error; }
345
131
  }
346
- return this;
132
+ return singlyLinkedList;
347
133
  };
348
- /**
349
- * The push function appends multiple NodeVal objects to a data structure and returns the new size of the data
350
- * structure.
351
- * @param {NodeVal[]} args - args is a rest parameter of type NodeVal[]. It allows the function to accept any number
352
- * of arguments of type NodeVal.
353
- * @returns The size of the data structure after the nodes are appended.
354
- */
355
- SinglyLinkedList.prototype.push = function () {
356
- var args = [];
357
- for (var _i = 0; _i < arguments.length; _i++) {
358
- args[_i] = arguments[_i];
359
- }
360
- this.append.apply(this, __spreadArray([], __read(args), false));
361
- return this.size;
134
+ SinglyLinkedList.prototype.getLength = function () {
135
+ return this._length;
362
136
  };
363
137
  /**
364
- * The `prepend` function adds new nodes to the beginning of a singly linked list.
365
- * @param {NodeVal[]} args - An array of NodeVal objects.
366
- * @returns The `prepend` method is returning the updated `SinglyLinkedList` object.
138
+ * The `push` function adds a new node with the given data to the end of a singly linked list.
139
+ * @param {T} data - The "data" parameter represents the value that you want to add to the linked list. It can be of
140
+ * any type (T) as specified in the generic type declaration of the class or function.
367
141
  */
368
- SinglyLinkedList.prototype.prepend = function () {
369
- var e_2, _a;
370
- var args = [];
371
- for (var _i = 0; _i < arguments.length; _i++) {
372
- args[_i] = arguments[_i];
373
- }
374
- var reverseArgs = Array.from(args).reverse();
375
- try {
376
- for (var reverseArgs_1 = __values(reverseArgs), reverseArgs_1_1 = reverseArgs_1.next(); !reverseArgs_1_1.done; reverseArgs_1_1 = reverseArgs_1.next()) {
377
- var val = reverseArgs_1_1.value;
378
- var node = new SinglyLinkedListNode(val, null, this.head, this);
379
- if (this.tail === null) {
380
- this.tail = node;
381
- }
382
- if (this.head !== null) {
383
- this.head.prev = node;
384
- }
385
- this.head = node;
386
- this.size += 1;
387
- }
388
- }
389
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
390
- finally {
391
- try {
392
- if (reverseArgs_1_1 && !reverseArgs_1_1.done && (_a = reverseArgs_1.return)) _a.call(reverseArgs_1);
393
- }
394
- finally { if (e_2) throw e_2.error; }
142
+ SinglyLinkedList.prototype.push = function (data) {
143
+ var newNode = new SinglyLinkedListNode(data);
144
+ if (!this.head) {
145
+ this.head = newNode;
146
+ this.tail = newNode;
395
147
  }
396
- return this;
397
- };
398
- /**
399
- * The `insertAt` function inserts a value at a specified index in a singly linked list.
400
- * @param {number} index - The index parameter is a number that represents the position at which the new node should be
401
- * inserted in the linked list.
402
- * @param {NodeVal} val - The `val` parameter represents the value of the node that you want to insert into the linked
403
- * list.
404
- * @returns The method `insertAt` returns the updated `SinglyLinkedList` object.
405
- */
406
- SinglyLinkedList.prototype.insertAt = function (index, val) {
407
- if (this.head === null) {
408
- return this.append(val);
409
- }
410
- if (index <= 0) {
411
- return this.prepend(val);
412
- }
413
- var currentNode = this.head;
414
- var currentIndex = 0;
415
- while (currentIndex < index - 1 && currentNode.next !== null) {
416
- currentIndex += 1;
417
- currentNode = currentNode.next;
148
+ else {
149
+ this.tail.next = newNode;
150
+ this.tail = newNode;
418
151
  }
419
- currentNode.insertAfter(val);
420
- return this;
152
+ this.length++;
421
153
  };
422
154
  /**
423
- * The removeNode function removes a node from a singly linked list and updates the head, tail, and size properties
424
- * accordingly.
425
- * @param node - The `node` parameter is of type `SinglyLinkedListNode<NodeVal>`, which represents a node in a singly
426
- * linked list.
427
- * @returns the removed node.
155
+ * The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
156
+ * pointers accordingly.
157
+ * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
158
+ * the linked list is empty, it returns `null`.
428
159
  */
429
- SinglyLinkedList.prototype.removeNode = function (node) {
430
- if (node.list !== this) {
431
- throw new ReferenceError('Node does not belong to this list');
432
- }
433
- if (node.prev !== null) {
434
- node.prev.next = node.next;
435
- }
436
- if (node.next !== null) {
437
- node.next.prev = node.prev;
438
- }
439
- if (this.head === node) {
440
- this.head = node.next;
160
+ SinglyLinkedList.prototype.pop = function () {
161
+ if (!this.head)
162
+ return null;
163
+ if (this.head === this.tail) {
164
+ var val_1 = this.head.val;
165
+ this.head = null;
166
+ this.tail = null;
167
+ this.length--;
168
+ return val_1;
441
169
  }
442
- if (this.tail === node) {
443
- this.tail = node.prev;
170
+ var current = this.head;
171
+ while (current.next !== this.tail) {
172
+ current = current.next;
444
173
  }
445
- this.size -= 1;
446
- node.next = null;
447
- node.prev = null;
448
- node.list = null;
449
- return node;
174
+ var val = this.tail.val;
175
+ current.next = null;
176
+ this.tail = current;
177
+ this.length--;
178
+ return val;
450
179
  };
451
180
  /**
452
- * The `removeAt` function removes a node at a specified index from a singly linked list.
453
- * @param {number} index - The index parameter is a number that represents the position of the node to be removed in
454
- * the singly linked list.
455
- * @returns The method `removeAt` returns a `SinglyLinkedListNode<NodeVal>` if the node at the specified index is
456
- * found and removed successfully. If the node is not found, it returns `undefined`.
181
+ * The `shift()` function removes and returns the value of the first node in a linked list.
182
+ * @returns The value of the node that is being removed from the beginning of the linked list.
457
183
  */
458
- SinglyLinkedList.prototype.removeAt = function (index) {
459
- var node = this.getNode(index);
460
- return node !== undefined ? this.removeNode(node) : undefined;
184
+ SinglyLinkedList.prototype.shift = function () {
185
+ if (!this.head)
186
+ return null;
187
+ var removedNode = this.head;
188
+ this.head = this.head.next;
189
+ this.length--;
190
+ return removedNode.val;
461
191
  };
462
192
  /**
463
- * The `insertBefore` function inserts a new node with a given value before a specified reference node in a singly
193
+ * The unshift function adds a new node with the given value to the beginning of a singly linked list.
194
+ * @param {T} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
464
195
  * linked list.
465
- * @param referenceNode - The referenceNode parameter is the node in the linked list before which the new node will be
466
- * inserted.
467
- * @param {NodeVal} val - The value of the new node that will be inserted before the reference node.
468
- * @returns The method is returning the updated SinglyLinkedList object.
469
196
  */
470
- SinglyLinkedList.prototype.insertBefore = function (referenceNode, val) {
471
- var node = new SinglyLinkedListNode(val, referenceNode.prev, referenceNode, this);
472
- if (referenceNode.prev === null) {
473
- this.head = node;
197
+ SinglyLinkedList.prototype.unshift = function (val) {
198
+ var newNode = new SinglyLinkedListNode(val);
199
+ if (!this.head) {
200
+ this.head = newNode;
201
+ this.tail = newNode;
474
202
  }
475
- if (referenceNode.prev !== null) {
476
- referenceNode.prev.next = node;
203
+ else {
204
+ newNode.next = this.head;
205
+ this.head = newNode;
477
206
  }
478
- referenceNode.prev = node;
479
- this.size += 1;
480
- return this;
207
+ this.length++;
481
208
  };
482
209
  /**
483
- * The `sort` function uses the quicksort algorithm to sort the elements of a singly linked list based on a provided
484
- * comparison function.
485
- * @param start - The `start` parameter is the starting node of the sublist that needs to be sorted.
486
- * @param end - The `end` parameter is a reference to the last node in the linked list. It is used as the pivot element
487
- * for the quicksort algorithm.
488
- * @returns The `sort` method is returning the sorted `SinglyLinkedList` object.
210
+ * The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
211
+ * @param {number} index - The index parameter is a number that represents the position of the element we want to
212
+ * retrieve from the list.
213
+ * @returns The method `getAt(index: number): T | null` returns the value at the specified index in the linked list, or
214
+ * `null` if the index is out of bounds.
489
215
  */
490
- SinglyLinkedList.prototype.sort = function (compare) {
491
- if (this.head === null || this.tail === null) {
492
- return this;
216
+ SinglyLinkedList.prototype.getAt = function (index) {
217
+ if (index < 0 || index >= this.length)
218
+ return null;
219
+ var current = this.head;
220
+ for (var i = 0; i < index; i++) {
221
+ current = current.next;
493
222
  }
494
- if (this.size < 2) {
495
- return this;
496
- }
497
- var quicksort = function (start, end) {
498
- if (start === end) {
499
- return;
500
- }
501
- var pivotData = end.val;
502
- var current = start;
503
- var split = start;
504
- while (current && current !== end) {
505
- var sort = compare(current.val, pivotData);
506
- if (sort) {
507
- if (current !== split) {
508
- var temp = split.val;
509
- split.val = current.val;
510
- current.val = temp;
223
+ return current.val;
224
+ };
225
+ /**
226
+ * The function `getNodeAt` returns the node at a given index in a singly linked list.
227
+ * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
228
+ * retrieve from the linked list. It indicates the zero-based index of the node we want to access.
229
+ * @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<T>` object if the node at the
230
+ * specified index exists, or `null` if the index is out of bounds.
231
+ */
232
+ SinglyLinkedList.prototype.getNodeAt = function (index) {
233
+ var current = this.head;
234
+ for (var i = 0; i < index; i++) {
235
+ current = current.next;
236
+ }
237
+ return current;
238
+ };
239
+ /**
240
+ * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
241
+ * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
242
+ * data structure. It is of type number.
243
+ * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
244
+ * bounds.
245
+ */
246
+ SinglyLinkedList.prototype.deleteAt = function (index) {
247
+ if (index < 0 || index >= this.length)
248
+ return null;
249
+ if (index === 0)
250
+ return this.shift();
251
+ if (index === this.length - 1)
252
+ return this.pop();
253
+ var prevNode = this.getNodeAt(index - 1);
254
+ var removedNode = prevNode.next;
255
+ prevNode.next = removedNode.next;
256
+ this.length--;
257
+ return removedNode.val;
258
+ };
259
+ /**
260
+ * The `delete` function removes a specified value from a linked list and returns true if the value was found and
261
+ * removed, otherwise it returns false.
262
+ * @param {T} value - The value parameter represents the value of the node that needs to be deleted from the linked
263
+ * list.
264
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value was successfully deleted from
265
+ * the linked list, and `false` if the value was not found in the linked list.
266
+ */
267
+ SinglyLinkedList.prototype.delete = function (value) {
268
+ var current = this.head;
269
+ var prev = null;
270
+ while (current) {
271
+ if (current.val === value) {
272
+ if (prev === null) {
273
+ this.head = current.next;
274
+ if (current === this.tail) {
275
+ this.tail = null;
511
276
  }
512
- // TODO after no-non-null-assertion not ensure the logic
513
- if (split.next) {
514
- split = split.next;
277
+ }
278
+ else {
279
+ prev.next = current.next;
280
+ if (current === this.tail) {
281
+ this.tail = prev;
515
282
  }
516
283
  }
517
- current = current.next;
518
- }
519
- end.val = split.val;
520
- split.val = pivotData;
521
- if (start.next === end.prev) {
522
- return;
284
+ this.length--;
285
+ return true;
523
286
  }
524
- if (split.prev && split !== start) {
525
- quicksort(start, split.prev);
526
- }
527
- if (split.next && split !== end) {
528
- quicksort(split.next, end);
529
- }
530
- };
531
- quicksort(this.head, this.tail);
532
- return this;
287
+ prev = current;
288
+ current = current.next;
289
+ }
290
+ return false;
533
291
  };
534
292
  /**
535
- * The `insertAfter` function inserts a new node with a given value after a specified reference node in a singly linked
536
- * list.
537
- * @param referenceNode - The referenceNode parameter is the node after which the new node will be inserted.
538
- * @param {NodeVal} val - The value of the new node that will be inserted after the reference node.
539
- * @returns The `insertAfter` method is returning the updated `SinglyLinkedList` object.
293
+ * The `insert` function inserts a value at a specified index in a singly linked list.
294
+ * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
295
+ * linked list. It is of type number.
296
+ * @param {T} val - The `val` parameter represents the value that you want to insert into the linked list at the
297
+ * specified index.
298
+ * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
299
+ * if the index is out of bounds.
540
300
  */
541
- SinglyLinkedList.prototype.insertAfter = function (referenceNode, val) {
542
- var node = new SinglyLinkedListNode(val, referenceNode, referenceNode.next, this);
543
- if (referenceNode.next === null) {
544
- this.tail = node;
301
+ SinglyLinkedList.prototype.insert = function (index, val) {
302
+ if (index < 0 || index > this.length)
303
+ return false;
304
+ if (index === 0) {
305
+ this.unshift(val);
306
+ return true;
545
307
  }
546
- if (referenceNode.next !== null) {
547
- referenceNode.next.prev = node;
308
+ if (index === this.length) {
309
+ this.push(val);
310
+ return true;
548
311
  }
549
- referenceNode.next = node;
550
- this.size += 1;
551
- return this;
552
- };
553
- /**
554
- * The `shift()` function removes and returns the first element from a linked list.
555
- * @returns The `shift()` method is returning a value of type `NodeVal` or `undefined`.
556
- */
557
- SinglyLinkedList.prototype.shift = function () {
558
- return this.removeFromAnyEnd(this.head);
312
+ var newNode = new SinglyLinkedListNode(val);
313
+ var prevNode = this.getNodeAt(index - 1);
314
+ newNode.next = prevNode.next;
315
+ prevNode.next = newNode;
316
+ this.length++;
317
+ return true;
559
318
  };
560
319
  /**
561
- * The `pop()` function removes and returns the last element from a linked list.
562
- * @returns The `pop()` method is returning a value of type `NodeVal` or `undefined`.
320
+ * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
321
+ * whether it is empty or not.
322
+ * @returns A boolean value indicating whether the length of the object is equal to 0.
563
323
  */
564
- SinglyLinkedList.prototype.pop = function () {
565
- return this.removeFromAnyEnd(this.tail);
324
+ SinglyLinkedList.prototype.isEmpty = function () {
325
+ return this.length === 0;
566
326
  };
567
327
  /**
568
- * The merge function merges two singly linked lists by updating the next and prev pointers, as well as the head, tail,
569
- * and size properties.
570
- * @param list - The parameter "list" is a SinglyLinkedList object that contains nodes with data of type NodeVal.
571
- */
572
- SinglyLinkedList.prototype.merge = function (list) {
573
- if (this.tail !== null) {
574
- this.tail.next = list.head;
575
- }
576
- if (list.head !== null) {
577
- list.head.prev = this.tail;
578
- }
579
- this.head = this.head || list.head;
580
- this.tail = list.tail || this.tail;
581
- this.size += list.size;
582
- list.size = this.size;
583
- list.head = this.head;
584
- list.tail = this.tail;
585
- };
586
- /**
587
- * The clear() function resets the linked list by setting the head and tail to null and the size to 0.
588
- * @returns The "this" object is being returned.
328
+ * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
589
329
  */
590
330
  SinglyLinkedList.prototype.clear = function () {
591
- this.head = null;
592
- this.tail = null;
593
- this.size = 0;
594
- return this;
331
+ this._head = null;
332
+ this._tail = null;
333
+ this._length = 0;
595
334
  };
596
335
  /**
597
- * The `slice` function returns a new SinglyLinkedList containing a portion of the original list, starting from the
598
- * specified index and ending at the optional end index.
599
- * @param {number} start - The `start` parameter is a number that represents the index at which to start slicing the
600
- * linked list.
601
- * @param {number} [end] - The `end` parameter is an optional number that specifies the index at which to end the
602
- * slicing. If no value is provided for `end`, or if the provided value is less than the `start` index, the slicing
603
- * will continue until the end of the list.
604
- * @returns a new SinglyLinkedList containing the sliced elements from the original list.
336
+ * The `toArray` function converts a linked list into an array.
337
+ * @returns The `toArray()` method is returning an array of type `T[]`.
605
338
  */
606
- SinglyLinkedList.prototype.slice = function (start, end) {
607
- var list = new SinglyLinkedList();
608
- var finish = end;
609
- if (this.head === null || this.tail === null) {
610
- return list;
611
- }
612
- if (finish === undefined || finish < start) {
613
- finish = this.size;
614
- }
615
- var head = this.getNode(start);
616
- for (var i = 0; i < finish - start && head !== null && head !== undefined; i++) {
617
- list.append(head.val);
618
- head = head.next;
339
+ SinglyLinkedList.prototype.toArray = function () {
340
+ var array = [];
341
+ var current = this.head;
342
+ while (current) {
343
+ array.push(current.val);
344
+ current = current.next;
619
345
  }
620
- return list;
346
+ return array;
621
347
  };
622
348
  /**
623
- * The reverse() function reverses the order of nodes in a singly linked list.
624
- * @returns The reverse() method is returning the reversed SinglyLinkedList.
349
+ * The `reverse` function reverses the order of the nodes in a singly linked list.
350
+ * @returns The reverse() method does not return anything. It has a return type of void.
625
351
  */
626
352
  SinglyLinkedList.prototype.reverse = function () {
627
- var currentNode = this.head;
628
- while (currentNode) {
629
- var next = currentNode.next;
630
- currentNode.next = currentNode.prev;
631
- currentNode.prev = next;
632
- currentNode = currentNode.prev;
353
+ var _a;
354
+ if (!this.head || this.head === this.tail)
355
+ return;
356
+ var prev = null;
357
+ var current = this.head;
358
+ var next = null;
359
+ while (current) {
360
+ next = current.next;
361
+ current.next = prev;
362
+ prev = current;
363
+ current = next;
364
+ }
365
+ _a = __read([this.tail, this.head], 2), this.head = _a[0], this.tail = _a[1];
366
+ };
367
+ /**
368
+ * The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
369
+ * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
370
+ * function is used to determine whether a particular value in the linked list satisfies a certain condition.
371
+ * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
372
+ * the callback function. If no element satisfies the condition, it returns `null`.
373
+ */
374
+ SinglyLinkedList.prototype.find = function (callback) {
375
+ var current = this.head;
376
+ while (current) {
377
+ if (callback(current.val)) {
378
+ return current.val;
379
+ }
380
+ current = current.next;
633
381
  }
634
- var tail = this.tail;
635
- this.tail = this.head;
636
- this.head = tail;
637
- return this;
382
+ return null;
638
383
  };
639
384
  /**
640
- * The `forEach` function iterates over a singly linked list and applies a callback function to each node, either in
641
- * forward or reverse order.
642
- * @param callbackFn - A callback function that will be called for each element in the linked list. It takes three
643
- * parameters:
644
- * @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
645
- * to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
646
- * (default), the iteration will start from the head and move towards the tail.
385
+ * The `indexOf` function returns the index of the first occurrence of a given value in a linked list.
386
+ * @param {T} value - The value parameter is the value that you want to find the index of in the linked list.
387
+ * @returns The method is returning the index of the first occurrence of the specified value in the linked list. If the
388
+ * value is not found, it returns -1.
647
389
  */
648
- SinglyLinkedList.prototype.forEach = function (callbackFn, reverse) {
649
- if (reverse === void 0) { reverse = false; }
650
- var currentIndex = reverse ? this.size - 1 : 0;
651
- var currentNode = reverse ? this.tail : this.head;
652
- var modifier = reverse ? -1 : 1;
653
- var nextNode = reverse ? 'prev' : 'next';
654
- while (currentNode) {
655
- callbackFn(currentNode.val, currentIndex, this);
656
- currentNode = currentNode[nextNode];
657
- currentIndex += modifier;
390
+ SinglyLinkedList.prototype.indexOf = function (value) {
391
+ var index = 0;
392
+ var current = this.head;
393
+ while (current) {
394
+ if (current.val === value) {
395
+ return index;
396
+ }
397
+ index++;
398
+ current = current.next;
658
399
  }
400
+ return -1;
659
401
  };
660
402
  /**
661
- * The map function takes a callback function and applies it to each element in the linked list, returning a new linked
662
- * list with the results.
663
- * @param callbackFn - A callback function that will be applied to each element in the linked list. It takes three
664
- * parameters:
665
- * @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the mapping should be
666
- * done in reverse order or not. If `reverse` is set to `true`, the mapping will be done in reverse order. If `reverse`
667
- * is set to `false` or not provided, the mapping will be
668
- * @returns The `map` function is returning a new `SinglyLinkedList` object.
403
+ * The function finds a node in a singly linked list by its value and returns the node if found, otherwise returns
404
+ * null.
405
+ * @param {T} value - The value parameter is the value that we want to search for in the linked list.
406
+ * @returns a `SinglyLinkedListNode<T>` if a node with the specified value is found in the linked list. If no node with
407
+ * the specified value is found, the function returns `null`.
669
408
  */
670
- SinglyLinkedList.prototype.map = function (callbackFn, reverse) {
671
- var _this = this;
672
- if (reverse === void 0) { reverse = false; }
673
- var list = new SinglyLinkedList();
674
- this.forEach(function (val, index) { return list.append(callbackFn(val, index, _this)); }, reverse);
675
- return list;
676
- };
677
- /**
678
- * The `filter` function filters the elements of a singly linked list based on a given callback function.
679
- * @param callbackFn - A callback function that takes three parameters: data, index, and list. It should return a
680
- * boolean value indicating whether the current element should be included in the filtered list or not.
681
- * @param [reverse=false] - The `reverse` parameter is a boolean value that determines whether the filtered list should
682
- * be reversed or not. If `reverse` is set to `true`, the filtered list will be in reverse order. If `reverse` is set
683
- * to `false` or not provided, the filtered list will be in
684
- * @returns The `filter` method is returning a new `SinglyLinkedList` object.
685
- */
686
- SinglyLinkedList.prototype.filter = function (callbackFn, reverse) {
687
- var _this = this;
688
- if (reverse === void 0) { reverse = false; }
689
- var list = new SinglyLinkedList();
690
- this.forEach(function (val, index) {
691
- if (callbackFn(val, index, _this)) {
692
- list.append(val);
409
+ SinglyLinkedList.prototype.findNode = function (value) {
410
+ var current = this.head;
411
+ while (current) {
412
+ if (current.val === value) {
413
+ return current;
693
414
  }
694
- }, reverse);
695
- return list;
696
- };
697
- /**
698
- * The `reduce` function iterates over a singly linked list and applies a callback function to each element,
699
- * accumulating a single value.
700
- * @param callbackFn - A callback function that will be called for each element in the linked list. It takes four
701
- * parameters:
702
- * @param {any} [start] - The `start` parameter is an optional initial value for the accumulator. If provided, the
703
- * `reduce` function will start accumulating from this value. If not provided, the `reduce` function will use the value
704
- * of the first element in the linked list as the initial value.
705
- * @param [reverse=false] - A boolean value indicating whether to iterate over the linked list in reverse order. If set
706
- * to true, the iteration will start from the tail of the linked list and move towards the head. If set to false
707
- * (default), the iteration will start from the head and move towards the tail.
708
- * @returns The `reduce` method returns the accumulated value after applying the callback function to each element in
709
- * the linked list.
710
- */
711
- SinglyLinkedList.prototype.reduce = function (callbackFn, start, reverse) {
712
- if (reverse === void 0) { reverse = false; }
713
- var currentIndex = reverse ? this.size - 1 : 0;
714
- var modifier = reverse ? -1 : 1;
715
- var nextNode = reverse ? 'prev' : 'next';
716
- var currentElement = reverse ? this.tail : this.head;
717
- var result;
718
- if (start !== undefined) {
719
- result = start;
720
- }
721
- else if (currentElement) {
722
- result = currentElement.val;
723
- currentElement = currentElement[nextNode];
724
- }
725
- else {
726
- throw new TypeError('Reduce of empty LinkedList with no initial value');
727
- }
728
- while (currentElement) {
729
- result = callbackFn(result, currentElement.val, currentIndex, this);
730
- currentIndex += modifier;
731
- currentElement = currentElement[nextNode];
415
+ current = current.next;
416
+ }
417
+ return null;
418
+ };
419
+ /**
420
+ * The `insertBefore` function inserts a new value before an existing value in a singly linked list.
421
+ * @param {T} existingValue - The existing value is the value that already exists in the linked list and before which
422
+ * we want to insert a new value.
423
+ * @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
424
+ * @returns The `insertBefore` function returns a boolean value. It returns `true` if the `newValue` is successfully
425
+ * inserted before the first occurrence of `existingValue` in the linked list. It returns `false` if the
426
+ * `existingValue` is not found in the linked list.
427
+ */
428
+ SinglyLinkedList.prototype.insertBefore = function (existingValue, newValue) {
429
+ if (!this.head) {
430
+ return false;
431
+ }
432
+ if (this.head.val === existingValue) {
433
+ this.unshift(newValue);
434
+ return true;
435
+ }
436
+ var current = this.head;
437
+ while (current.next) {
438
+ if (current.next.val === existingValue) {
439
+ var newNode = new SinglyLinkedListNode(newValue);
440
+ newNode.next = current.next;
441
+ current.next = newNode;
442
+ this.length++;
443
+ return true;
444
+ }
445
+ current = current.next;
446
+ }
447
+ return false;
448
+ };
449
+ /**
450
+ * The function inserts a new value after an existing value in a singly linked list.
451
+ * @param {T} existingValue - The existing value is the value of the node after which we want to insert the new value.
452
+ * @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the linked list
453
+ * after the node with the `existingValue`.
454
+ * @returns The method is returning a boolean value. It returns true if the insertion is successful and false if the
455
+ * existing value is not found in the linked list.
456
+ */
457
+ SinglyLinkedList.prototype.insertAfter = function (existingValue, newValue) {
458
+ var existingNode = this.findNode(existingValue);
459
+ if (existingNode) {
460
+ var newNode = new SinglyLinkedListNode(newValue);
461
+ newNode.next = existingNode.next;
462
+ existingNode.next = newNode;
463
+ if (existingNode === this.tail) {
464
+ this.tail = newNode;
465
+ }
466
+ this.length++;
467
+ return true;
732
468
  }
733
- return result;
469
+ return false;
734
470
  };
735
471
  /**
736
- * The toArray() function converts a NodeVal object into an array of NodeVal objects.
737
- * @returns An array of NodeVal objects.
472
+ * The function counts the number of occurrences of a given value in a linked list.
473
+ * @param {T} value - The value parameter is the value that you want to count the occurrences of in the linked list.
474
+ * @returns The count of occurrences of the given value in the linked list.
738
475
  */
739
- SinglyLinkedList.prototype.toArray = function () {
740
- return __spreadArray([], __read(this), false);
741
- };
742
- /**
743
- * The `toString` function takes an optional separator and returns a string representation of an array, with each
744
- * element separated by the specified separator.
745
- * @param [separator= ] - The separator parameter is a string that specifies the character(s) to be used as a separator
746
- * between each element in the array when converting it to a string. By default, the separator is set to a space
747
- * character (' ').
748
- * @returns The toString method is being returned as a string.
749
- */
750
- SinglyLinkedList.prototype.toString = function (separator) {
751
- if (separator === void 0) { separator = ' '; }
752
- return this.reduce(function (s, val) { return "".concat(s).concat(separator).concat(val); });
753
- };
754
- /**
755
- * The function is an iterator that returns the values of each node in a linked list.
756
- */
757
- SinglyLinkedList.prototype[Symbol.iterator] = function () {
758
- var element;
759
- return __generator(this, function (_a) {
760
- switch (_a.label) {
761
- case 0:
762
- element = this.head;
763
- _a.label = 1;
764
- case 1:
765
- if (!(element !== null)) return [3 /*break*/, 3];
766
- return [4 /*yield*/, element.val];
767
- case 2:
768
- _a.sent();
769
- element = element.next;
770
- return [3 /*break*/, 1];
771
- case 3: return [2 /*return*/];
476
+ SinglyLinkedList.prototype.countOccurrences = function (value) {
477
+ var count = 0;
478
+ var current = this.head;
479
+ while (current) {
480
+ if (current.val === value) {
481
+ count++;
772
482
  }
773
- });
774
- };
775
- /**
776
- * The function removes a node from either end of a singly linked list and returns its value.
777
- * @param {SinglyLinkedListNode<NodeVal> | null} node - The `node` parameter is a reference to a node in a singly
778
- * linked list. It can be either a `SinglyLinkedListNode` object or `null`.
779
- * @returns The value of the removed node if the node is not null, otherwise undefined.
780
- */
781
- SinglyLinkedList.prototype.removeFromAnyEnd = function (node) {
782
- return node !== null ? this.removeNode(node).val : undefined;
483
+ current = current.next;
484
+ }
485
+ return count;
783
486
  };
784
487
  return SinglyLinkedList;
785
488
  }());