What is a Queue?
Imagine you’re standing in line at a movie ticket counter.
The person who arrives first gets the ticket first, right?
And if you come late, you’ll have to wait until it’s your turn.
That’s exactly how a Queue works in programming!
A Queue is a linear data structure that follows the rule:
👉 FIFO – First In, First Out.
It means:
- The first element added to the queue is the first one removed.
- The last one added will have to wait for its turn.
So, if we think of it like a line of people:
- New people join at the end (rear).
- Service happens at the front.
🎯 Basic Concept
A queue has two ends:
- Front – where elements are removed.
- Rear – where elements are added.
Whenever you insert something, it goes to the rear.
Whenever you remove something, it comes out from the front.
🧱 Diagram of a Queue
Let’s picture it simply:
Front → [10] [20] [30] [40] ← Rear
- If we enqueue (add) an element, it goes after 40.
- If we dequeue (remove) an element, 10 leaves first.
That’s FIFO in action!
⚙️ Main Operations on a Queue
Let’s understand the basic actions we can perform on a queue.
🥇 1. Enqueue (Insertion)
This means adding a new element at the rear of the queue.
Example:
If your queue is [10, 20, 30]
and you enqueue 40, the new queue becomes:[10, 20, 30, 40]
Here,
10is still at the front,40is added at the rear.
Note:
If the queue is full and you still try to insert more, that’s called a Queue Overflow.
🥈 2. Dequeue (Deletion)
This means removing an element from the front of the queue.
Example:
If your queue is [10, 20, 30, 40]
and you dequeue one element,
the new queue becomes: [20, 30, 40]
10 — the first element — was removed.
Note:
If the queue is empty and you try to remove something, that’s called Queue Underflow.
🥉 3. Peek (Front Element)
Sometimes, you don’t want to remove anything —
you just want to see which element is at the front.
That’s what Peek does.
It simply shows the element at the front without changing the queue.
Example:
If the queue is [10, 20, 30], then Peek() = 10
🏅 4. Display
This operation simply prints or displays all the elements of the queue — from front to rear.
Example:
If the queue is [10, 20, 30, 40],
the display will look like this:
Front → 10 20 30 40 ← Rear
📘 Real-Life Examples of Queues
To make it easy, let’s look at where we see queues around us every day:
- 🚌 Bus Stop Line: People enter at the end and leave from the front.
- 🖨️ Printer Queue: Print jobs are handled one by one in the order they were added.
- ⏳ Customer Support: Your call is answered in the same order you joined the waiting list.
- 🚦 Traffic Line: Cars move in and out of a lane in the same sequence.
So, queues are not just a programming concept — they reflect real life!
🧮 Types of Queues
There are a few variations of the basic queue, depending on how you use it.
1. Simple Queue
- Normal FIFO structure.
- Elements added at rear, removed from front.
2. Circular Queue
- The last position connects back to the first, forming a circle.
- Useful to efficiently use memory.
3. Priority Queue
- Each element has a priority.
- The element with higher priority is served first (not just the one added first).
4. Double-Ended Queue (Deque)
- Elements can be added or removed from both ends (front and rear).
💻 Simple Queue Diagram (Step by Step)
Step 1: Empty Queue
Front = -1, Rear = -1
Step 2: Enqueue(10)
[10]
Front → 0, Rear → 0
Step 3: Enqueue(20)
[10, 20]
Front → 0, Rear → 1
Step 4: Enqueue(30)
[10, 20, 30]
Front → 0, Rear → 2
Step 5: Dequeue()
[20, 30]
Front → 1, Rear → 2
Each enqueue moves the rear forward,
and each dequeue moves the front forward.
⚠️ Common Queue Errors
| Error Type | When It Happens |
|---|---|
| Overflow | When the queue is full and you try to add more elements. |
| Underflow | When the queue is empty and you try to remove an element. |
Think of a ticket counter again:
- If the line is already packed to the door — you can’t join (overflow).
- If there’s no one left in line — you can’t remove anyone (underflow).
💻 Simple Pseudocode Example
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
void enqueue(int value) {
if (rear == MAX - 1)
printf("Queue Overflow");
else {
if (front == -1)
front = 0;
queue[++rear] = value;
}
}
int dequeue() {
if (front == -1 || front > rear)
printf("Queue Underflow");
else
return queue[front++];
}
int peek() {
if (front != -1)
return queue[front];
}