Circular Queue in Data Structure

Data Structure : Circular Queue

Circular queues come into the picture to overcome the limitations of simple queues, in this post we will learn more about the same.

TCS NQT Registration Steps Large

Why Circular Queues are needed ?

When we talk about linear queues, once the queue becomes full, we can not insert more elements. Even though we dequeue a few of the elements, only once all the elements are dequeued then only queue is reset and then new elements can be inserted. The example of the same is shown in image below –

Circular Queues in Data Structures Limitations with Linear Queues
Circular Queues in DSA

How Circular Queues work in Data Structure

Circular queues work very similarly as linear queues with minor addition and enhancements. Any circular queue as the following –

  • Front – The starting head of the circular queue
  • Rear – The ending tail of the circular queue
  • Enqueue Operation – Process of adding a new item in the queue
  • Dequeue Operation – Process of removing an existing item from the queue
  • Overflow Condition – When the queue is full
  • Underflow Condition – When queue is empty
  • Size – The max number of items the circular queue can hold
Circular Queues in Data Structure and Algorithms

Code for Circular Queue in Data Structure

#include <stdio.h>
#define SIZE 6

int array[SIZE];
int front = -1, rear = -1;

// Function to check if the queue is full
int isFull(){
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)){
return 1;
}
return 0;
}

// Function to check if the queue is empty
int isEmpty(){
if (front == -1)
{
return 1;
}
return 0;
}

// Enqueueing in Queue
void enqueue(int value){
if (isFull())
printf("Can't add the queue is full \n");

else
{
if (front == -1)
front = 0;

rear = (rear + 1) % SIZE;
array[rear] = value;
printf("%d was added\n", value);
}
}

// Dequeueing in Queue
int dequeue() {
int element;
if (isEmpty()) {
printf("Can't add the queue is empty \n");
return (-1);
} else {
element = array[front];
//The has only 1 item so we need to put rear and front values to - 1
//After dequeue queue is empty
if (front == rear) {
front = -1;
rear = -1;
}

else {
front = (front + 1) % SIZE;
}
printf("%d dequeued\n", element);
return (1);
}
}

// Display the queue
void print(){
int i;
if (isEmpty())
printf("Empty Queue\n");
else
{
printf("\nThe queue looks like: \n");
for (i = front; i != rear; i = (i + 1) % SIZE)
{
printf("%d ", array[i]);
}
printf("%d \n\n", array[i]);

}
}

int main() {
// Can't as no elements in the queue both front & rear at -1
dequeue();

enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);

print();
dequeue();
dequeue();

print();

enqueue(60);
enqueue(70);
enqueue(80);
enqueue(90);//can't add the queue is full
print();

return 0;
}

Output

Can't add the queue is empty
10 was added
20 was added
30 was added
40 was added
50 was added

The queue looks like: 10 20 30 40 50
10 dequeued
20 dequeued

The queue looks like: 30 40 50

60 was added
70 was added
80 was added

Can't add the queue is full

The queue looks like: 30 40 50 60 70 80