Stack & Queue

πŸ“š Stack LIFO
Last In, First Out β€” like a stack of plates. Push adds to top, pop removes from top.
Visual
[ empty stack ]Push items to see them stack up ↑
β–Ό BOTTOM
Operations
Push an item to begin.
0
Size
0
Ops done
Operation Log
Operations appear here…
🚢 Queue FIFO
First In, First Out β€” like a real queue. Enqueue adds to rear, dequeue removes from front.
Visual
FRONT →← REAR
[ empty queue ]
Operations
Enqueue an item to begin.
0
Size
0
Ops done
Quick Reference
Stack (LIFO):
push(x) β†’ O(1)   pop() β†’ O(1)
peek() β†’ O(1)  is_empty() β†’ O(1)

Queue (FIFO):
enqueue(x) β†’ O(1)  dequeue() β†’ O(1)
front() β†’ O(1)   is_empty() β†’ O(1)
Difficulty
🟒 Beginner
🟑 Intermediate
πŸ”΄ Advanced
🎲 All
⭐ 0 XP
0 / 0
Loading questions…
βœ… 0 correct    ❌ 0 wrong
πŸ“Š Stack vs Queue β€” Head-to-Head Comparison
πŸ“š Stack (LIFO)
OrderLast In, First Out
Addpush() β€” top
Removepop() β€” top
Peekpeek() / top()
Pythonlist.append/pop
SpaceO(n)
Time opsO(1) all
🚢 Queue (FIFO)
OrderFirst In, First Out
Addenqueue() β€” rear
Removedequeue() β€” front
Peekfront()
Pythoncollections.deque
SpaceO(n)
Time opsO(1) all
πŸ’» Python Code Examples
# Stack using list stack = [] stack.append(10) # push stack.append(20) stack.append(30) print(stack.pop()) # 30 β€” LIFO print(stack[-1]) # peek β†’ 20
# Queue using deque from collections import deque q = deque() q.append(10) # enqueue (rear) q.append(20) q.append(30) print(q.popleft()) # 10 β€” FIFO print(q[0]) # front β†’ 20
🌍 Real-World Use Cases
🌐
Browser Back Button
Stack β€” each page visited is pushed; back button pops
πŸ–¨οΈ
Print Queue
Queue β€” first document sent is printed first (FIFO)
↩️
Undo Feature
Stack β€” each action pushed; Ctrl+Z pops last action
πŸ–₯️
CPU Scheduling
Queue β€” processes handled in order they arrive (FIFO)
πŸ“ž
Call Center
Queue β€” first caller on hold is first to be answered
🧠
DFS Algorithm
Stack β€” depth-first search explores deepest path first
πŸ—ΊοΈ
BFS Algorithm
Queue β€” breadth-first explores level by level outward
βœ…
Bracket Checker
Stack β€” push opening, pop and match when closing found