Stack in Data Structures: Complete Guide with Java Implementation and Real-Life Examples
Stack in Data Structures (Complete Detailed Guide)
What is Stack?
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle, meaning the last element inserted into the stack is the first one to be removed. It works similarly to a stack of plates where you can only remove the top plate first. In a stack, all insertions and deletions happen from one end called the top. Stack operations are efficient and typically take constant time. Stacks are widely used in function calls, expression evaluation, undo-redo operations, and syntax parsing.
Socho tumhare paas plates ka ek dher (stack) hai.
Tum:
-
Plate upar se hi rakh sakte ho
-
Plate upar se hi nikaal sakte ho
Tum beech wali plate directly nahi nikaal sakte.
Isi concept ko programming me bolte hain:
👉 Stack = Last In, First Out (LIFO)
Matlab jo sabse last me aaya, wahi sabse pehle bahar jayega.
Real Life Example – Browser Back Button
Tumne:
-
Google open kiya
-
Fir YouTube
-
Fir Instagram
Back dabaya → Instagram se YouTube
Fir back → YouTube se Google
Ye bhi stack ka example hai.
What is LIFO?
LIFO stands for Last In, First Out. It means the most recently added element is removed first. If you imagine a stack of books, the last book placed on top will be the first one removed. This principle defines how stack operations behave. Unlike queues, stacks do not allow access from both ends, making them useful in scenarios where reverse processing is required.
Basic Operations in Stack
-
Push
Ek element ko stack ke top par add karna. Matlab element ko stack me add karna (upar rakhna) -
Pop
Top element ko remove karna.Upar wala element hata dena -
Peek (Top)
Top element ko dekhna bina remove kiye. -
isEmpty
Check karna stack empty hai ya nahi.
Real Life Example (Student Friendly)
Socho tumhare paas books ka stack hai.
1️⃣ Nayi book upar rakhi → Push
2️⃣ Upar wali book hatai → Pop
3️⃣ Sirf upar wali book dekhi → Peek
Tum beech wali book directly nahi nikal sakte.
Java Implementation (Using Array)
public class StackExample {
static int top = -1;
static int[] stack = new int[5];
static void push(int value) {
if (top == stack.length - 1) {
System.out.println("Stack Overflow");
} else {
stack[++top] = value;
}
}
static void pop() {
if (top == -1) {
System.out.println("Stack Underflow");
} else {
System.out.println("Removed: " + stack[top--]);
}
}
static void peek() {
if (top != -1)
System.out.println("Top Element: " + stack[top]);
}
public static void main(String[] args) {
push(10);
push(20);
push(30);
peek();
pop();
peek();
}
}Java Me Simple Example
public class SimpleStack {
static int top = -1;
static int[] stack = new int[5];
static void push(int value) {
stack[++top] = value;
}
static void pop() {
System.out.println("Removed: " + stack[top--]);
}
public static void main(String[] args) {
push(10);
push(20);
push(30);
pop(); // 30 remove hoga
}
}Output:
30Kyun?
Kyuki 30 last me dala tha.
Time Complexity
-
Push → O(1)
-
Pop → O(1)
-
Peek → O(1)
Sab operations constant time me hote hain.
Applications of Stack
-
Function Call Management
Recursion internally stack use karta hai. -
Undo/Redo Operations
Text editor me. -
Expression Evaluation
Postfix / Prefix expressions. -
Parenthesis Checking
Compiler design me.
Advantages of Stack
-
Simple implementation
-
Fast operations
-
Reverse order processing easy
Recursion me internally stack use hota hai
-
Undo/Redo feature me
-
Expression solve karne me
-
Coding interviews me frequently poocha jata hai
Disadvantages of Stack
-
Random access possible nahi
-
Fixed size array me overflow risk
Ek Line Me Samjho
👉 Stack ek aisa data structure hai jisme entry aur exit dono sirf upar se hoti hai.
Quick Check
👉 Agar stack me 10, 20, 30 push kiya gaya ho, to pop karne par kaunsa element niklega?
Thinking Question
Kya stack ko Linked List se implement kiya ja sakta hai?
Agar haan, to usme overflow issue kaise solve hoga?

Comments
Post a Comment