Java में Stack और Queue
In this page:
Stack<Type> stack = new Stack<>();
stack.push(item);
stack.pop();
Queue<Type> queue = new LinkedList<>();
queue.offer(item);
queue.poll();
Working with Stack (LIFO)
Stack (एक legacy Vector subclass) push() और pop() के through LIFO (last-in, first-out) behavior प्रदान करता है — सबसे हाल ही में जोड़ा element हमेशा पहला हटाया जाने वाला है, plates के एक stack जैसा।
उदाहरण: Working with Stack (LIFO)
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
System.out.println(stack.pop()); // 2: last in, first out
}
}
Login to try C/C++/Java/PHP code in the editor
Stack Search and Size
Stack top से एक element की 1-based distance ढूंढने के लिए search() भी support करता है, और capacity checks के लिए size()/isEmpty() — तब उपयोगी जब आपको इसके through pop किए बिना stack inspect करना हो।
उदाहरण: Stack Search and Size
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
System.out.println(stack.search(10)); // 1-based distance from top
System.out.println(stack.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Working with Queue (FIFO)
Queue offer() और poll() के through FIFO (first-in, first-out) behavior प्रदान करता है — पहला जोड़ा element हमेशा पहला हटाया जाने वाला है, line में इंतज़ार कर रहे लोगों जैसा।
उदाहरण: Working with Queue (FIFO)
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
System.out.println(queue.poll()); // 1: first in, first out
}
}
Login to try C/C++/Java/PHP code in the editor
Modern Stack and Queue with Deque
ArrayDeque आज दोनों roles के लिए आमतौर पर पुराने Stack और LinkedList-as-queue से ज़्यादा पसंद किया जाता है, क्योंकि यह तेज़ है और इसे explicitly एक modern double-ended queue implementation के रूप में design किया गया।
उदाहरण: Modern Stack and Queue with Deque
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>(); // preferred over Stack/LinkedList
deque.push(1);
deque.push(2);
System.out.println(deque.pop());
}
}
Login to try C/C++/Java/PHP code in the editor
Handling Queue Edge Cases
एक empty stack या queue पर pop()/poll() call करना या तो एक exception throw करता है या null return करता है इस आधार पर कि आप कौन सा method उपयोग करते हैं — पहले isEmpty() जांचें, या peek/poll variants उपयोग करें जो throw करने के बजाय null return करते हैं।
उदाहरण: Handling Queue Edge Cases
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
System.out.println(queue.poll()); // null: empty queue, no exception
System.out.println(queue.isEmpty());
}
}
Login to try C/C++/Java/PHP code in the editor
- नए code में legacy
Stackclass उपयोग करना, जबArrayDequeजैसाDequerecommend किया जाता है। - एक empty structure पर
pop()याremove()call करना, जो एक exception throw करता है;nullके लिएpoll()याpeek()उपयोग करें। - LIFO (stack) को FIFO (queue) के साथ confuse करना।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting