← Back to Core Java Course | Chapter 11: Collections | Lesson 14 of 17

Java में Stack और Queue

एक stack plates के एक pile जैसा है जहां आप top वाला पहले लेते हैं, और एक queue एक shop में एक line जैसी है जहां पहला person पहले serve होता है। वे control करते हैं कि चीज़ें किस order में बाहर आती हैं।
Syntax
java
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)

java
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
	}
}

Stack Search and Size

Stack top से एक element की 1-based distance ढूंढने के लिए search() भी support करता है, और capacity checks के लिए size()/isEmpty() — तब उपयोगी जब आपको इसके through pop किए बिना stack inspect करना हो।

उदाहरण: Stack Search and Size

java
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());
	}
}

Working with Queue (FIFO)

Queue offer() और poll() के through FIFO (first-in, first-out) behavior प्रदान करता है — पहला जोड़ा element हमेशा पहला हटाया जाने वाला है, line में इंतज़ार कर रहे लोगों जैसा।

उदाहरण: Working with Queue (FIFO)

java
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
	}
}

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

java
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());
	}
}

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

java
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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. नए code में legacy Stack class उपयोग करना, जब ArrayDeque जैसा Deque recommend किया जाता है।
  2. एक empty structure पर pop() या remove() call करना, जो एक exception throw करता है; null के लिए poll() या peek() उपयोग करें।
  3. LIFO (stack) को FIFO (queue) के साथ confuse करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.