Queue in Data Structures Explained in Simple Language (FIFO with Java Example)
- Get link
- X
- Other Apps
Queue Kya Hoti Hai?
Queue ek data structure hai jo FIFO principle follow karti hai.
FIFO = First In, First Out
Matlab jo sabse pehle aaya, wahi sabse pehle bahar jayega.
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. This means the element that is inserted first will be removed first. In a queue, insertion of elements happens at one end called the rear, and deletion happens at the other end called the front. Unlike stacks, where insertion and deletion happen at the same end, queues maintain order based on arrival time. Queues are widely used in scheduling systems, resource management, and real-time data processing.
What is FIFO?
FIFO stands for First In, First Out. It means that the element that enters the data structure first will be the first one to leave. This principle ensures fairness in processing. FIFO is commonly seen in daily life situations like waiting lines, where the person who arrives first gets served first. In programming, FIFO helps maintain processing order in tasks and requests.
Real Life Examples (Student Friendly)
Example 1 – Movie Ticket Counter
-
Rahul line me sabse pehle khada hua
-
Fir Aman
-
Fir Sita
Ticket kisko milega?
👉 Rahul ko
Kyunki Queue me pehle aane wale ko pehle service milti hai.
Example 2 – Printer Queue
Agar 3 log print command dete hain:
Document A
Document B
Document C
Printer pehle A print karega, fir B, fir C.
Ye queue system hai.
Basic Operations of Queue
-
Enqueue
Element ko rear (end) me add karna. -
Dequeue
Front (first element) ko remove karna. -
Front
Sabse pehle element ko dekhna. -
Rear
Sabse last element ko dekhna.
Stack vs Queue (Simple Difference)
Stack → Last In First Out
Queue → First In First Out
Stack me upar se entry-exit
Queue me ek side se entry, dusri side se exit
Java Me Simple Example (Array Se)
public class SimpleQueue {
static int[] queue = new int[5];
static int front = 0;
static int rear = -1;
static void enqueue(int value) {
queue[++rear] = value;
}
static void dequeue() {
System.out.println("Removed: " + queue[front++]);
}
public static void main(String[] args) {
enqueue(10);
enqueue(20);
enqueue(30);
dequeue(); // 10 remove hoga
}
}
Output:
10
Kyun?
Kyuki 10 sabse pehle add hua tha.
Queue Important Kyun Hai?
CPU Scheduling me
Printer queue me
Call center waiting system me
BFS (Graph Traversal) me
Time Complexity
-
Enqueue → O(1)
-
Dequeue → O(1)
-
Front → O(1)
Queue operations fast hote hain.
Advantages of Queue
-
Order maintain karta hai
-
Fair processing system
-
Real-time systems me useful
Disadvantages of Queue
-
Random access nahi hota
-
Fixed size array me overflow risk
Where Queue is Used?
-
CPU scheduling
-
Task management systems
-
Call center systems
-
Graph traversal (BFS)
YouTube Link:- QUEUE
Ek Line Me Samjho
Queue ek aisa data structure hai jisme entry piche se hoti hai aur exit aage se hoti hai.
Chhota Test
Agar queue me 5, 8, 12 add kiya gaya ho
to remove karne par kaunsa number niklega?
Socho 🤔
- Get link
- X
- Other Apps
Comments
Post a Comment