Introduction to Stack: Last-In-First-Out Principle in Java
Introduction to Stack: Last-In-First-Out Principle in Java
जब हम Java programming में data structure की बात करते हैं, तो Stack एक बहुत ही important concept होता है। Stack एक linear data structure है जो Last-In-First-Out (LIFO) principle पर काम करता है। इसका मतलब है कि जो element सबसे आख़िर में insert किया जाता है, वही सबसे पहले remove होता है। इस principle को हम रोज़मर्रा की ज़िंदगी में भी देख सकते हैं, जैसे कि किताबों का ढेर — जहाँ ऊपर रखी किताब सबसे पहले उठाई जाती है।
What is Stack in Java?
Stack Java में एक class है जो java.util package का हिस्सा है। यह class Vector class को extend करती है और Stack की functionality provide करती है। Stack का use तब किया जाता है जब हमें data को temporary store करके manage करना हो — जैसे function call management, expression evaluation या backtracking algorithm में।
Basic Concept
Stack दो main operations को support करता है:
- push() – Stack के top पर नया element जोड़ने के लिए।
- pop() – Stack के top से element हटाने के लिए।
इसके अलावा Stack में peek() method भी होता है, जो top element को return करता है लेकिन उसे remove नहीं करता।
Stack की Working Mechanism (LIFO Principle)
Stack में elements को manage करने का तरीका बहुत simple है। जब भी कोई नया element add किया जाता है, वो हमेशा top पर जाता है, और जब remove किया जाता है, तो वही element पहले निकलता है।
उदाहरण के लिए:
Stack stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.pop()); // Output: 30
यहाँ 30 सबसे आख़िर में डाला गया element है, और वही सबसे पहले निकल रहा है — यही LIFO principle है।
Stack Operations in Java
1. push() Method
push() method का use नए element को Stack के top पर add करने के लिए किया जाता है। अगर Stack full नहीं है तो element successfully add हो जाता है।
stack.push("Data");
stack.push("Structure");
2. pop() Method
pop() method Stack के top से element को remove करता है। अगर Stack खाली है और हम pop() call करते हैं तो यह EmptyStackException throw करता है।
stack.pop(); // Removes top element
3. peek() Method
peek() method Stack के top element को देखता है लेकिन उसे remove नहीं करता। यह current top value को check करने के लिए useful होता है।
System.out.println(stack.peek());
4. empty() Method
empty() method check करता है कि Stack खाली है या नहीं। यह true या false return करता है।
if(stack.empty()){
System.out.println("Stack is empty");
}
5. search() Method
search() method किसी element की position (top से count करके) return करता है। अगर element नहीं मिला तो -1 return करता है।
int pos = stack.search("Java");
System.out.println(pos);
Implementation of Stack
Java में Stack को कई तरीकों से implement किया जा सकता है। सबसे common तरीके हैं:
- Using Stack Class
- Using Array
- Using LinkedList
1. Stack Class Implementation
import java.util.*;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(1);
s.push(2);
s.push(3);
System.out.println(s.pop());
}
}
2. Array-Based Implementation
इस method में हम Stack को manually manage करते हैं।
class StackArray {
int top = -1;
int max = 5;
int arr[] = new int[max];
void push(int data){
if(top == max - 1){
System.out.println("Stack Overflow");
}else{
arr[++top] = data;
}
}
int pop(){
if(top == -1){
System.out.println("Stack Underflow");
return -1;
}else{
return arr[top--];
}
}
}
3. LinkedList-Based Implementation
LinkedList का use करके Stack बनाना memory-efficient तरीका है क्योंकि इसमें dynamic size होता है।
import java.util.LinkedList;
class StackLL {
LinkedList<Integer> list = new LinkedList<>();
void push(int data){
list.addFirst(data);
}
int pop(){
if(list.isEmpty()){
System.out.println("Stack Underflow");
return -1;
}
return list.removeFirst();
}
}
Applications of Stack
Stack का use कई practical situations में किया जाता है।
- Expression Evaluation (Postfix, Prefix, Infix)
- Function Call Management (Call Stack)
- Undo/Redo Operations (Text Editors)
- Browser History Navigation
- Backtracking Algorithms जैसे maze solving
- Parenthesis Checking और Syntax Validation
Internal Working of Stack
जब हम Stack में data push करते हैं, तो JVM internal memory में stack structure बनाती है, जिसमें हर push operation के साथ pointer top increment होता है। इसी तरह pop operation top को decrement करता है।
| Operation | Top Value | Stack Content |
|---|---|---|
| push(10) | 0 | 10 |
| push(20) | 1 | 10, 20 |
| pop() | 0 | 10 |
Advantages of Stack
- Memory management easy होता है।
- Operations fast और simple हैं।
- Recursion और expression handling में helpful है।
Disadvantages of Stack
- Fixed size (in case of array-based stack)।
- Access random elements नहीं किया जा सकता।
- Stack overflow या underflow का risk रहता है।
Difference between Stack and Queue
| Factor | Stack | Queue |
|---|---|---|
| Working Principle | LIFO | FIFO |
| Insertion | push() at Top | enqueue() at Rear |
| Deletion | pop() from Top | dequeue() from Front |
| Example | Plate Stack | People Queue |
Real Life Example of Stack
जब आप किसी browser में pages visit करते हैं और “back” बटन दबाते हैं, तो Stack concept ही काम करता है। सबसे आख़िरी में खोला गया page सबसे पहले close होता है। इसी तरह text editor में Ctrl+Z (undo) भी Stack पर आधारित होता है।
Stack in Java 8 and Modern Approach
Java 8 के बाद से Stack class को थोड़ा पुराना माना जाता है और इसकी जगह Deque interface या ArrayDeque को use करने की सलाह दी जाती है क्योंकि यह faster और thread-safe नहीं होता।
Deque<Integer> stack = new ArrayDeque<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // Output: 20
Important Notes for Exam
- Stack works on LIFO (Last-In-First-Out) principle।
- push(), pop(), peek(), empty(), search() इसके core methods हैं।
- Array और LinkedList दोनों से implement किया जा सकता है।
- Stack overflow तब होता है जब Stack full हो जाए।
- Stack underflow तब होता है जब Stack खाली हो और हम pop() करें।
- Postfix और Prefix expression evaluation में Stack का उपयोग किया जाता है।
- Java में Stack class Vector को extend करती है।
Summary (Quick Revision Notes)
| Topic | Key Point |
|---|---|
| Definition | Linear data structure based on LIFO |
| Important Methods | push(), pop(), peek(), empty(), search() |
| Implementation | Using Array, LinkedList, Stack class |
| Main Use | Expression evaluation, recursion, undo/redo |
| Java Package | java.util |