Stack and Queue Simulator — Visualise Push, Pop, Enqueue, and Dequeue
Stacks and queues are two of the most fundamental data structures in computer science. A stack follows Last-In-First-Out (LIFO) order — the most recently added item is the first to be removed. A queue follows First-In-First-Out (FIFO) order — items are processed in the order they arrived. Both are abstract data types with well-defined operations and dozens of real-world applications. The stack and queue simulator visualises every operation step by step.
Stack and Queue Operations
| Structure | Operation | What it does | Time complexity | Notes |
|---|---|---|---|---|
| Stack | Push(x) | Add element x to the top of the stack | O(1) | Top pointer increments; item placed at new top |
| Stack | Pop() | Remove and return the top element | O(1) | Top pointer decrements; underflow error if stack is empty |
| Stack | Peek() | Return the top element without removing it | O(1) | Also called Top(); does not modify the stack |
| Stack | isEmpty() | Return true if the stack has no elements | O(1) | Check before Pop() to avoid underflow |
| Queue | Enqueue(x) | Add element x to the rear of the queue | O(1) | Also called Offer() in Java; rear pointer advances |
| Queue | Dequeue() | Remove and return the front element | O(1) | Also called Poll(); front pointer advances; underflow if empty |
| Queue | Front() | Return the front element without removing it | O(1) | Also called Peek() in Queue context; non-destructive |
| Queue | isEmpty() | Return true if the queue has no elements | O(1) | Check before Dequeue() to avoid underflow |
How to Use the Simulator
- Open the stack and queue simulator.
- Select the data structure: Stack or Queue.
- For the stack: enter a value and click Push to add it to the top. Click Pop to remove the top element.
- For the queue: enter a value and click Enqueue to add it to the rear. Click Dequeue to remove the front element.
- The visualisation shows the state of the structure after each operation, with arrows indicating front/top and rear positions.
- Use the step through mode to execute a pre-written sequence of operations one by one, watching the structure evolve.
Real-World Applications
| Structure | Application | How it works |
|---|---|---|
| Stack | Browser back button | Pages visited are pushed onto a history stack; Back pops them off — LIFO restores the previous page |
| Stack | Undo/Redo in applications | Each action is pushed onto an undo stack; Ctrl+Z pops the last action; Ctrl+Y pushes to redo stack |
| Stack | Function call stack | When a function calls another, the new call is pushed; when it returns, it is popped. Recursion fills the call stack — stack overflow occurs if recursion is too deep. |
| Stack | Bracket matching / syntax parsing | Opening brackets are pushed; closing brackets should match the top — if not, or if stack empty when ) found, syntax error |
| Queue | Print spooler | Print jobs are enqueued; printer dequeues and processes in order — FIFO ensures first-submitted prints first |
| Queue | BFS (Breadth-First Search) | Graph nodes are enqueued when discovered; dequeued when processed — queue order ensures BFS visits all nodes at distance n before n+1 |
| Queue | Message queues (Kafka, RabbitMQ) | Messages are enqueued by producers; consumers dequeue in order — decouples producers and consumers in distributed systems |
| Queue | Operating system process scheduling | CPU time-slicing uses a ready queue — processes enqueue when ready; CPU dequeues and runs the next process |
Stack: LIFO in Detail
A stack works like a pile of plates: you can only add or remove from the top. The last plate placed is the first to be taken off — Last-In-First-Out.
Visualise a stack as a vertical list with the "top" at the top:
- Push(A) → Stack: [A] (A is top)
- Push(B) → Stack: [B, A] (B is top)
- Push(C) → Stack: [C, B, A] (C is top)
- Pop() → returns C, Stack: [B, A]
- Pop() → returns B, Stack: [A]
- Peek() → returns A without removing, Stack: [A]
Note: the order of retrieval reverses the order of insertion. This reversal property makes stacks useful for any problem requiring reversal (reversing a string, reversing a linked list) or backtracking (maze solving, parsing expressions).
Queue: FIFO in Detail
A queue works like a real-world queue (line of people): people join at the rear and leave from the front. The first person to join is the first to be served — First-In-First-Out.
Visualise a queue as a horizontal list with front on the left:
- Enqueue(A) → Queue: [A] (A is front and rear)
- Enqueue(B) → Queue: [A, B] (A is front, B is rear)
- Enqueue(C) → Queue: [A, B, C]
- Dequeue() → returns A, Queue: [B, C]
- Dequeue() → returns B, Queue: [C]
- Front() → returns C without removing, Queue: [C]
Implementing Stacks and Queues
Array-based implementation
Both can be implemented using a fixed-size array. For a stack: keep a "top" pointer (index of the top element). Push increments top and sets array[top] = value; Pop returns array[top] and decrements top. Fixed size limits the stack to n elements — overflow error if pushing when full.
Linked-list implementation
A linked list implementation avoids the fixed-size limitation. Each element is a node containing the value and a pointer to the next node. Push creates a new node and makes it the new head; Pop removes the head node. This gives O(1) push and pop with no maximum size (limited only by memory).
Circular buffer queue
A naive array queue wastes space as the front pointer advances — the front of the array becomes unused. A circular buffer uses modulo arithmetic to wrap the rear and front pointers around, reusing the beginning of the array. Front = (front + 1) % capacity. This is the standard efficient queue implementation.
Priority Queue
A priority queue is a variant of the queue where each element has a priority. Dequeue always removes the highest-priority element, regardless of insertion order. Useful for:
- Dijkstra's shortest path algorithm (always processes the node with minimum distance)
- A* pathfinding (used in GPS and game AI)
- Hospital triage (most critical patients treated first regardless of arrival time)
- Operating system process scheduling with priorities
Priority queues are typically implemented with a heap data structure, giving O(log n) enqueue and dequeue operations.
Deque (Double-Ended Queue)
A deque (pronounced "deck") allows insertion and removal from both ends. It generalises both stacks and queues — a stack is a deque where you only use one end; a queue is a deque where you insert at one end and remove from the other. Python's collections.deque is a commonly used implementation with O(1) operations at both ends.
Common Questions
When should I use a stack vs. a queue?
Use a stack when you need LIFO access: processing things in reverse order, undo operations, depth-first traversal, expression evaluation, or backtracking algorithms. Use a queue when you need FIFO access: processing in arrival order, breadth-first traversal, buffering data streams, or task scheduling where fairness matters. If priority matters, use a priority queue.
What is a stack overflow?
A stack overflow occurs when a program uses more stack memory than is available. This most commonly happens with infinite or very deep recursion — each function call pushes a new frame onto the call stack, and if the recursion does not terminate, the stack grows until it exhausts the memory allocated for it. Most runtimes raise a stack overflow error (StackOverflowError in Java, RecursionError in Python) before the process crashes.
How are stacks used in expression evaluation?
Compilers and calculators use stacks for expression evaluation. The shunting-yard algorithm (Dijkstra, 1961) converts infix notation (3 + 4 × 2) to postfix (RPN: 3 4 2 × +) using a stack to handle operator precedence. To evaluate postfix: push operands; when an operator is encountered, pop two operands, apply the operator, and push the result. Final stack value is the answer.
Simulate Stack and Queue Operations
Interactive visualisation of push, pop, enqueue, and dequeue — step through each operation to see exactly how stacks and queues work.
Open Stack & Queue Simulator