On this page we will discuss about Circular queue in C . Circular queues are made to overcome the problems faced by normal queues of size reduction whenever Dequeueing any element.
Circular Queues how they work?
Circular queues have the same terminologies and function is queues. The core of the code lies in using remainder operator with size in most operations as given in the code below.
Also enqueue, dequeue, front, rear etc work as queues itself, if you’re unaware of these, we recommend check this page first.
Important to note why Circular Queues?
Once the queue becomes full, we can not insert more elements. Even though we dequeue a few of the elements.
Because the following code –
if (rear == SIZE-1)
printf("Overflow condition");
After each enqueue, rear value is incremented & once queue is full rear value becomes equal to size – 1
void Enqueue()
{
//Other part of code
rear = rear + 1;
//other part of code
}
So no more elements can be entered & even if dequeue happens, it only changes the front value & not rear value.
Thus, with dequeues effective size of queue is reduced
#include <stdio.h>
#include <stdlib.h>
#define SIZE 6
struct Queue
{
int front, rear, currSize;
unsigned maxSize;
int *a;
};
struct Queue *createQueue (unsigned maxSize)
{
struct Queue *queue = (struct Queue *) malloc (sizeof (struct Queue));
queue->maxSize = maxSize;
queue->front = queue->rear = -1;
queue->a = (int *) malloc (queue->maxSize * sizeof (int));
return queue;
}
// Checking for Overflow condition
int isFull (struct Queue *queue)
{
if ((queue->front == queue->rear + 1) ||
(queue->front == 0 && queue->rear == queue->maxSize - 1))
{
return 1;
}
return 0;
}
// Checking for Underflow condition
int isEmpty (struct Queue *queue)
{
if (queue->front == -1)
{
return 1;
}
return 0;
}
// Function to do enqueue
void enqueue (struct Queue *queue, int value)
{
if (isFull (queue))
printf ("Can't add the queue is full \n");
else
{
if (queue->front == -1)
queue->front = 0;
queue->rear = (queue->rear + 1) % queue->maxSize;
queue->a[queue->rear] = value;
printf ("%d was added\n", value);
}
}
// Function to do dequeue
int dequeue (struct Queue *queue)
{
int item;
if (isEmpty (queue))
{
printf ("Can't add the queue is empty \n");
return (-1);
}
else
{
item = queue->a[queue->front];
if (queue->front == queue->rear)
{
queue->front = queue->rear = -1;
}
else
{
queue->front = (queue->front + 1) % queue->maxSize;
}
printf ("%d dequeued\n", item);
}
}
// Function to print the queue
void print (struct Queue *queue)
{
int i;
if (isEmpty (queue))
printf ("Empty Queue\n");
else
{
printf ("\nThe items in the queue are : \n");
for (i = queue->front; i != queue->rear; i = (i + 1) % queue->maxSize)
{
printf ("%d ", queue->a[i]);
}
printf ("%d \n\n", queue->a[i]);
}
}
int main ()
{
struct Queue *queue = createQueue (6);
dequeue (queue); //Underflow condition
enqueue (queue, 12);
enqueue (queue, 14);
enqueue (queue, 16);
enqueue (queue, 18);
enqueue (queue, 20);
print (queue);
dequeue (queue);
dequeue (queue);
print (queue);
enqueue (queue, 22);
enqueue (queue, 24);
enqueue (queue, 26);
enqueue (queue, 28); //Overflow condition
print (queue);
return 0;
}
Output –
Can't add the queue is empty
12 was added
14 was added
16 was added
18 was added
20 was added
The items in the queue are : 12 14 16 18 20
12 dequeued
14 dequeued
The items in the queue are : 16 18 20
22 was added
24 was added
26 was added
Can't add the queue is full
The items in the queue are : 16 18 20 22 24 26
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment