Two pointers, front and rear, will indicate the start and end of the queue.
Enqueue Operation:
We add an element at the rear of the queue.
If the queue is full, we can’t add any more elements.
If the queue isn’t full, we move the rear pointer forward in a circular manner (using modulo %).
Dequeue Operation:
We remove an element from the front of the queue.
If the queue is empty, we can’t remove any elements.
After dequeuing, we move the front pointer forward in a circular manner.
Check for Empty and Full Conditions:
A queue is empty if front == -1.
A queue is full if (rear + 1) % maxSize == front.
Display Operation:
To display the queue, we need to traverse it starting from front to rear considering the circular nature.
Program in C
#include <stdio.h>
#define MAX 5 // Define the maximum size of the circular queue
// Circular Queue Structure
struct Circular Queue
{
int items[MAX];
int front, rear;
};
// Function to initialize the queue
void initialize Queue(struct Circular Queue *q)
{
q->front = -1;
q->rear = -1;
}
// Function to check if the queue is full
int is Full(struct Circular Queue *q)
{
if ((q->rear + 1) % MAX == q->front)
{
return 1;
}
return 0;
}
// Function to check if the queue is empty
int is Empty(struct Circular Queue *q)
{
if (q->front == -1)
{
return 1;
}
return 0;
}
// Function to add an element to the circular queue
void enqueue(struct Circular Queue *q, int value)
{
if (is Full(q))
{
printf("Queue is Full!\n");
}
else
{
if (q->front == -1) // Inserting the first element
q->front = 0;
q->rear = (q->rear + 1) % MAX;
q->items[q->rear] = value;
printf("Inserted %d\n", value);
}
}
// Function to remove an element from the circular queue
int dequeue(struct Circular Queue *q)
{
int element;
if (is Empty(q))
{
printf("Queue is Empty!\n");
return -1;
}
else
{
element = q->items[q->front];
if (q->front == q->rear)
{ // Queue has only one element
q->front = -1;
q->rear = -1;
}
else
{
q->front = (q->front + 1) % MAX;
}
printf("Deleted %d\n", element);
return element;
}
}
// Function to display the circular queue
void display(struct Circular Queue *q)
{
int i;
if (is Empty(q))
{
printf("Queue is Empty!\n");
}
else
{
printf("Front -> %d\n", q->front);
printf("Items -> ");
for (i = q->front; i != q->rear; i = (i + 1) % MAX)
{
printf("%d ", q->items[i]);
}
printf("%d ", q->items[i]);
printf("\nRear -> %d\n", q->rear);
}
}
// Main function
int main()
{
struct Circular Queue q;
initialize Queue(&q);
// Test enqueue operation
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50); // Queue is full now
// Test display operation
display(&q);
// Test dequeue operation
dequeue(&q);
dequeue(&q);
// Display after two dequeue operations
display(&q);
// Enqueue after dequeue operations
enqueue(&q, 60);
display(&q);
return 0;
}
Explanation:
initializeQueue():
Initializes the queue by setting both front and rear to -1, which indicates an empty queue.
isFull():
Checks if the queue is full by checking if (rear + 1) % MAX == front. This means that if the position just after rear is front, the queue is full.
isEmpty():
Checks if the queue is empty by verifying if front == -1.
enqueue():
Adds an element to the rear of the queue.
If the queue is empty, we set front to 0 because we are inserting the first element.
Then, we update the rear to the next position using (rear + 1) % MAX.
Finally, the value is inserted at the rear position.
dequeue():
Removes the front element from the queue.
If the queue becomes empty after dequeuing (i.e., front == rear), we reset both pointers to -1.
display():
Prints the queue from the front to the rear, accounting for the circular nature by looping through indices using modulo.