What is a Stack?
Stack follows the LIFO (Last-In-First-Out)
principle. The last element added is the first one to come out.
Stack Operations
- Push: Add an element to the top
- Pop: Remove the top element
- Peek/Top: View the top-most element
Visualisation:

Example Flow
stack.push(1)
→ [1]stack.push(3)
→ [1, 3]stack.pop()
→ returns 3 → [1]stack.peek()
→ returns 1
Examples
- Stacks of Books
- Undo/Redo feature
- Recursion (Function Call Stack)
- Browsing History
What is a Queue?
Queue follows the FIFO (First-In-First-Out)
principle. The first element added is the first to be removed.
Queue Operations
- Enqueue: Add an element to the end
- Dequeue: Remove element from the front
- Peek/Front: View the front-most element
Visualisation:

Examples
- Ticket Counter
- OS Task Scheduling
- Printers
- Queues (in general)
Why Use Stack and Queue❓
- Organize data logically (based on problem’s need).
- Order of Operations Matter
- Optimize time & memory for specific scenarios
Use Cases
- Recursion (stack)
- Level Order Traversal (queue)
- BFS (queue)
- DFS (stack)
Language Implementation
JavaScript
let stack = [];
stack.push(10);
stack.push(7);
stack.pop(); // returns 7
stack.pop(); // returns 10
Java
Stack stack = new Stack<>();
stack.push(10);
stack.pop();
stack.peek();
C++
#include
std::stack s;
s.push(10);
s.pop();
s.top();
Python
stack = []
stack.append(10)
stack.pop()
top = stack[-1] # peek
Note:Find out more about stacks in your preferred programming language.
Stack vs Queue
📥 Stack
- Last-In-First-Out
- Access only from top
- Used in DFS, Recursion
📤 Queue
- First-In-First-Out
- Access from front & rear
- Used in BFS, Scheduling
comparison with other Data-Structures
