Stack Types — Array, Linked, Min & Monotonic Visualized
Compare stack implementations — array-based, linked-list, min stack, and monotonic stack with interactive visualizer, code, and real-world examples.
Introduction
Stack types share the same LIFO interface but differ in backing storage and specialized behavior. This guide covers array, linked-list, min/max, and monotonic stacks — all building on stack fundamentals.
Quick index
| # | Section |
|---|---|
| 1 | Type comparison |
| 2 | Interactive visualizer |
| 3 | Full implementation |
| 4 | Choosing a type |
| 5 | Real-life examples |
1. Type comparison
| Type | Backing | Extra ops | Typical use |
|---|---|---|---|
| Array stack | Dynamic array | — | Default — simple, cache-friendly |
| Linked stack | Singly linked list | — | Unbounded, no resize spikes |
| Min stack | Array + aux min tracker | getMin() O(1) | Running minimum, stock span |
| Monotonic stack | Array (maintains order) | next greater/smaller | Histogram, trapping rain water |
2. Interactive visualizer
Stack Types — Array, Linked, Min & Monotonic
Four stack variants — same LIFO interface, different backing storage and specialized behavior.
Dynamic array with push/pop at end — JavaScript Array as stack.
Array-based stack
| class ArrayStack { | |
| constructor() { this.items = [] } | |
| push(v) { this.items.push(v) } | O(1) amortized |
| pop() { return this.items.pop() } | |
| } | |
| const s = new ArrayStack() | |
| s.push(10); s.push(20); s.push(30) | → [10, 20, 30] |
Default choice — cache-friendly, simple.
3. Full implementation
// ── Array stack ──
class ArrayStack {
constructor() {
this.items = []
}
push(v) {
this.items.push(v)
}
pop() {
return this.items.pop()
}
peek() {
return this.items.at(-1)
}
}
// ── Linked-list stack ──
class LNode {
constructor(v) {
this.val = v
this.next = null
}
}
class LinkedStack {
constructor() {
this.head = null
}
push(v) {
const n = new LNode(v)
n.next = this.head
this.head = n
}
pop() {
if (!this.head) return
const v = this.head.val
this.head = this.head.next
return v
}
}
// ── Min stack ──
class MinStack {
constructor() {
this.stack = []
this.mins = []
}
push(v) {
this.stack.push(v)
const min = this.mins.length ? Math.min(this.mins.at(-1), v) : v
this.mins.push(min)
}
getMin() {
return this.mins.at(-1)
}
pop() {
this.mins.pop()
return this.stack.pop()
}
}
// ── Monotonic stack (next greater) ──
function nextGreater(arr) {
const stack = [],
result = Array(arr.length).fill(-1)
for (let i = 0; i < arr.length; i++) {
while (stack.length && arr[stack.at(-1)] < arr[i]) {
result[stack.pop()] = arr[i]
}
stack.push(i)
}
return result
}Function calls with output
// ── Demo calls — output shown on the right ──
const s = new ArrayStack()
s.push(10)
s.push(20) // → stack [10, 20]
s.pop() // → 20
const ms = new MinStack()
ms.push(3)
ms.push(1)
ms.push(5)
ms.getMin() // → 1
ms.pop() // → 5
nextGreater([2, 5, 3]) // → [5, -1, -1]4. Choosing a type
| Need | Pick |
|---|---|
| General purpose | Array stack |
| No array resize, unbounded | Linked stack |
| O(1) minimum at any point | Min stack |
| Next greater/smaller element | Monotonic stack |
5. Real-life examples
| Type | Real system |
|---|---|
| Array stack | JavaScript call stack, undo history |
| Min stack | Stock drawdown tracker, sliding window minimum |
| Monotonic stack | Largest rectangle in histogram, daily temperatures |
| Linked stack | Expression evaluator with unbounded depth |
Min stack for stock prices
Track running minimum in O(1) — used in financial dashboards and interview problems.
Index → value
Live stock prices streaming in.
Real-world implementation
class MinStack {
constructor() { this.stack = []; this.mins = [] }
push(v) {
this.stack.push(v)
const min = this.mins.length ? Math.min(this.mins.at(-1), v) : v
this.mins.push(min)
}
pop() { this.mins.pop(); return this.stack.pop() }
getMin() { return this.mins.at(-1) }
}Full working code for the scenario above.
Function calls with output
| // ── Function calls with output ── | |
| const ms = new MinStack() | |
| ms.push(150) | → min: 150 |
Side output shows return value after each call — step through to trace execution.
Summary
Array stack for defaults; min stack for running extrema; monotonic stack for next-greater/smaller patterns — all O(1) push/pop at the top.
Next reads: Stack Applications · DSA Stacks
Subscribe to my newsletter
Stay up to date and get notified when I share new contents.
No spam ever, unsubscribe anytime