Difference Between Stack and Queue Data Structure
Stacks and Queues are both linear data structures used to store collections of elements, but they differ fundamentally in the order elements are added and removed.
Stack
A Stack follows the LIFO (Last In, First Out) principle. The last element added is the first one to be removed. Core operations are push (insert) and pop (remove) from the same end, called the top.
Queue
A Queue follows the FIFO (First In, First Out) principle. The first element added is the first one to be removed. Core operations are enqueue (insert at rear) and dequeue (remove from front).
Comparison Table
| Aspect | Stack | Queue |
|---|---|---|
| Order | LIFO | FIFO |
| Insertion | push (at top) | enqueue (at rear) |
| Removal | pop (from top) | dequeue (from front) |
| Access point | Single end (top) | Two ends (front & rear) |
| Use cases | Undo/redo, function call stack, expression evaluation | Task scheduling, printer queues, BFS traversal |
# Stack using a Python list
stack = []
stack.append(1) # push
stack.append(2)
stack.pop() # removes 2 (LIFO)
# Queue using collections.deque
from collections import deque
queue = deque()
queue.append(1) # enqueue
queue.append(2)
queue.popleft() # removes 1 (FIFO)
Applications
- Stack: backtracking algorithms, syntax parsing, browser history.
- Queue: CPU task scheduling, handling requests in web servers, breadth-first search.
Ready to master Data Structures & Algorithms?
Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.
.png)