Visualize & Master
Algorithms & Data Structures
Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.
About A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from the top
A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from the top.
A queue is a First-In-First-Out (FIFO) data structure where elements are added at the rear and removed from the front.
How It Works
Stacks provide two primary operations: `push` (add to top) and `pop` (remove from top).
Queues provide `enqueue` (add to rear) and `dequeue` (remove from front).
Both can be implemented with arrays or linked lists.
Stacks model function call recursion, while queues model waiting lines and scheduled processing.
The `peek` operation returns the top/front element without removing it.
Time & Space Complexities
| Operation | Time | Space |
|---|---|---|
| Stack Push | O(1) | O(1) |
| Stack Pop | O(1) | O(1) |
| Stack Peek | O(1) | O(1) |
| Queue Enqueue | O(1) | O(1) |
| Queue Dequeue (linked-list) | O(1) | O(1) |
| Queue Dequeue (array) | O(n) | O(1) |
| Queue Peek | O(1) | O(1) |
Best Use Cases
- Undo/redo in editors (stack)
- Syntax parsing and expression evaluation (stack)
- DFS traversal and backtracking (stack)
- BFS traversal and level-order (queue)
- Printer job scheduling, task queues (queue)
- Breadth-first search in graphs (queue)
Worked Example
Stack push 42 and pop (LIFO)
Input: stack = [10, 20, 30, 40, 50] (top = 50)- 1 Push 42: append it to the end — the stack becomes [10, 20, 30, 40, 50, 42], top = 42.
- 2 Peek returns 42 without removing it.
- 3 Pop removes the top: 42 leaves, and the stack is back to [10, 20, 30, 40, 50].
- 4 Each operation touches only the top element, so all are O(1).
Queue enqueue 42 and dequeue (FIFO)
Input: queue = [10, 20, 30, 40, 50] (front = 10)- 1 Enqueue 42: append to the rear — the queue becomes [10, 20, 30, 40, 50, 42].
- 2 Peek returns the front (10) without removing it.
- 3 Dequeue removes the front (10), leaving [20, 30, 40, 50, 42]; a linked-list implementation updates the front pointer in O(1).
- 4 The first element enqueued is always the first dequeued.
Pseudocode
function push(stack, value):
stack[stack.length] = value
function pop(stack):
if stack is empty:
error "Stack underflow"
value = stack[stack.length - 1]
stack.length = stack.length - 1
return value function peek(stack):
if stack is empty:
error "Stack underflow"
return stack[stack.length - 1] function enqueue(queue, value):
queue[queue.length] = value
function dequeue(queue):
if queue is empty:
error "Queue underflow"
value = queue[0]
shift all elements left by 1
queue.length = queue.length - 1
return value function enqueue(queue, value):
newNode = Node(value)
if queue.rear is null:
queue.front = newNode
queue.rear = newNode
else:
queue.rear.next = newNode
queue.rear = newNode
function dequeue(queue):
if queue.front is null:
error "Queue underflow"
value = queue.front.value
queue.front = queue.front.next
if queue.front is null:
queue.rear = null
return value Stack
Select an operation to begin.