# 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.

## 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 –

## 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

### Code for Circular Queue in Data Structure

`#include <stdio.h>#define SIZE 6int array[SIZE];int front = -1, rear = -1;// Function to check if the queue is fullint isFull(){    if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)){        return 1;    }    return 0;}// Function to check if the queue is emptyint isEmpty(){    if (front == -1)     {        return 1;    }  return 0;}// Enqueueing in Queuevoid 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 Queueint 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 queuevoid 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 empty10 was added20 was added30 was added40 was added50 was addedThe queue looks like: 10 20 30 40 5010 dequeued20 dequeuedThe queue looks like: 30 40 5060 was added70 was added80 was added Can't add the queue is full The queue looks like: 30 40 50 60 70 80`