data-structure-typed 2.5.0 → 2.5.2

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 (246) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +5 -1
  7. package/README.md +124 -29
  8. package/dist/cjs/binary-tree.cjs +26282 -0
  9. package/dist/cjs/graph.cjs +5422 -0
  10. package/dist/cjs/hash.cjs +1310 -0
  11. package/dist/cjs/heap.cjs +1602 -0
  12. package/dist/cjs/index.cjs +31257 -14673
  13. package/dist/cjs/linked-list.cjs +4576 -0
  14. package/dist/cjs/matrix.cjs +1080 -0
  15. package/dist/cjs/priority-queue.cjs +1376 -0
  16. package/dist/cjs/queue.cjs +4264 -0
  17. package/dist/cjs/stack.cjs +907 -0
  18. package/dist/cjs/trie.cjs +1223 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +26319 -0
  20. package/dist/cjs-legacy/graph.cjs +5420 -0
  21. package/dist/cjs-legacy/hash.cjs +1310 -0
  22. package/dist/cjs-legacy/heap.cjs +1599 -0
  23. package/dist/cjs-legacy/index.cjs +31268 -14679
  24. package/dist/cjs-legacy/linked-list.cjs +4582 -0
  25. package/dist/cjs-legacy/matrix.cjs +1083 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1374 -0
  27. package/dist/cjs-legacy/queue.cjs +4262 -0
  28. package/dist/cjs-legacy/stack.cjs +907 -0
  29. package/dist/cjs-legacy/trie.cjs +1222 -0
  30. package/dist/esm/binary-tree.mjs +26267 -0
  31. package/dist/esm/graph.mjs +5409 -0
  32. package/dist/esm/hash.mjs +1307 -0
  33. package/dist/esm/heap.mjs +1596 -0
  34. package/dist/esm/index.mjs +31254 -14674
  35. package/dist/esm/linked-list.mjs +4569 -0
  36. package/dist/esm/matrix.mjs +1076 -0
  37. package/dist/esm/priority-queue.mjs +1372 -0
  38. package/dist/esm/queue.mjs +4260 -0
  39. package/dist/esm/stack.mjs +905 -0
  40. package/dist/esm/trie.mjs +1220 -0
  41. package/dist/esm-legacy/binary-tree.mjs +26304 -0
  42. package/dist/esm-legacy/graph.mjs +5407 -0
  43. package/dist/esm-legacy/hash.mjs +1307 -0
  44. package/dist/esm-legacy/heap.mjs +1593 -0
  45. package/dist/esm-legacy/index.mjs +31265 -14680
  46. package/dist/esm-legacy/linked-list.mjs +4575 -0
  47. package/dist/esm-legacy/matrix.mjs +1079 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1370 -0
  49. package/dist/esm-legacy/queue.mjs +4258 -0
  50. package/dist/esm-legacy/stack.mjs +905 -0
  51. package/dist/esm-legacy/trie.mjs +1219 -0
  52. package/dist/types/common/error.d.ts +9 -0
  53. package/dist/types/common/index.d.ts +1 -1
  54. package/dist/types/data-structures/base/index.d.ts +1 -0
  55. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  56. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  57. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  61. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  62. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  63. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  64. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  65. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  66. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  70. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  71. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  72. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  73. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  74. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  75. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  76. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  77. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  78. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  79. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  80. package/dist/types/interfaces/graph.d.ts +1 -1
  81. package/dist/types/types/common.d.ts +2 -2
  82. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  83. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  84. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  85. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  86. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  87. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  88. package/dist/types/types/utils/validate-type.d.ts +4 -4
  89. package/dist/umd/data-structure-typed.js +31196 -14608
  90. package/dist/umd/data-structure-typed.min.js +11 -5
  91. package/docs-site-docusaurus/README.md +41 -0
  92. package/docs-site-docusaurus/docs/api/README.md +52 -0
  93. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6644 -0
  94. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  95. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  96. package/docs-site-docusaurus/docs/api/classes/BST.md +6293 -0
  97. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  98. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  99. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  100. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  101. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  102. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  103. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  104. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  105. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  106. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  107. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  108. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  109. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  110. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  111. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  112. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  113. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  116. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  117. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  118. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  119. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  120. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  121. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  122. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  123. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  124. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  125. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6888 -0
  126. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  127. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  128. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  129. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  130. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  131. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1389 -0
  132. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1591 -0
  133. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1246 -0
  134. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  135. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  136. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  137. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  138. package/docs-site-docusaurus/docs/guide/architecture.md +615 -0
  139. package/docs-site-docusaurus/docs/guide/concepts.md +451 -0
  140. package/docs-site-docusaurus/docs/guide/faq.md +180 -0
  141. package/docs-site-docusaurus/docs/guide/guides.md +597 -0
  142. package/docs-site-docusaurus/docs/guide/installation.md +62 -0
  143. package/docs-site-docusaurus/docs/guide/integrations.md +825 -0
  144. package/docs-site-docusaurus/docs/guide/overview.md +645 -0
  145. package/docs-site-docusaurus/docs/guide/performance.md +835 -0
  146. package/docs-site-docusaurus/docs/guide/quick-start.md +104 -0
  147. package/docs-site-docusaurus/docs/guide/use-cases/_category_.json +6 -0
  148. package/docs-site-docusaurus/docs/guide/use-cases/array-sort-alternative.md +158 -0
  149. package/docs-site-docusaurus/docs/guide/use-cases/heap-vs-sorting.md +92 -0
  150. package/docs-site-docusaurus/docs/guide/use-cases/map-vs-treemap.md +151 -0
  151. package/docs-site-docusaurus/docs/guide/use-cases/priority-queue-typescript.md +113 -0
  152. package/docs-site-docusaurus/docs/guide/use-cases/treemap-javascript.md +151 -0
  153. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  154. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  155. package/docs-site-docusaurus/package-lock.json +18667 -0
  156. package/docs-site-docusaurus/package.json +50 -0
  157. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  158. package/docs-site-docusaurus/sidebars.ts +23 -0
  159. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  160. package/docs-site-docusaurus/src/css/custom.css +96 -0
  161. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  162. package/docs-site-docusaurus/src/pages/index.tsx +120 -0
  163. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  164. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  165. package/docs-site-docusaurus/static/.nojekyll +0 -0
  166. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  167. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  168. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  169. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  170. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  171. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  172. package/docs-site-docusaurus/static/img/logo.png +0 -0
  173. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  174. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  175. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  176. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  177. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  178. package/docs-site-docusaurus/static/llms.txt +37 -0
  179. package/docs-site-docusaurus/static/robots.txt +4 -0
  180. package/docs-site-docusaurus/typedoc.json +23 -0
  181. package/llms.txt +37 -0
  182. package/package.json +159 -55
  183. package/src/common/error.ts +19 -1
  184. package/src/common/index.ts +1 -1
  185. package/src/data-structures/base/index.ts +1 -0
  186. package/src/data-structures/base/iterable-element-base.ts +3 -2
  187. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  188. package/src/data-structures/base/linear-base.ts +3 -3
  189. package/src/data-structures/binary-tree/avl-tree.ts +287 -0
  190. package/src/data-structures/binary-tree/binary-indexed-tree.ts +327 -5
  191. package/src/data-structures/binary-tree/binary-tree.ts +581 -6
  192. package/src/data-structures/binary-tree/bst.ts +922 -7
  193. package/src/data-structures/binary-tree/red-black-tree.ts +453 -0
  194. package/src/data-structures/binary-tree/segment-tree.ts +139 -2
  195. package/src/data-structures/binary-tree/tree-map.ts +3300 -495
  196. package/src/data-structures/binary-tree/tree-multi-map.ts +3384 -563
  197. package/src/data-structures/binary-tree/tree-multi-set.ts +2757 -493
  198. package/src/data-structures/binary-tree/tree-set.ts +3122 -440
  199. package/src/data-structures/graph/abstract-graph.ts +6 -6
  200. package/src/data-structures/graph/directed-graph.ts +230 -0
  201. package/src/data-structures/graph/undirected-graph.ts +207 -0
  202. package/src/data-structures/hash/hash-map.ts +270 -19
  203. package/src/data-structures/heap/heap.ts +326 -4
  204. package/src/data-structures/heap/max-heap.ts +2 -2
  205. package/src/data-structures/linked-list/doubly-linked-list.ts +394 -3
  206. package/src/data-structures/linked-list/singly-linked-list.ts +348 -3
  207. package/src/data-structures/linked-list/skip-linked-list.ts +421 -7
  208. package/src/data-structures/matrix/matrix.ts +194 -10
  209. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  210. package/src/data-structures/queue/deque.ts +350 -5
  211. package/src/data-structures/queue/queue.ts +276 -0
  212. package/src/data-structures/stack/stack.ts +230 -0
  213. package/src/data-structures/trie/trie.ts +283 -7
  214. package/src/interfaces/graph.ts +1 -1
  215. package/src/types/common.ts +2 -2
  216. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  217. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  218. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  219. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  220. package/src/types/data-structures/heap/heap.ts +1 -0
  221. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  222. package/src/types/utils/validate-type.ts +4 -4
  223. package/vercel.json +6 -0
  224. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  225. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  226. package/dist/leetcode/avl-tree.mjs +0 -2720
  227. package/dist/leetcode/binary-tree.mjs +0 -1594
  228. package/dist/leetcode/bst.mjs +0 -2398
  229. package/dist/leetcode/deque.mjs +0 -683
  230. package/dist/leetcode/directed-graph.mjs +0 -1733
  231. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  232. package/dist/leetcode/hash-map.mjs +0 -493
  233. package/dist/leetcode/heap.mjs +0 -542
  234. package/dist/leetcode/max-heap.mjs +0 -375
  235. package/dist/leetcode/max-priority-queue.mjs +0 -383
  236. package/dist/leetcode/min-heap.mjs +0 -363
  237. package/dist/leetcode/min-priority-queue.mjs +0 -371
  238. package/dist/leetcode/priority-queue.mjs +0 -363
  239. package/dist/leetcode/queue.mjs +0 -943
  240. package/dist/leetcode/red-black-tree.mjs +0 -2765
  241. package/dist/leetcode/singly-linked-list.mjs +0 -754
  242. package/dist/leetcode/stack.mjs +0 -217
  243. package/dist/leetcode/tree-counter.mjs +0 -3039
  244. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  245. package/dist/leetcode/trie.mjs +0 -413
  246. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,1573 @@
1
+ [**data-structure-typed**](../README.md)
2
+
3
+ ***
4
+
5
+ [data-structure-typed](../README.md) / Stack
6
+
7
+ # Class: Stack\<E, R\>
8
+
9
+ Defined in: [data-structures/stack/stack.ts:135](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L135)
10
+
11
+ LIFO stack with array storage and optional record→element conversion.
12
+
13
+ ## Remarks
14
+
15
+ Time O(1), Space O(1)
16
+
17
+ ## Examples
18
+
19
+ ```ts
20
+ // Function Call Stack
21
+ const functionStack = new Stack<string>();
22
+ functionStack.push('main');
23
+ functionStack.push('foo');
24
+ functionStack.push('bar');
25
+ console.log(functionStack.pop()); // 'bar';
26
+ console.log(functionStack.pop()); // 'foo';
27
+ console.log(functionStack.pop()); // 'main';
28
+ ```
29
+
30
+ ```ts
31
+ // Balanced Parentheses or Brackets
32
+ type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
33
+
34
+ const stack = new Stack<string>();
35
+ const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
36
+ const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
37
+ for (const char of input) {
38
+ if ('([{'.includes(char)) {
39
+ stack.push(char);
40
+ } else if (')]}'.includes(char)) {
41
+ if (stack.pop() !== matches[char]) {
42
+ fail('Parentheses are not balanced');
43
+ }
44
+ }
45
+ }
46
+ console.log(stack.isEmpty()); // true;
47
+ ```
48
+
49
+ ```ts
50
+ // Expression Evaluation and Conversion
51
+ const stack = new Stack<number>();
52
+ const expression = [5, 3, '+']; // Equivalent to 5 + 3
53
+ expression.forEach(token => {
54
+ if (typeof token === 'number') {
55
+ stack.push(token);
56
+ } else {
57
+ const b = stack.pop()!;
58
+ const a = stack.pop()!;
59
+ stack.push(token === '+' ? a + b : 0); // Only handling '+' here
60
+ }
61
+ });
62
+ console.log(stack.pop()); // 8;
63
+ ```
64
+
65
+ ```ts
66
+ // Backtracking Algorithms
67
+ const stack = new Stack<[number, number]>();
68
+ const maze = [
69
+ ['S', ' ', 'X'],
70
+ ['X', ' ', 'X'],
71
+ [' ', ' ', 'E']
72
+ ];
73
+ const start: [number, number] = [0, 0];
74
+ const end = [2, 2];
75
+ const directions = [
76
+ [0, 1], // To the right
77
+ [1, 0], // down
78
+ [0, -1], // left
79
+ [-1, 0] // up
80
+ ];
81
+
82
+ const visited = new Set<string>(); // Used to record visited nodes
83
+ stack.push(start);
84
+ const path: number[][] = [];
85
+
86
+ while (!stack.isEmpty()) {
87
+ const [x, y] = stack.pop()!;
88
+ if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
89
+ visited.add(`${x},${y}`);
90
+
91
+ path.push([x, y]);
92
+
93
+ if (x === end[0] && y === end[1]) {
94
+ break; // Find the end point and exit
95
+ }
96
+
97
+ for (const [dx, dy] of directions) {
98
+ const nx = x + dx;
99
+ const ny = y + dy;
100
+ if (
101
+ maze[nx]?.[ny] === ' ' || // feasible path
102
+ maze[nx]?.[ny] === 'E' // destination
103
+ ) {
104
+ stack.push([nx, ny]);
105
+ }
106
+ }
107
+ }
108
+
109
+ console.log(path); // contains end;
110
+ ```
111
+
112
+ ```ts
113
+ // Stock Span Problem
114
+ const stack = new Stack<number>();
115
+ const prices = [100, 80, 60, 70, 60, 75, 85];
116
+ const spans: number[] = [];
117
+ prices.forEach((price, i) => {
118
+ while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
119
+ stack.pop();
120
+ }
121
+ spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
122
+ stack.push(i);
123
+ });
124
+ console.log(spans); // [1, 1, 1, 2, 1, 4, 6];
125
+ ```
126
+
127
+ ```ts
128
+ // Simplify File Paths
129
+ const stack = new Stack<string>();
130
+ const path = '/a/./b/../../c';
131
+ path.split('/').forEach(segment => {
132
+ if (segment === '..') stack.pop();
133
+ else if (segment && segment !== '.') stack.push(segment);
134
+ });
135
+ console.log(stack.elements.join('/')); // 'c';
136
+ ```
137
+
138
+ ```ts
139
+ // Convert stack to array
140
+ const stack = new Stack<number>([1, 2, 3]);
141
+ console.log(stack.toArray()); // [1, 2, 3];
142
+ ```
143
+
144
+ ## Extends
145
+
146
+ - [`IterableElementBase`](IterableElementBase.md)\<`E`, `R`\>
147
+
148
+ ## Type Parameters
149
+
150
+ ### E
151
+
152
+ `E` = `any`
153
+
154
+ ### R
155
+
156
+ `R` = `any`
157
+
158
+ 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
159
+ 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
160
+ 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
161
+ 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
162
+ 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
163
+ 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
164
+
165
+ ## Constructors
166
+
167
+ ### Constructor
168
+
169
+ ```ts
170
+ new Stack<E, R>(elements?, options?): Stack<E, R>;
171
+ ```
172
+
173
+ Defined in: [data-structures/stack/stack.ts:146](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L146)
174
+
175
+ Create a Stack and optionally bulk-push elements.
176
+
177
+ #### Parameters
178
+
179
+ ##### elements?
180
+
181
+ `Iterable`\<`E`, `any`, `any`\> \| `Iterable`\<`R`, `any`, `any`\>
182
+
183
+ Iterable of elements (or raw records if toElementFn is set).
184
+
185
+ ##### options?
186
+
187
+ `StackOptions`\<`E`, `R`\>
188
+
189
+ Options such as toElementFn and equality function.
190
+
191
+ #### Returns
192
+
193
+ `Stack`\<`E`, `R`\>
194
+
195
+ New Stack instance.
196
+
197
+ #### Remarks
198
+
199
+ Time O(N), Space O(N)
200
+
201
+ #### Overrides
202
+
203
+ [`IterableElementBase`](IterableElementBase.md).[`constructor`](IterableElementBase.md#constructor)
204
+
205
+ ## Properties
206
+
207
+ ### elements
208
+
209
+ #### Get Signature
210
+
211
+ ```ts
212
+ get elements(): E[];
213
+ ```
214
+
215
+ Defined in: [data-structures/stack/stack.ts:159](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L159)
216
+
217
+ Get the backing array of elements.
218
+
219
+ ##### Remarks
220
+
221
+ Time O(1), Space O(1)
222
+
223
+ ##### Returns
224
+
225
+ `E`[]
226
+
227
+ Internal elements array.
228
+
229
+ ***
230
+
231
+ ### size
232
+
233
+ #### Get Signature
234
+
235
+ ```ts
236
+ get size(): number;
237
+ ```
238
+
239
+ Defined in: [data-structures/stack/stack.ts:206](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L206)
240
+
241
+ Get the number of stored elements.
242
+
243
+ ##### Remarks
244
+
245
+ Time O(1), Space O(1)
246
+
247
+ ##### Example
248
+
249
+ ```ts
250
+ // Get number of elements
251
+ const stack = new Stack<number>([1, 2, 3]);
252
+ console.log(stack.size); // 3;
253
+ ```
254
+
255
+ ##### Returns
256
+
257
+ `number`
258
+
259
+ Current size.
260
+
261
+ *
262
+
263
+ ***
264
+
265
+ ### toElementFn
266
+
267
+ #### Get Signature
268
+
269
+ ```ts
270
+ get toElementFn(): ((rawElement) => E) | undefined;
271
+ ```
272
+
273
+ Defined in: [data-structures/base/iterable-element-base.ts:48](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L48)
274
+
275
+ Exposes the current `toElementFn`, if configured.
276
+
277
+ ##### Remarks
278
+
279
+ Time O(1), Space O(1).
280
+
281
+ ##### Returns
282
+
283
+ ((`rawElement`) => `E`) \| `undefined`
284
+
285
+ The converter function or `undefined` when not set.
286
+
287
+ #### Inherited from
288
+
289
+ [`IterableElementBase`](IterableElementBase.md).[`toElementFn`](IterableElementBase.md#toelementfn)
290
+
291
+ ## Methods
292
+
293
+ ### \[iterator\]()
294
+
295
+ ```ts
296
+ iterator: IterableIterator<E>;
297
+ ```
298
+
299
+ Defined in: [data-structures/base/iterable-element-base.ts:61](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L61)
300
+
301
+ Returns an iterator over the structure's elements.
302
+
303
+ #### Parameters
304
+
305
+ ##### args
306
+
307
+ ...`unknown`[]
308
+
309
+ Optional iterator arguments forwarded to the internal iterator.
310
+
311
+ #### Returns
312
+
313
+ `IterableIterator`\<`E`\>
314
+
315
+ An `IterableIterator<E>` that yields the elements in traversal order.
316
+
317
+ #### Remarks
318
+
319
+ Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
320
+
321
+ #### Inherited from
322
+
323
+ [`IterableElementBase`](IterableElementBase.md).[`[iterator]`](IterableElementBase.md#iterator)
324
+
325
+ ***
326
+
327
+ ### clear()
328
+
329
+ ```ts
330
+ clear(): void;
331
+ ```
332
+
333
+ Defined in: [data-structures/stack/stack.ts:593](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L593)
334
+
335
+ Remove all elements and reset storage.
336
+
337
+ #### Returns
338
+
339
+ `void`
340
+
341
+ void
342
+
343
+ *
344
+
345
+ #### Remarks
346
+
347
+ Time O(1), Space O(1)
348
+
349
+ #### Example
350
+
351
+ ```ts
352
+ // Remove all elements
353
+ const stack = new Stack<number>([1, 2, 3]);
354
+ stack.clear();
355
+ console.log(stack.isEmpty()); // true;
356
+ ```
357
+
358
+ #### Overrides
359
+
360
+ [`IterableElementBase`](IterableElementBase.md).[`clear`](IterableElementBase.md#clear)
361
+
362
+ ***
363
+
364
+ ### clone()
365
+
366
+ ```ts
367
+ clone(): this;
368
+ ```
369
+
370
+ Defined in: [data-structures/stack/stack.ts:643](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L643)
371
+
372
+ Deep clone this stack.
373
+
374
+ #### Returns
375
+
376
+ `this`
377
+
378
+ A new stack with the same content.
379
+
380
+ *
381
+
382
+ #### Remarks
383
+
384
+ Time O(N), Space O(N)
385
+
386
+ #### Example
387
+
388
+ ```ts
389
+ // Create independent copy
390
+ const stack = new Stack<number>([1, 2, 3]);
391
+ const copy = stack.clone();
392
+ copy.pop();
393
+ console.log(stack.size); // 3;
394
+ console.log(copy.size); // 2;
395
+ ```
396
+
397
+ #### Overrides
398
+
399
+ [`IterableElementBase`](IterableElementBase.md).[`clone`](IterableElementBase.md#clone)
400
+
401
+ ***
402
+
403
+ ### delete()
404
+
405
+ ```ts
406
+ delete(element): boolean;
407
+ ```
408
+
409
+ Defined in: [data-structures/stack/stack.ts:514](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L514)
410
+
411
+ Delete the first occurrence of a specific element.
412
+
413
+ #### Parameters
414
+
415
+ ##### element
416
+
417
+ `E`
418
+
419
+ Element to remove (using the configured equality).
420
+
421
+ #### Returns
422
+
423
+ `boolean`
424
+
425
+ True if an element was removed.
426
+
427
+ *
428
+
429
+ #### Remarks
430
+
431
+ Time O(N), Space O(1)
432
+
433
+ #### Example
434
+
435
+ ```ts
436
+ // Remove element
437
+ const stack = new Stack<number>([1, 2, 3]);
438
+ stack.delete(2);
439
+ console.log(stack.toArray()); // [1, 3];
440
+ ```
441
+
442
+ ***
443
+
444
+ ### deleteAt()
445
+
446
+ ```ts
447
+ deleteAt(index): boolean;
448
+ ```
449
+
450
+ Defined in: [data-structures/stack/stack.ts:526](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L526)
451
+
452
+ Delete the element at an index.
453
+
454
+ #### Parameters
455
+
456
+ ##### index
457
+
458
+ `number`
459
+
460
+ Zero-based index from the bottom.
461
+
462
+ #### Returns
463
+
464
+ `boolean`
465
+
466
+ True if removed.
467
+
468
+ #### Remarks
469
+
470
+ Time O(N), Space O(1)
471
+
472
+ ***
473
+
474
+ ### deleteWhere()
475
+
476
+ ```ts
477
+ deleteWhere(predicate): boolean;
478
+ ```
479
+
480
+ Defined in: [data-structures/stack/stack.ts:539](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L539)
481
+
482
+ Delete the first element that satisfies a predicate.
483
+
484
+ #### Parameters
485
+
486
+ ##### predicate
487
+
488
+ (`value`, `index`, `stack`) => `boolean`
489
+
490
+ Function (value, index, stack) → boolean to decide deletion.
491
+
492
+ #### Returns
493
+
494
+ `boolean`
495
+
496
+ True if a match was removed.
497
+
498
+ #### Remarks
499
+
500
+ Time O(N), Space O(1)
501
+
502
+ ***
503
+
504
+ ### every()
505
+
506
+ ```ts
507
+ every(predicate, thisArg?): boolean;
508
+ ```
509
+
510
+ Defined in: [data-structures/base/iterable-element-base.ts:87](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L87)
511
+
512
+ Tests whether all elements satisfy the predicate.
513
+
514
+ #### Parameters
515
+
516
+ ##### predicate
517
+
518
+ `ElementCallback`\<`E`, `R`, `boolean`\>
519
+
520
+ Function invoked for each element with signature `(value, index, self)`.
521
+
522
+ ##### thisArg?
523
+
524
+ `unknown`
525
+
526
+ Optional `this` binding for the predicate.
527
+
528
+ #### Returns
529
+
530
+ `boolean`
531
+
532
+ `true` if every element passes; otherwise `false`.
533
+
534
+ #### Remarks
535
+
536
+ Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
537
+
538
+ #### Inherited from
539
+
540
+ [`IterableElementBase`](IterableElementBase.md).[`every`](IterableElementBase.md#every)
541
+
542
+ ***
543
+
544
+ ### filter()
545
+
546
+ ```ts
547
+ filter(predicate, thisArg?): this;
548
+ ```
549
+
550
+ Defined in: [data-structures/stack/stack.ts:695](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L695)
551
+
552
+ Filter elements into a new stack of the same class.
553
+
554
+ #### Parameters
555
+
556
+ ##### predicate
557
+
558
+ `ElementCallback`\<`E`, `R`, `boolean`\>
559
+
560
+ Predicate (value, index, stack) → boolean to keep value.
561
+
562
+ ##### thisArg?
563
+
564
+ `unknown`
565
+
566
+ Value for `this` inside the predicate.
567
+
568
+ #### Returns
569
+
570
+ `this`
571
+
572
+ A new stack with kept values.
573
+
574
+ *
575
+
576
+ #### Remarks
577
+
578
+ Time O(N), Space O(N)
579
+
580
+ #### Example
581
+
582
+ ```ts
583
+ // Filter elements
584
+ const stack = new Stack<number>([1, 2, 3, 4, 5]);
585
+ const evens = stack.filter(x => x % 2 === 0);
586
+ console.log(evens.toArray()); // [2, 4];
587
+ ```
588
+
589
+ #### Overrides
590
+
591
+ [`IterableElementBase`](IterableElementBase.md).[`filter`](IterableElementBase.md#filter)
592
+
593
+ ***
594
+
595
+ ### find()
596
+
597
+ #### Call Signature
598
+
599
+ ```ts
600
+ find<S>(predicate, thisArg?): S | undefined;
601
+ ```
602
+
603
+ Defined in: [data-structures/base/iterable-element-base.ts:163](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L163)
604
+
605
+ Finds the first element that satisfies the predicate and returns it.
606
+
607
+ Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
608
+
609
+ ##### Type Parameters
610
+
611
+ ###### S
612
+
613
+ `S`
614
+
615
+ ##### Parameters
616
+
617
+ ###### predicate
618
+
619
+ `ElementCallback`\<`E`, `R`, `S`\>
620
+
621
+ Type-guard predicate: `(value, index, self) => value is S`.
622
+
623
+ ###### thisArg?
624
+
625
+ `unknown`
626
+
627
+ Optional `this` binding for the predicate.
628
+
629
+ ##### Returns
630
+
631
+ `S` \| `undefined`
632
+
633
+ The matched element typed as `S`, or `undefined` if not found.
634
+
635
+ ##### Remarks
636
+
637
+ Time O(n) in the worst case; may exit early on the first match. Space O(1).
638
+
639
+ ##### Inherited from
640
+
641
+ [`IterableElementBase`](IterableElementBase.md).[`find`](IterableElementBase.md#find)
642
+
643
+ #### Call Signature
644
+
645
+ ```ts
646
+ find(predicate, thisArg?): E | undefined;
647
+ ```
648
+
649
+ Defined in: [data-structures/base/iterable-element-base.ts:164](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L164)
650
+
651
+ Finds the first element that satisfies the predicate and returns it.
652
+
653
+ Finds the first element of type `S` (a subtype of `E`) that satisfies the predicate and returns it.
654
+
655
+ ##### Parameters
656
+
657
+ ###### predicate
658
+
659
+ `ElementCallback`\<`E`, `R`, `unknown`\>
660
+
661
+ Type-guard predicate: `(value, index, self) => value is S`.
662
+
663
+ ###### thisArg?
664
+
665
+ `unknown`
666
+
667
+ Optional `this` binding for the predicate.
668
+
669
+ ##### Returns
670
+
671
+ `E` \| `undefined`
672
+
673
+ The matched element typed as `S`, or `undefined` if not found.
674
+
675
+ ##### Remarks
676
+
677
+ Time O(n) in the worst case; may exit early on the first match. Space O(1).
678
+
679
+ ##### Inherited from
680
+
681
+ [`IterableElementBase`](IterableElementBase.md).[`find`](IterableElementBase.md#find)
682
+
683
+ ***
684
+
685
+ ### forEach()
686
+
687
+ ```ts
688
+ forEach(callbackfn, thisArg?): void;
689
+ ```
690
+
691
+ Defined in: [data-structures/base/iterable-element-base.ts:133](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L133)
692
+
693
+ Invokes a callback for each element in iteration order.
694
+
695
+ #### Parameters
696
+
697
+ ##### callbackfn
698
+
699
+ `ElementCallback`\<`E`, `R`, `void`\>
700
+
701
+ Function invoked per element with signature `(value, index, self)`.
702
+
703
+ ##### thisArg?
704
+
705
+ `unknown`
706
+
707
+ Optional `this` binding for the callback.
708
+
709
+ #### Returns
710
+
711
+ `void`
712
+
713
+ `void`.
714
+
715
+ #### Remarks
716
+
717
+ Time O(n), Space O(1).
718
+
719
+ #### Inherited from
720
+
721
+ [`IterableElementBase`](IterableElementBase.md).[`forEach`](IterableElementBase.md#foreach)
722
+
723
+ ***
724
+
725
+ ### has()
726
+
727
+ ```ts
728
+ has(element): boolean;
729
+ ```
730
+
731
+ Defined in: [data-structures/base/iterable-element-base.ts:189](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L189)
732
+
733
+ Checks whether a strictly-equal element exists in the structure.
734
+
735
+ #### Parameters
736
+
737
+ ##### element
738
+
739
+ `E`
740
+
741
+ The element to test with `===` equality.
742
+
743
+ #### Returns
744
+
745
+ `boolean`
746
+
747
+ `true` if an equal element is found; otherwise `false`.
748
+
749
+ #### Remarks
750
+
751
+ Time O(n) in the worst case. Space O(1).
752
+
753
+ #### Inherited from
754
+
755
+ [`IterableElementBase`](IterableElementBase.md).[`has`](IterableElementBase.md#has)
756
+
757
+ ***
758
+
759
+ ### isEmpty()
760
+
761
+ ```ts
762
+ isEmpty(): boolean;
763
+ ```
764
+
765
+ Defined in: [data-structures/stack/stack.ts:276](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L276)
766
+
767
+ Check whether the stack is empty.
768
+
769
+ #### Returns
770
+
771
+ `boolean`
772
+
773
+ True if size is 0.
774
+
775
+ *
776
+
777
+ #### Remarks
778
+
779
+ Time O(1), Space O(1)
780
+
781
+ #### Example
782
+
783
+ ```ts
784
+ // Check if stack has elements
785
+ const stack = new Stack<number>();
786
+ console.log(stack.isEmpty()); // true;
787
+ stack.push(1);
788
+ console.log(stack.isEmpty()); // false;
789
+ ```
790
+
791
+ #### Overrides
792
+
793
+ [`IterableElementBase`](IterableElementBase.md).[`isEmpty`](IterableElementBase.md#isempty)
794
+
795
+ ***
796
+
797
+ ### map()
798
+
799
+ ```ts
800
+ map<EM, RM>(
801
+ callback,
802
+ options?,
803
+ thisArg?): Stack<EM, RM>;
804
+ ```
805
+
806
+ Defined in: [data-structures/stack/stack.ts:771](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L771)
807
+
808
+ Map values into a new stack (possibly different element type).
809
+
810
+ #### Type Parameters
811
+
812
+ ##### EM
813
+
814
+ `EM`
815
+
816
+ ##### RM
817
+
818
+ `RM`
819
+
820
+ #### Parameters
821
+
822
+ ##### callback
823
+
824
+ `ElementCallback`\<`E`, `R`, `EM`\>
825
+
826
+ Mapping function (value, index, stack) → newElement.
827
+
828
+ ##### options?
829
+
830
+ `IterableElementBaseOptions`\<`EM`, `RM`\>
831
+
832
+ Options for the output stack (e.g., toElementFn).
833
+
834
+ ##### thisArg?
835
+
836
+ `unknown`
837
+
838
+ Value for `this` inside the callback.
839
+
840
+ #### Returns
841
+
842
+ `Stack`\<`EM`, `RM`\>
843
+
844
+ A new Stack with mapped elements.
845
+
846
+ *
847
+
848
+ #### Remarks
849
+
850
+ Time O(N), Space O(N)
851
+
852
+ #### Example
853
+
854
+ ```ts
855
+ // Transform elements
856
+ const stack = new Stack<number>([1, 2, 3]);
857
+ const doubled = stack.map(x => x * 2);
858
+ console.log(doubled.toArray()); // [2, 4, 6];
859
+ ```
860
+
861
+ #### Overrides
862
+
863
+ [`IterableElementBase`](IterableElementBase.md).[`map`](IterableElementBase.md#map)
864
+
865
+ ***
866
+
867
+ ### mapSame()
868
+
869
+ ```ts
870
+ mapSame(callback, thisArg?): this;
871
+ ```
872
+
873
+ Defined in: [data-structures/stack/stack.ts:713](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L713)
874
+
875
+ Map values into a new stack of the same element type.
876
+
877
+ #### Parameters
878
+
879
+ ##### callback
880
+
881
+ `ElementCallback`\<`E`, `R`, `E`\>
882
+
883
+ Mapping function (value, index, stack) → newValue.
884
+
885
+ ##### thisArg?
886
+
887
+ `unknown`
888
+
889
+ Value for `this` inside the callback.
890
+
891
+ #### Returns
892
+
893
+ `this`
894
+
895
+ A new stack with mapped values.
896
+
897
+ #### Remarks
898
+
899
+ Time O(N), Space O(N)
900
+
901
+ #### Overrides
902
+
903
+ [`IterableElementBase`](IterableElementBase.md).[`mapSame`](IterableElementBase.md#mapsame)
904
+
905
+ ***
906
+
907
+ ### peek()
908
+
909
+ ```ts
910
+ peek(): E | undefined;
911
+ ```
912
+
913
+ Defined in: [data-structures/stack/stack.ts:326](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L326)
914
+
915
+ Get the top element without removing it.
916
+
917
+ #### Returns
918
+
919
+ `E` \| `undefined`
920
+
921
+ Top element or undefined.
922
+
923
+ *
924
+
925
+ #### Remarks
926
+
927
+ Time O(1), Space O(1)
928
+
929
+ #### Example
930
+
931
+ ```ts
932
+ // View the top element without removing it
933
+ const stack = new Stack<string>(['a', 'b', 'c']);
934
+ console.log(stack.peek()); // 'c';
935
+ console.log(stack.size); // 3;
936
+ ```
937
+
938
+ ***
939
+
940
+ ### pop()
941
+
942
+ ```ts
943
+ pop(): E | undefined;
944
+ ```
945
+
946
+ Defined in: [data-structures/stack/stack.ts:450](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L450)
947
+
948
+ Pop and return the top element.
949
+
950
+ #### Returns
951
+
952
+ `E` \| `undefined`
953
+
954
+ Removed element or undefined.
955
+
956
+ *
957
+
958
+ #### Remarks
959
+
960
+ Time O(1), Space O(1)
961
+
962
+ #### Example
963
+
964
+ ```ts
965
+ // Stack pop operation (LIFO - Last In First Out)
966
+ const stack = new Stack<number>([10, 20, 30, 40, 50]);
967
+
968
+ // Peek at the top element without removing
969
+ const top = stack.peek();
970
+ console.log(top); // 50;
971
+
972
+ // Pop removes from the top (LIFO order)
973
+ const popped = stack.pop();
974
+ console.log(popped); // 50;
975
+
976
+ // Next pop gets the previous element
977
+ const next = stack.pop();
978
+ console.log(next); // 40;
979
+
980
+ // Verify length decreased
981
+ console.log(stack.size); // 3;
982
+ ```
983
+
984
+ ***
985
+
986
+ ### print()
987
+
988
+ ```ts
989
+ print(): void;
990
+ ```
991
+
992
+ Defined in: [data-structures/base/iterable-element-base.ts:269](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L269)
993
+
994
+ Prints `toVisual()` to the console. Intended for quick debugging.
995
+
996
+ #### Returns
997
+
998
+ `void`
999
+
1000
+ `void`.
1001
+
1002
+ #### Remarks
1003
+
1004
+ Time O(n) due to materialization, Space O(n) for the intermediate representation.
1005
+
1006
+ #### Inherited from
1007
+
1008
+ [`IterableElementBase`](IterableElementBase.md).[`print`](IterableElementBase.md#print)
1009
+
1010
+ ***
1011
+
1012
+ ### push()
1013
+
1014
+ ```ts
1015
+ push(element): boolean;
1016
+ ```
1017
+
1018
+ Defined in: [data-structures/stack/stack.ts:386](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L386)
1019
+
1020
+ Push one element onto the top.
1021
+
1022
+ #### Parameters
1023
+
1024
+ ##### element
1025
+
1026
+ `E`
1027
+
1028
+ Element to push.
1029
+
1030
+ #### Returns
1031
+
1032
+ `boolean`
1033
+
1034
+ True when pushed.
1035
+
1036
+ *
1037
+
1038
+ #### Remarks
1039
+
1040
+ Time O(1), Space O(1)
1041
+
1042
+ #### Example
1043
+
1044
+ ```ts
1045
+ // basic Stack creation and push operation
1046
+ // Create a simple Stack with initial values
1047
+ const stack = new Stack([1, 2, 3, 4, 5]);
1048
+
1049
+ // Verify the stack maintains insertion order (LIFO will be shown in pop)
1050
+ console.log([...stack]); // [1, 2, 3, 4, 5];
1051
+
1052
+ // Check length
1053
+ console.log(stack.size); // 5;
1054
+
1055
+ // Push a new element to the top
1056
+ stack.push(6);
1057
+ console.log(stack.size); // 6;
1058
+ ```
1059
+
1060
+ ***
1061
+
1062
+ ### pushMany()
1063
+
1064
+ ```ts
1065
+ pushMany(elements): boolean[];
1066
+ ```
1067
+
1068
+ Defined in: [data-structures/stack/stack.ts:461](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L461)
1069
+
1070
+ Push many elements from an iterable.
1071
+
1072
+ #### Parameters
1073
+
1074
+ ##### elements
1075
+
1076
+ `Iterable`\<`E`, `any`, `any`\> \| `Iterable`\<`R`, `any`, `any`\>
1077
+
1078
+ Iterable of elements (or raw records if toElementFn is set).
1079
+
1080
+ #### Returns
1081
+
1082
+ `boolean`[]
1083
+
1084
+ Array of per-element success flags.
1085
+
1086
+ #### Remarks
1087
+
1088
+ Time O(N), Space O(1)
1089
+
1090
+ ***
1091
+
1092
+ ### reduce()
1093
+
1094
+ Reduces all elements to a single accumulated value.
1095
+
1096
+ #### Param
1097
+
1098
+ Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
1099
+
1100
+ #### Param
1101
+
1102
+ Reducer of signature `(acc, value, index, self) => nextAcc`.
1103
+
1104
+ #### Param
1105
+
1106
+ The initial accumulator value of type `E`.
1107
+
1108
+ #### Template
1109
+
1110
+ The accumulator type when it differs from `E`.
1111
+
1112
+ #### Param
1113
+
1114
+ Reducer of signature `(acc: U, value, index, self) => U`.
1115
+
1116
+ #### Param
1117
+
1118
+ The initial accumulator value of type `U`.
1119
+
1120
+ #### Remarks
1121
+
1122
+ Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
1123
+
1124
+ #### Call Signature
1125
+
1126
+ ```ts
1127
+ reduce(callbackfn): E;
1128
+ ```
1129
+
1130
+ Defined in: [data-structures/base/iterable-element-base.ts:194](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L194)
1131
+
1132
+ ##### Parameters
1133
+
1134
+ ###### callbackfn
1135
+
1136
+ `ReduceElementCallback`\<`E`, `R`\>
1137
+
1138
+ ##### Returns
1139
+
1140
+ `E`
1141
+
1142
+ ##### Inherited from
1143
+
1144
+ [`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
1145
+
1146
+ #### Call Signature
1147
+
1148
+ ```ts
1149
+ reduce(callbackfn, initialValue): E;
1150
+ ```
1151
+
1152
+ Defined in: [data-structures/base/iterable-element-base.ts:195](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L195)
1153
+
1154
+ ##### Parameters
1155
+
1156
+ ###### callbackfn
1157
+
1158
+ `ReduceElementCallback`\<`E`, `R`\>
1159
+
1160
+ ###### initialValue
1161
+
1162
+ `E`
1163
+
1164
+ ##### Returns
1165
+
1166
+ `E`
1167
+
1168
+ ##### Inherited from
1169
+
1170
+ [`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
1171
+
1172
+ #### Call Signature
1173
+
1174
+ ```ts
1175
+ reduce<U>(callbackfn, initialValue): U;
1176
+ ```
1177
+
1178
+ Defined in: [data-structures/base/iterable-element-base.ts:196](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L196)
1179
+
1180
+ ##### Type Parameters
1181
+
1182
+ ###### U
1183
+
1184
+ `U`
1185
+
1186
+ ##### Parameters
1187
+
1188
+ ###### callbackfn
1189
+
1190
+ `ReduceElementCallback`\<`E`, `R`, `U`\>
1191
+
1192
+ ###### initialValue
1193
+
1194
+ `U`
1195
+
1196
+ ##### Returns
1197
+
1198
+ `U`
1199
+
1200
+ ##### Inherited from
1201
+
1202
+ [`IterableElementBase`](IterableElementBase.md).[`reduce`](IterableElementBase.md#reduce)
1203
+
1204
+ ***
1205
+
1206
+ ### setEquality()
1207
+
1208
+ ```ts
1209
+ setEquality(equals): this;
1210
+ ```
1211
+
1212
+ Defined in: [data-structures/stack/stack.ts:792](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L792)
1213
+
1214
+ Set the equality comparator used by delete/search operations.
1215
+
1216
+ #### Parameters
1217
+
1218
+ ##### equals
1219
+
1220
+ (`a`, `b`) => `boolean`
1221
+
1222
+ Equality predicate (a, b) → boolean.
1223
+
1224
+ #### Returns
1225
+
1226
+ `this`
1227
+
1228
+ This stack.
1229
+
1230
+ #### Remarks
1231
+
1232
+ Time O(1), Space O(1)
1233
+
1234
+ ***
1235
+
1236
+ ### some()
1237
+
1238
+ ```ts
1239
+ some(predicate, thisArg?): boolean;
1240
+ ```
1241
+
1242
+ Defined in: [data-structures/base/iterable-element-base.ts:110](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L110)
1243
+
1244
+ Tests whether at least one element satisfies the predicate.
1245
+
1246
+ #### Parameters
1247
+
1248
+ ##### predicate
1249
+
1250
+ `ElementCallback`\<`E`, `R`, `boolean`\>
1251
+
1252
+ Function invoked for each element with signature `(value, index, self)`.
1253
+
1254
+ ##### thisArg?
1255
+
1256
+ `unknown`
1257
+
1258
+ Optional `this` binding for the predicate.
1259
+
1260
+ #### Returns
1261
+
1262
+ `boolean`
1263
+
1264
+ `true` if any element passes; otherwise `false`.
1265
+
1266
+ #### Remarks
1267
+
1268
+ Time O(n) in the worst case; may exit early on first success. Space O(1).
1269
+
1270
+ #### Inherited from
1271
+
1272
+ [`IterableElementBase`](IterableElementBase.md).[`some`](IterableElementBase.md#some)
1273
+
1274
+ ***
1275
+
1276
+ ### toArray()
1277
+
1278
+ ```ts
1279
+ toArray(): E[];
1280
+ ```
1281
+
1282
+ Defined in: [data-structures/base/iterable-element-base.ts:246](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L246)
1283
+
1284
+ Materializes the elements into a new array.
1285
+
1286
+ #### Returns
1287
+
1288
+ `E`[]
1289
+
1290
+ A shallow array copy of the iteration order.
1291
+
1292
+ #### Remarks
1293
+
1294
+ Time O(n), Space O(n).
1295
+
1296
+ #### Inherited from
1297
+
1298
+ [`IterableElementBase`](IterableElementBase.md).[`toArray`](IterableElementBase.md#toarray)
1299
+
1300
+ ***
1301
+
1302
+ ### toVisual()
1303
+
1304
+ ```ts
1305
+ toVisual(): E[];
1306
+ ```
1307
+
1308
+ Defined in: [data-structures/base/iterable-element-base.ts:258](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L258)
1309
+
1310
+ Returns a representation of the structure suitable for quick visualization.
1311
+ Defaults to an array of elements; subclasses may override to provide richer visuals.
1312
+
1313
+ #### Returns
1314
+
1315
+ `E`[]
1316
+
1317
+ A visual representation (array by default).
1318
+
1319
+ #### Remarks
1320
+
1321
+ Time O(n), Space O(n).
1322
+
1323
+ #### Inherited from
1324
+
1325
+ [`IterableElementBase`](IterableElementBase.md).[`toVisual`](IterableElementBase.md#tovisual)
1326
+
1327
+ ***
1328
+
1329
+ ### values()
1330
+
1331
+ ```ts
1332
+ values(): IterableIterator<E>;
1333
+ ```
1334
+
1335
+ Defined in: [data-structures/base/iterable-element-base.ts:72](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L72)
1336
+
1337
+ Returns an iterator over the values (alias of the default iterator).
1338
+
1339
+ #### Returns
1340
+
1341
+ `IterableIterator`\<`E`\>
1342
+
1343
+ An `IterableIterator<E>` over all elements.
1344
+
1345
+ #### Remarks
1346
+
1347
+ Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
1348
+
1349
+ #### Inherited from
1350
+
1351
+ [`IterableElementBase`](IterableElementBase.md).[`values`](IterableElementBase.md#values)
1352
+
1353
+ ***
1354
+
1355
+ ### fromArray()
1356
+
1357
+ ```ts
1358
+ static fromArray<E, R>(
1359
+ this,
1360
+ elements,
1361
+ options?): any;
1362
+ ```
1363
+
1364
+ Defined in: [data-structures/stack/stack.ts:221](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L221)
1365
+
1366
+ Create a stack from an array of elements.
1367
+
1368
+ #### Type Parameters
1369
+
1370
+ ##### E
1371
+
1372
+ `E`
1373
+
1374
+ ##### R
1375
+
1376
+ `R` = `any`
1377
+
1378
+ #### Parameters
1379
+
1380
+ ##### this
1381
+
1382
+ `Object`
1383
+
1384
+ The constructor (subclass) to instantiate.
1385
+
1386
+ ##### elements
1387
+
1388
+ `E`[]
1389
+
1390
+ Array of elements to push in order.
1391
+
1392
+ ##### options?
1393
+
1394
+ `StackOptions`\<`E`, `R`\>
1395
+
1396
+ Options forwarded to the constructor.
1397
+
1398
+ #### Returns
1399
+
1400
+ `any`
1401
+
1402
+ A new Stack populated from the array.
1403
+
1404
+ #### Remarks
1405
+
1406
+ Time O(N), Space O(N)
1407
+
1408
+
1409
+ ---
1410
+
1411
+ ## Protected Members
1412
+
1413
+ ### \_toElementFn?
1414
+
1415
+ ```ts
1416
+ protected optional _toElementFn?: (rawElement) => E;
1417
+ ```
1418
+
1419
+ Defined in: [data-structures/base/iterable-element-base.ts:39](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/base/iterable-element-base.ts#L39)
1420
+
1421
+ The converter used to transform a raw element (`R`) into a public element (`E`).
1422
+
1423
+ #### Parameters
1424
+
1425
+ ##### rawElement
1426
+
1427
+ `R`
1428
+
1429
+ #### Returns
1430
+
1431
+ `E`
1432
+
1433
+ #### Remarks
1434
+
1435
+ Time O(1), Space O(1).
1436
+
1437
+ #### Inherited from
1438
+
1439
+ [`IterableElementBase`](IterableElementBase.md).[`_toElementFn`](IterableElementBase.md#_toelementfn)
1440
+
1441
+ ## Accessors
1442
+
1443
+ ### \_createInstance()
1444
+
1445
+ ```ts
1446
+ protected _createInstance(options?): this;
1447
+ ```
1448
+
1449
+ Defined in: [data-structures/stack/stack.ts:816](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L816)
1450
+
1451
+ (Protected) Create an empty instance of the same concrete class.
1452
+
1453
+ #### Parameters
1454
+
1455
+ ##### options?
1456
+
1457
+ `StackOptions`\<`E`, `R`\>
1458
+
1459
+ Options forwarded to the constructor.
1460
+
1461
+ #### Returns
1462
+
1463
+ `this`
1464
+
1465
+ An empty like-kind stack instance.
1466
+
1467
+ #### Remarks
1468
+
1469
+ Time O(1), Space O(1)
1470
+
1471
+ ***
1472
+
1473
+ ### \_createLike()
1474
+
1475
+ ```ts
1476
+ protected _createLike<T, RR>(elements?, options?): Stack<T, RR>;
1477
+ ```
1478
+
1479
+ Defined in: [data-structures/stack/stack.ts:831](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L831)
1480
+
1481
+ (Protected) Create a like-kind stack and seed it from an iterable.
1482
+
1483
+ #### Type Parameters
1484
+
1485
+ ##### T
1486
+
1487
+ `T` = `E`
1488
+
1489
+ ##### RR
1490
+
1491
+ `RR` = `R`
1492
+
1493
+ #### Parameters
1494
+
1495
+ ##### elements?
1496
+
1497
+ `Iterable`\<`T`, `any`, `any`\> \| `Iterable`\<`RR`, `any`, `any`\>
1498
+
1499
+ Iterable used to seed the new stack.
1500
+
1501
+ ##### options?
1502
+
1503
+ `StackOptions`\<`T`, `RR`\>
1504
+
1505
+ Options forwarded to the constructor.
1506
+
1507
+ #### Returns
1508
+
1509
+ `Stack`\<`T`, `RR`\>
1510
+
1511
+ A like-kind Stack instance.
1512
+
1513
+ #### Remarks
1514
+
1515
+ Time O(N), Space O(N)
1516
+
1517
+ ***
1518
+
1519
+ ### \_getIterator()
1520
+
1521
+ ```ts
1522
+ protected _getIterator(): IterableIterator<E>;
1523
+ ```
1524
+
1525
+ Defined in: [data-structures/stack/stack.ts:848](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L848)
1526
+
1527
+ (Protected) Iterate elements from bottom to top.
1528
+
1529
+ #### Returns
1530
+
1531
+ `IterableIterator`\<`E`\>
1532
+
1533
+ Iterator of elements.
1534
+
1535
+ #### Remarks
1536
+
1537
+ Time O(N), Space O(1)
1538
+
1539
+ #### Overrides
1540
+
1541
+ [`IterableElementBase`](IterableElementBase.md).[`_getIterator`](IterableElementBase.md#_getiterator)
1542
+
1543
+ ***
1544
+
1545
+ ### \_indexOfByEquals()
1546
+
1547
+ ```ts
1548
+ protected _indexOfByEquals(target): number;
1549
+ ```
1550
+
1551
+ Defined in: [data-structures/stack/stack.ts:804](https://github.com/zrwusa/data-structure-typed/blob/8292cb978daf85ebe846186c7c7572aece04fb7b/src/data-structures/stack/stack.ts#L804)
1552
+
1553
+ (Protected) Find the index of a target element using the equality function.
1554
+
1555
+ #### Parameters
1556
+
1557
+ ##### target
1558
+
1559
+ `E`
1560
+
1561
+ Element to search for.
1562
+
1563
+ #### Returns
1564
+
1565
+ `number`
1566
+
1567
+ Index or -1 if not found.
1568
+
1569
+ #### Remarks
1570
+
1571
+ Time O(N), Space O(1)
1572
+
1573
+ ***