Stack ek linear data structure hai jo LIFO (Last In First Out) principle follow karta hai.
Stack ek ordered collection hai jahan insertion aur deletion sirf ek hi end se hota hai jise top kaha jata hai.
Stack ke do main rules hote hain:
Naya element hamesha top par add hota hai
Remove bhi top se hi hota hai
Queue kya hota hai
FIFO concept
Operations (enqueue, dequeue)
Real-life example
C++ code
Socho plates ka stack 🍽️
Tum ek plate rakhte ho → push
Sabse upar wali plate uthate ho → pop
👉 Jo last me rakhi gayi thi → wahi pehle niklegi
👉 Yehi Stack ka concept hai
Stack ke kuch important operations hote hain:
Is operation me hum stack me naya element add karte hain.
Is operation me hum stack ke top element ko remove karte hain.
Is operation se hum top element ko dekh sakte hain bina remove kiye.
Ye check karta hai ki stack empty hai ya nahi.
Agar stack array se implement hai to ye check karta hai overflow condition.
Maan lo hum stack me elements add karte hain:
Push: 10 → 20 → 30
Stack dikhega:
Top → 30, 20, 10
Ab agar hum Pop karein:
👉 30 remove hoga
👉 Fir 20
👉 Fir 10
Ye clearly dikhata hai ki stack LIFO follow karta hai.
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int x){
if(top == MAX-1){
printf("Stack Overflow\n");
} else {
stack[++top] = x;
}
}
void pop(){
if(top == -1){
printf("Stack Underflow\n");
} else {
top--;
}
}
void display(){
for(int i = top; i >= 0; i--){
printf("%d ", stack[i]);
}
}
Other Code Example:
#include <iostream>
using namespace std;
#define MAX 5
int stack[MAX];
int top = -1;
// Push function
void push(int value) {
if (top == MAX - 1) {
cout << "Stack Overflow\n";
} else {
top++;
stack[top] = value;
cout << value << " pushed into stack\n";
}
}
// Pop function
void pop() {
if (top == -1) {
cout << "Stack Underflow\n";
} else {
cout << stack[top] << " popped from stack\n";
top--;
}
}
// Peek function
void peek() {
if (top != -1)
cout << "Top element is: " << stack[top] << endl;
else
cout << "Stack is empty\n";
}
int main() {
push(10);
push(20);
push(30);
pop();
peek();
return 0;
}
👉 Ye ek basic array-based stack implementation hai jo beginners ke liye perfect hai.
Fixed size hota hai
Simple hota hai
Dynamic size hota hai
Memory efficient hota hai
Stack real-world aur programming dono me bahut use hota hai:
Function calls (Call Stack)
Undo/Redo operations
Expression evaluation (infix, postfix)
Parenthesis checking
Browser history
Easy to implement
Fast operations (O(1))
Memory management me useful
Recursion me use hota hai
Limited access (sirf top element)
Array implementation me overflow problem
Fixed size limitation
👉 Ye topics ek dusre se connected hain aur aapki DSA journey complete karte hain.
Operation Time Complexity
Push O(1)
Pop O(1)
Peek O(1)
👉 Stack bahut efficient data structure hai kyunki iska har operation constant time me hota hai.
Stack ko queue ke saath confuse karna
Overflow aur underflow conditions ignore karna
Sirf theory padhna, practice na karna
👉 Solution: Code likho + dry run karo
Concept clear karo
Code likho (array + linked list)
10–15 questions practice karo
Real problems solve karo (parenthesis, expression evaluation)
Stack ek simple lekin powerful data structure hai jo har programmer ko aana chahiye. Agar aap DSA roadmap follow kar rahe hain, to stack aapka pehla strong building block hoga.
Consistent practice aur concept clarity ke saath aap stack ko easily master kar sakte hain aur coding interviews ke liye ready ho sakte hain.
👉 Complete Roadmap:
https://sites.google.com/view/dsarop/
👉 Next Topic: Queue