Circular Queue

Arife Gül Yalçın
5 min readApr 19, 2021

--

The Circular Queue is a linear data structure where the end position is connected back to the initial position to form a circle. There are 3 main operations for the Circular Queue. These;

1. enqueue() Operation

2. dequeue() Operation

3. display() Operation

First I should explain why a circular queue is needed when there is already a linear queue data structure.

In linear queue, when the queue is completely filled, it is not possible to add more values. Because when the values are deleted, we actually move the front of the tail forward. So we consider the queue smaller. It is necessary to reset the linear queue to add new value.

The Circular Queue is a linear data structure that follows the FIFO principle. FIFO stands for first-in-first-out. Circular Queue, on the other hand, restarts the queue from the first position after the end, rather than ending it at the last position.

As seen in Figure 1, there are 3 different options and 1 exit option for Circular Queue operations. These transactions;

1) INSERT

2) DELETE

3) DISPLAY

4) EXIT

The user must first INSERT value to the queue.

Figure 1 Circular Queue Operations

1. INSERT ( enqueue() )

>It is checked whether the queue is full.

>It is set as 0 FRONT value in the array for the first element.

>If rear = MAX-1 and front are not equal to 0, the array is declared empty.

>Each added value takes place in the array in order (the REAR index is incremented circularly by 1 each in the array order)

Figure 3.1. Insertion Value
Figure 3.1.1. Circular Queue is Full

2. DELETE ( dequeue() )

>It is checked if there is a value in the row ( front = -1 and rear = -1, the array is empty and cannot be deleted ).

>Return the value indicated by front

>Increase front index by 1 circularly

>Reset the ‘front’ and ‘rear’ values to -1 for the last element.

Figure 3.2. Delete Value

3. DISPLAY ( display() )

>It is checked whether the queue is empty.

>If the row is not empty ‘front’ is assigned a value of i.

>If front <= rear, the value of i is displayed sequentially until rear.

>If i <= MAX-1, it is displayed sequentially until rear.

Figure 3.3. Before Delete Value
Figure 3.3.1. After Delete Value

Circular Queue C Code

#include<stdio.h>
#include<stdlib.h> // for exit()
#include<conio.h> // for getch();
#define MAX 7 // Size of array

void enqueue(int); // enqueue() operation is used to add an value to the queue
void dequeue(); // dequeue() operation is used to delete(remove) an value to the queue
void display(); // display() operation is used to display values in the queue.

int countqueue[MAX]; // It is defined to count the size of the array.
int front = -1, rear = -1; // Global value of front and rear.

int main() {

int value;

do {
printf(“\n\t\t/-/ CIRCULAR QUEUE /-/ \n\n 1-INSERT \n\n 2-DELETE\n\n 3-DISPLAY\n\n 4-EXIT\n\n”);
switch (getch()){ // getch() -> This function is available in the conio.h library. It is used to get a character from the user.

case ‘1’:
printf(“\nEnter the value to be insert: “);
scanf(“%d”,&value); // Takes value from the user
enqueue(value); // Goes to the enqueue(int value) operation defined.
break;

case ‘2’: dequeue(); // Goes to the dequeue() operation defined.
break;

case ‘3’: display(); // Goes to the display() operation defined.
break;

case ‘4’: { // for EXIT operation
return 0;
break;
}

default:
printf(“Error!”);
system(“CLS”);
}
}

while(1);
getch();
return 0;

}

void enqueue(int value)
{
if((front == 0 && rear == MAX — 1) || (front == rear+1)) // Statuses indicating that the Circular Queue is full
printf(“\n Circular Queue is Full \n”);
else{
if(front == -1) // If front = -1, this indicates that the array is empty and front will be equal to 0, the first place in the array. (front = 0)
front = 0;

// If rear = MAX-1 and front are not equal to 0, the array is declared empty.
//Each added value takes place in the array in order.
if(rear == MAX-1 && front != 0)
rear = -1;
countqueue[++rear] = value;
printf(“\nInserted Value -> %d \n”);

}
}

void dequeue()
{
// In the case of front = -1 and rear = -1, the array is empty and cannot be deleted.
if(front == -1 && rear == -1)
printf(“\nDeletion cannot be Performed, because the Circular Queue is Empty.\n”);


else{
printf(“\nDeleted Value : %d\n”,countqueue[front++]); // increase front index by 1 circularly
if(front == MAX)
front = 0;

if(front-1 == rear)
front = rear = -1; // Reset the FRONT and REAR values to -1 for the last element

}
}
void display()
{
if(front == -1)
printf(“\nCircular Queue is Empty\n”);
else{

int i = front;
printf(“\nCircular Queue Elements are : \n”);

if(front <= rear){

while(i <= rear)
printf(“%d\t”,countqueue[i++]);

}

else{

while(i <= MAX — 1)
printf(“%d\t”, countqueue[i++]);

i = 0;
while(i <= rear)
printf(“%d\t”,countqueue[i++]);

}
}
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arife Gül Yalçın
Arife Gül Yalçın

Written by Arife Gül Yalçın

Backend Developer @GrandMedicalGroup

No responses yet

Write a response