Q.Compare and contrast queue with stack.
Queue and stack are both linear data structures that differ fundamentally in their access order: queue follows FIFO (First In, First Out) while stack follows LIFO (Last In, First Out), making each suited to different real-world scenarios.
A queue and a stack are two foundational abstract data types that manage collections of elements, but they impose opposite disciplines on how elements are added and removed. Understanding when to reach for each comes down to recognizing the access pattern your problem demands.
The Core Difference: Access Order
Stack operates on the LIFO (Last In, First Out) principle. Think of a stack of plates: you add a new plate on top, and when you need one you take from the top. The most recently added element is the first to leave. This makes stacks natural for problems involving reversal, backtracking, or nested structures — function call management, undo mechanisms, expression evaluation, depth-first traversal.
Queue operates on the FIFO (First In, First Out) principle. Picture a line at a ticket counter: the person who arrives first is served first. The oldest element in the collection is removed first. Queues model fairness, scheduling, and sequential processing — printer job queues, breadth-first traversal, CPU task scheduling, buffering data streams.
Detailed Comparison
| Aspect | Stack | Queue |
|---|---|---|
| Principle | LIFO (Last In, First Out) | FIFO (First In, First Out) |
| Insertion | push() — adds to the top | enqueue() — adds to the rear |
| Deletion | pop() — removes from the top | dequeue() — removes from the front |
| Access | Only the top element is accessible | Only the front element is accessible |
| Ends involved | Single end (top) for both operations | Two ends: rear (insert), front (delete) |
| Real-world analogy | Stack of books, browser back button | Queue at a bank, printer spooler |
| Typical applications | Function calls (call stack), recursion, undo/redo, parenthesis matching, DFS | BFS, CPU scheduling, handling requests, keyboard buffer |
| Python implementation | list with append() and pop() | collections.deque with append() and popleft() |
Implementation Perspective
Both can be implemented using arrays or linked lists, but the choice affects efficiency.
Stack with a list (array):
stack = []
stack.append(10) # push
stack.append(20)
top = stack.pop() # pop, returns 20
Both push and pop are at the end of a Python list.
Queue with collections.deque:
from collections import deque
queue = deque()
queue.append(10) # enqueue at rear
queue.append(20)
front = queue.popleft() # dequeue from front, returns 10
Using a plain list for a queue is inefficient: pop(0) is because all elements shift. A deque (double-ended queue) gives operations at both ends.
Never use list.pop(0) for a queue in production code — it forces a linear-time shift of all remaining elements. Always prefer collections.deque for queue operations.
When to Use Each
Choose a stack when:
- You need to reverse order (most recent first).
- You are implementing recursion iteratively.
- You are parsing nested structures (HTML tags, balanced parentheses).
- You need backtracking (maze solving, game state exploration).
Choose a queue when:
- You need to preserve arrival order (first come, first served).
- You are implementing BFS or level-order traversal.
- You are modeling real-world waiting lines or buffers.
- You are scheduling tasks fairly.
Example: Reversing vs. Preserving Order
Suppose you have the sequence [1, 2, 3].
Stack behavior:
stack = []
for x in [1, 2, 3]:
stack.append(x)
while stack:
print(stack.pop(), end=' ') # Output: 3 2 1
Queue behavior:
from collections import deque
queue = deque()
for x in [1, 2, 3]:
queue.append(x)
while queue:
print(queue.popleft(), end=' ') # Output: 1 2 3
The stack reverses; the queue preserves.
Stack uses LIFO (Last In, First Out) with operations at one end (top), suited for recursion and backtracking. Queue uses FIFO (First In, First Out) with insertion at the rear and deletion at the front, suited for scheduling and breadth-first processing. Stack reverses order; queue preserves it.
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.