Java Stack: LIFO Data Structure with Real Examples
Java Stack: LIFO Data Structure with Real Examples
Java Stack Introduction
जब भी हम किसी data को temporary store करना चाहते हैं और उसे उसी क्रम में वापस पाना चाहते हैं जिस क्रम में हमने डाला था (Last In First Out), तो हम Stack का use करते हैं। Java में Stack एक LIFO (Last In First Out) data structure होता है, यानी जो element सबसे बाद में डाला गया है, वही सबसे पहले निकलता है।
Stack का use बहुत जगह किया जाता है जैसे expression evaluation, recursion, function call tracking, और undo-redo operations में।
Stack Class in Java
Java में Stack एक built-in class है जो java.util package के अंदर आती है। यह Vector class को extend करती है और कुछ extra methods provide करती है ताकि हम stack operations जैसे push(), pop(), peek() कर सकें।
Stack Class Syntax
import java.util.Stack;
Stack<DataType> stack = new Stack<>();
Commonly Used Stack Methods
| Method | Description |
|---|---|
push(E item) | Stack में एक नया element जोड़ने के लिए। |
pop() | Top element को remove और return करने के लिए। |
peek() | Top element को देखता है पर remove नहीं करता। |
isEmpty() | Check करता है कि stack खाली है या नहीं। |
search(Object o) | Element की position (top से count) return करता है। |
Stack Operations in Java
1. Push Operation
Push() method का use stack में नया element डालने के लिए किया जाता है। यह element को stack के top पर रखता है।
import java.util.Stack;
public class StackPushExample {
public static void main(String[] args) {
Stack<Integer> numbers = new Stack<>();
numbers.push(10);
numbers.push(20);
numbers.push(30);
System.out.println("Current Stack: " + numbers);
}
}
Output: Current Stack: [10, 20, 30]
2. Pop Operation
Pop() method top element को निकालता और return करता है। अगर stack खाली है और pop() call किया जाए तो यह EmptyStackException throw करता है।
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("Removed Element: " + stack.pop());
System.out.println("Updated Stack: " + stack);
Output: Removed Element: 3 और Updated Stack: [1, 2]
3. Peek Operation
Peek() method top element को show करता है लेकिन remove नहीं करता। यह तब useful होता है जब हमें सिर्फ top पर क्या है यह देखना हो।
Stack<String> names = new Stack<>();
names.push("Amit");
names.push("Ravi");
names.push("Karan");
System.out.println("Top Element: " + names.peek());
Output: Top Element: Karan
4. isEmpty() Method
isEmpty() method check करता है कि stack में कोई element है या नहीं। अगर stack खाली है तो यह true return करता है।
Stack<Character> stack = new Stack<>();
System.out.println(stack.isEmpty()); // true
stack.push('A');
System.out.println(stack.isEmpty()); // false
5. search() Method
search() method किसी element की position बताता है (1-based index) जो top से count होती है।
Stack<String> stack = new Stack<>();
stack.push("Red");
stack.push("Green");
stack.push("Blue");
System.out.println(stack.search("Green")); // Output: 2
How Stack Works Internally
Stack memory concept में दो operations होते हैं — Push और Pop। जब हम element डालते हैं तो उसे stack के top में push किया जाता है। जब हम निकालते हैं तो वही last element pop होकर बाहर आता है।
Example Flow:
- Push(10) → Stack: [10]
- Push(20) → Stack: [10, 20]
- Push(30) → Stack: [10, 20, 30]
- Pop() → Stack: [10, 20]
इस concept को ही LIFO (Last In First Out) कहा जाता है।
Real World Examples of Stack
1. Browser Back Button
जब आप browser में pages open करते हैं, हर page एक stack में store होता है। Back button दबाने पर last opened page सबसे पहले निकलता है — यही LIFO principle है।
2. Undo/Redo Feature
Text editors जैसे MS Word या VS Code में हर action stack में store होता है। जब आप undo करते हैं, तो last action सबसे पहले हटता है।
3. Function Call Stack
जब कोई program functions को call करता है, हर function call stack में push होता है। जैसे ही function पूरा होता है, वो pop होकर stack से निकलता है।
Stack Implementation (Without Using Built-in Stack Class)
अगर हम चाहें तो Java में stack को manually भी बना सकते हैं using Array या LinkedList।
Implementation using Array
class CustomStack {
int top = -1;
int[] stack = new int[5];
void push(int data) {
if (top == 4) {
System.out.println("Stack Overflow");
} else {
stack[++top] = data;
}
}
void pop() {
if (top == -1) {
System.out.println("Stack Underflow");
} else {
System.out.println("Popped: " + stack[top--]);
}
}
void display() {
for (int i = 0; i <= top; i++) {
System.out.print(stack[i] + " ");
}
System.out.println();
}
}
public class StackArrayExample {
public static void main(String[] args) {
CustomStack s = new CustomStack();
s.push(10);
s.push(20);
s.push(30);
s.display();
s.pop();
s.display();
}
}
Implementation using LinkedList
import java.util.LinkedList;
class StackUsingLinkedList {
LinkedList<Integer> list = new LinkedList<>();
void push(int value) {
list.addFirst(value);
}
void pop() {
if (list.isEmpty()) {
System.out.println("Stack Underflow");
} else {
System.out.println("Popped: " + list.removeFirst());
}
}
void peek() {
if (!list.isEmpty()) {
System.out.println("Top Element: " + list.getFirst());
}
}
void display() {
System.out.println("Stack Elements: " + list);
}
}
public class StackLinkedListExample {
public static void main(String[] args) {
StackUsingLinkedList s = new StackUsingLinkedList();
s.push(5);
s.push(10);
s.push(15);
s.display();
s.pop();
s.peek();
s.display();
}
}
Advantages and Disadvantages of Stack
Advantages
- Simple और efficient data structure है।
- Function calling, expression evaluation और recursion handling के लिए best है।
- Memory management आसान होता है।
Disadvantages
- Limited access — केवल top element से ही interaction होता है।
- Fixed size (अगर array से implement किया गया हो)।
- Overflow और underflow जैसी errors हो सकती हैं।
Use Cases of Stack in Java
- Parentheses balancing programs
- Expression evaluation (Postfix, Prefix)
- Browser history management
- Backtracking algorithms (maze solving, recursion)
- Memory management (call stack)
Java Stack Memory Concept
Java memory में दो main parts होते हैं — Heap और Stack। जब भी कोई method call होती है, तो उसके लिए एक stack frame allocate होता है। Function पूरा होने पर वह frame pop हो जाता है।
Stack memory fast होती है क्योंकि इसका access LIFO तरीके से होता है और यह local variables को manage करती है।
Stack Performance
Stack operations O(1) time complexity में execute होते हैं क्योंकि push और pop operations सिर्फ top element पर काम करते हैं।
| Operation | Time Complexity |
|---|---|
| Push | O(1) |
| Pop | O(1) |
| Peek | O(1) |
| Search | O(n) |
Summary
Java Stack एक powerful data structure है जो LIFO principle पर काम करता है। यह simple होने के साथ-साथ काफी useful है, खासकर जब हमें data को reverse order में process करना हो। चाहे recursion हो, function call tracking हो या expression evaluation — Stack हर जगह काम आता है।