Dremendo Tag Line

Circular Queue in C Programming

Data Structure in C

In this lesson, we will understand what is Circular Queue in C Programming and how to create them along with some examples.

What is Circular Queue in C

A Circular Queue in C is a data structure in which elements are stored in a circular manner. In Circular Queue, after the last element, the first element occurs.

A Circular Queue is used to overcome the limitation we face in the array implementation of a Queue. The problem is that when the rear reaches the end and if we delete some elements from the front and then try to add a new element in the queue, it says "Queue is full", but still there are spaces available in the queue. See the example given below.

c queue example

In the above image, the queue is full because the rear R reached the end of the queue. We have deleted two elements from the queue, so the front F is at index 2. We can see that there are spaces available in the queue, but we can't add a new element because the rear can't go back to index 0.

video-poster

Operation on Circular Queue

There are two operations possible on the circular queue.

  • Add - When we add an element in the circular queue.
  • Delete - When we delete an element from the circular queue.

To understand how the above operations work on a circular queue. See the example given below.

c circular queue using array

From the above image, we can see that when we add a new element in the circular queue, the variable R is increased by R=(R+1)%Size, and the new element is added at the new position of R and te is increased by 1. Similarly, when we delete an element from the circular queue, the variable F is increased by F=(F+1)%Size and te is decreased by 1.

Add Operation in Circular Queue

For add operation in the circular queue first, we check if the value of te is equal to the value of size then, we will display a message Queue is full, else we will increase the value of R by R=(R+1)%Size and add the element in the array at the new location of R and then increased the value of te by 1.

Example

if(te==size)
{
	printf("Queue is full\n");
}
else
{
	R=(R+1)%size;
	arr[R]=new_item;
    te=te+1;
}

Delete Operation in Circular Queue

For delete operation in the circular queue first, we check if the value of te is 0 then, we will display a message Queue is empty, else we will display the deleted element on the screen and then increase the value of F by F=(F+1)%Size and then decrease the value of te by 1.

Example

if(te==0)
{
	printf("Queue is empty\n");
}
else
{
	printf("Element Deleted = %d",arr[F]);
	F=(F+1)%size;
    te=te-1;
}

Program of Circular Queue using Array

Below is the complete program of circular queue in C using an array having size 5.

Circular Queue Program in C using Array

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define size 5

int main()
{
    int arr[size],R=-1,F=0,te=0,ch,n,i,x;

    for(;;)		// An infinite loop
    {
        system("cls");		// for clearing the screen
        printf("1. Add\n");
        printf("2. Delete\n");
        printf("3. Display\n");
        printf("4. Exit\n");
        printf("Enter Choice: ");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                if(te==size)
                {
                    printf("Queue is full");
                    getch();	// pause the loop to see the message
                }
                else
                {
                    printf("Enter a number ");
                    scanf("%d",&n);
                    R=(R+1)%size;
                    arr[R]=n;
                    te=te+1;
                }
                break;

            case 2:
                if(te==0)
                {
                    printf("Queue is empty");
                    getch();	// pause the loop to see the message
                }
                else
                {
                    printf("Number Deleted = %d",arr[F]);
                    F=(F+1)%size;
                    te=te-1;
                    getch();	// pause the loop to see the number
                }
                break;

            case 3:
                if(te==0)
                {
                    printf("Queue is empty");
                    getch();	// pause the loop to see the message
                }
                else
                {
                    x=F;
                    for(i=1; i<=te; i++)
                    {
                        printf("%d ",arr[x]);
                        x=(x+1)%size;
                    }
                    getch();	// pause the loop to see the numbers
                }
                break;

            case 4:
                exit(0);
                break;

            default:
                printf("Wrong Choice");
                getch();	// pause the loop to see the message
        }
    }
    return 0;
}

In the above program, we have defined a macro named size having value 5 using the statement #define. We can use the word size to declare the size of the array as int arr[size]. When we run the above program, the word size will be replaced by its value 5. So the size of the array will be 5.