Stack Visualization & Animation

Last-In-First-Out structure with O(1) push, pop, and peek; backbone of DFS, expression evaluation, and undo.

## What is it? A Stack is a Last-In-First-Out (LIFO) data structure. Elements are added (pushed) and removed (popped) from the same end — the top. Think of a stack of plates. ## How it works **Core operations (all O(1)):** - **push(x)** — add element `x` to the top - **pop()** — remove and return the top element - **peek() / top()** — return the top element without removing it - **isEmpty()** — check if the stack has no elements **Implementations:** - Array-based: use an index to track the top - Linked list: push/pop at the head ## When to use - Expression evaluation and conversion (infix/postfix) - Balanced parentheses checking - Undo/redo operations - Depth-First Search (DFS) — call stack or explicit stack - Monotonic stack problems ## Key Points - LIFO — the last element pushed is the first popped - All core operations are O(1) amortized with a dynamic array - Stack overflow occurs when the stack exceeds its capacity (recursion depth in call stack)

Category: data-structure

Difficulty: beginner

Time Complexity: O(1) push/pop

Space Complexity: O(n)

Stack

beginner

Last-In-First-Out structure with O(1) push, pop, and peek; backbone of DFS, expression evaluation, and undo.

Stack is empty

Stack initialized (empty)