Queue Operations in C: Step-by-Step Guide
1. Initialization of the Queue
Linear Queue Initialization:
int front = -1, rear = -1;
Circular Queue Initialization:
int front = 0, rear = 0;
Explanation: Initialize the front and rear indices. In a circular queue, both indices start at 0 to utilize
the array fully.
2. Check if the Queue is Empty
Method 1 (Linear Queue):
if (front == -1 || front > rear) return 1;
Method 2 (Circular Queue):
if (front == rear) return 1;
Explanation: In a circular queue, an empty queue has both front and rear pointing to the same index.
3. Check if the Queue is Full
Method 1 (Linear Queue):
if (rear == MAX - 1) return 1;
Method 2 (Circular Queue):
if ((rear + 1) % MAX == front) return 1;
Explanation: A circular queue is full when the next position of rear is equal to the front index.
4. Insert into the Queue (Enqueue)
Linear Queue:
if (rear == MAX - 1) printf("Overflow"); else queue[++rear] = element;
Circular Queue:
if ((rear + 1) % MAX == front) printf("Overflow"); else {
rear = (rear + 1) % MAX; queue[rear] = element;
Explanation: Increment rear and insert the element. In a circular queue, use modulo to wrap around.
5. Remove from the Queue (Dequeue)
Linear Queue:
if (front == -1 || front > rear) printf("Underflow"); else return queue[front++];
Circular Queue:
if (front == rear) printf("Underflow"); else {
front = (front + 1) % MAX; return queue[front];
Explanation: Increment front to remove the element. In a circular queue, use modulo to handle
wrap-around.
6. Explanation of Circular Queue Advantages
1. Efficient utilization of space.
2. Handles wrap-around without shifting elements.
3. Suitable for real-time applications like buffering and scheduling.