STACK
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
This means the element inserted last will be removed first.
Key Features:
Elements are added and removed from the same end (called TOP).
Works like a stack of plates — the last plate kept is the first to be taken out.
Operations:
Push: Insert an element into the stack.
Pop: Remove the top element from the stack.
Peek/Top: View the top element without removing it.
Example:
Imagine a pile of books 📚.
You keep placing books on top (push).
To take one, you remove the topmost book first (pop).
Applications of Stack:
Undo/Redo functionality in editors
Browser history (back and forward buttons)
Expression evaluation in programming
Recursive function calls
QUEUE
Definition:
A queue is a linear data structure that follows the FIFO (First In, First Out) principle.
This means the element inserted first will be removed first.
Key Features:
Elements are added at the rear (enqueue) and removed from the front (dequeue).
Works like a bus queue — the first person in line gets in the bus first.
Operations:
Enqueue: Insert an element at the rear.
Dequeue: Remove an element from the front.
Peek/Front: View the front element without removing it.
Example:
A ticket counter
The person who comes first gets the ticket first.