Stacks
Stacks provide LIFO semantics via either an array-backed implementation (Stack) or a node-based one (LinkedStack). Both honor IStack and expose iterators plus reverseIterator.
Stack
- Backed by an array.
- Supports
push,pop,peek,clear,contains, iteration, andreverseIterator. - Constructor accepts an
Iterableto seed contents.
ts
const stack = new Stack<number>([1, 2])
stack.push(3)
console.log(stack.pop()) // 3LinkedStack
- Uses linked nodes to achieve O(1) push/pop without array reallocations.
- Provides the same public API as
Stackplus access tofirstfor diagnostics.
ts
import { LinkedStack } from '@avensio/shared'
const linked = new LinkedStack<string>()
linked.push('root')
linked.push('child')
linked.top() // 'child'Complexity
| Operation | Stack | LinkedStack |
|---|---|---|
push / add | Amortized O(1) | O(1) |
pop | O(1) | O(1) |
top / peek | O(1) | O(1) |
contains | O(n) | O(n) |