Stack and Queue Simulator — Visualize LIFO and FIFO
Stacks and queues are the two most fundamental abstract data types in computer science. Every recursive function call uses a stack; every task scheduler uses a queue. This guide covers both structures, their operations, and real-world applications — with an interactive simulator to reinforce the concepts.
Stack vs Queue at a Glance
| Property | Stack | Queue |
|---|---|---|
| Order | LIFO — Last In, First Out | FIFO — First In, First Out |
| Add element | Push (to top) | Enqueue (to rear) |
| Remove element | Pop (from top) | Dequeue (from front) |
| Peek | Top element, no removal | Front element, no removal |
| All operations | O(1) | O(1) |
| Real-world analogy | Stack of plates | Queue at a checkout |
How to Use the Stack and Queue Simulator
- Open the Stack and Queue Simulator
- Select Stack or Queue mode using the toggle
- Type a value and click Push (stack) or Enqueue (queue) to add an element
- Click Pop or Dequeue to remove and see which element leaves first
- Click Peek to inspect the next element without removing it
- Use Clear to reset and try a new sequence
Stack Applications
Function call stack
Every time a function is called, the CPU pushes a stack frame containing the return address, local variables, and parameters. When the function returns, the frame is popped. Recursive functions build deep stacks — a stack overflow occurs when recursion depth exceeds the stack's memory limit (typically 1–8 MB).
Undo/redo history
Text editors maintain two stacks: an undo stack and a redo stack. Each edit is pushed onto the undo stack. Ctrl+Z pops from undo and pushes to redo. Ctrl+Y reverses this. Any new edit clears the redo stack — the same pattern used in version control systems.
Expression parsing
The shunting-yard algorithm uses a stack to convert infix expressions (3 + 4 × 2) to postfix (3 4 2 × +), which is trivial to evaluate. Balanced parenthesis checking also uses a stack: push on open brackets, pop and match on close brackets.
Queue Applications
Breadth-First Search (BFS)
BFS uses a queue to explore a graph level by level. Start by enqueuing the source node. Dequeue a node, process it, then enqueue all unvisited neighbours. This guarantees the shortest path in an unweighted graph — something DFS (which uses a stack) cannot guarantee.
Task scheduling and rate limiting
Operating system process schedulers, print spoolers, and web server request queues all use FIFO ordering to ensure fairness — the first request in is the first served. Priority queues extend this by weighting items, but the basic abstraction is still queue-based.
Sliding window problems
A monotonic deque (double-ended queue) solves the sliding window maximum problem in O(n). Elements are added to the rear and expired elements removed from the front — maintaining a deque of candidates in decreasing order. This pattern appears constantly in competitive programming.
Variants You Should Know
| Variant | Based on | Key difference |
|---|---|---|
| Deque (double-ended) | Queue | Add/remove at both ends |
| Priority queue | Queue | Highest priority dequeued first |
| Circular buffer | Queue | Fixed size, wraps around |
| Min/max stack | Stack | O(1) min or max query |
| Monotonic stack | Stack | Maintains sorted order for next-greater problems |
Common Interview Questions
How do you implement a queue using two stacks?
Use an "inbox" stack and an "outbox" stack. Enqueue pushes to inbox. Dequeue pops from outbox; if outbox is empty, pour all inbox elements into outbox first (reversing their order). Each element is moved at most once, so amortized cost is O(1) per operation.
What is the min stack problem?
Design a stack that supports push, pop, and getMin in O(1). The trick: maintain a parallel "min stack" that records the minimum at each level. When you push a value, also push min(value, minStack.top()) to the min stack. When you pop, pop both stacks in sync.
Try the Stack and Queue Simulator
Visualize LIFO and FIFO operations live in the Stack and Queue Simulator — push, pop, enqueue, dequeue, and peek with animated element movement.
Open Stack and Queue Simulator