Queue Operations
A queue is a linear data structure that follows the FIFO (First-In, First-Out) principle, where the first element inserted is the first one to be removed.
All operations in a queue revolve around two pointers:
- Front: points to the first element (deletion happens here)
- Rear: points to the last element (insertion happens here)
Understanding queue operations is important because they control how data is inserted, accessed, and removed in an ordered manner.
1. Enqueue Operation (Insertion)
The enqueue operation inserts a new element at the rear of the queue.
Before inserting, we must check whether the queue is full (overflow condition).
Key Idea:
- If queue is empty → initialize
front = 0 - Increase
rearand insert element
Algorithm:
if REAR == MAX_SIZE - 1 then
Output "Queue Overflow"
else
if FRONT == -1 then
FRONT ← 0
end if
REAR ← REAR + 1
QUEUE[REAR] ← ITEM
end ifExample:
Queue: [10, 20]
Insert 30 → Queue becomes: [10, 20, 30]
2. Dequeue Operation (Deletion)
The dequeue operation removes an element from the front of the queue.
Before deleting, we must check whether the queue is empty (underflow condition).
Key Idea:
- Remove element at
front - Move
frontforward - Reset queue if it becomes empty
Algorithm:
if FRONT == -1 or FRONT > REAR then
Output "Queue Underflow"
else
ITEM ← QUEUE[FRONT]
FRONT ← FRONT + 1
if FRONT > REAR then
FRONT ← -1
REAR ← -1
end if
end ifExample:
Queue: [10, 20, 30]
Remove → 10 removed → Queue becomes: [20, 30]
3. Peek Operation (Front Element Access)
The peek operation returns the front element without removing it. Used to check which element will be removed next
Algorithm:
if FRONT == -1 or FRONT > REAR then
Output "Queue is Empty"
else
return QUEUE[FRONT]
end ifExample:
Queue: [20, 30]
Peek → Output: 20
4. isEmpty Operation
This operation checks whether the queue has no elements.
Condition:
- Queue is empty if:
FRONT == -1ORFRONT > REAR
Algorithm:
if FRONT == -1 or FRONT > REAR then
return TRUE
else
return FALSE
end ifExample:
Queue: [] → Empty
Queue: [10] → Not Empty
5. isFull Operation
This operation checks whether the queue is full (in array implementation).
Condition:
- Queue is full if:
REAR == MAX_SIZE - 1
Algorithm:
if REAR == MAX_SIZE - 1 then
return TRUE
else
return FALSE
end ifExample:
Queue size = 5
Queue: [10, 20, 30, 40, 50] → Full
6. Display Operation
This operation prints all elements from front to rear.
Algorithm:
if FRONT == -1 or FRONT > REAR then
Output "Queue is empty"
else
for i ← FRONT to REAR do
Output QUEUE[i]
end for
end ifExample:
Queue: [10, 20, 30]
Output → 10 20 30
Complete Queue Implementation in C
#include <stdio.h>
#define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1, rear = -1;
// Check if queue is full
int isFull() {
return rear == MAX_SIZE - 1;
}
// Check if queue is empty
int isEmpty() {
return front == -1 || front > rear;
}
// Enqueue operation
void enqueue(int item) {
if (isFull()) {
printf("Queue Overflow - Cannot insert %d\n", item);
return;
}
if (front == -1)
front = 0;
rear++;
queue[rear] = item;
printf("Inserted %d\n", item);
}
// Dequeue operation
void dequeue() {
if (isEmpty()) {
printf("Queue Underflow\n");
return;
}
printf("Deleted %d\n", queue[front]);
front++;
if (front > rear) {
front = rear = -1;
}
}
// Peek operation
void peek() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}
printf("Front element: %d\n", queue[front]);
}
// Display operation
void display() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}
printf("Queue: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
// Main function
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
peek();
dequeue();
display();
dequeue();
dequeue();
dequeue(); // Underflow
return 0;
}Time and Space Complexity
| Operation | Time Complexity |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Peek | O(1) |
| isEmpty | O(1) |
| isFull | O(1) |
| Display | O(n) |
Space Complexity: O(n) depends on number of elements stored in queue
Conclusion
Queue operations ensure that data is handled in a strict order (FIFO).
They are simple but extremely powerful in real-world systems like scheduling, buffering, and processing tasks in sequence.