Circular Queue in C - its algorithm, how it's works & menu driven program

In this tutorial, we learn about circular queue in c, its algorithm, how it works, and also describe menu driven program for circular queue with output.

Circular queue in data structure

Circular queue is linear data structure that is implemented in circular form rather than a straight array and avoids the wastage of space in queue using arrays. If we joint both ends of the linear queue then it overcomes the problem of unutilized space in linear queue implemented as an array. 

circular queue in data structure
Circular Queue in Data Structure


How It Work's



Circular queue works by these two operations Insertion and deletion queue as array. It stored in the memory as array in c. 

Queue operations work as follows

In case of insertion of elements, the first element insertion takes place in the circular queue then you have to increase your front and rear from "front=-1" and "rear=-1" then you have to shift both with an increment of +1 and your front and rear at "front=0" and "rear=0" then you insert your first element otherwise your "front=0" is fixed and increment your "rear=rear+1" to insert more. 

In case of deletion of elements, if "front==-1" than deletion is not possible then check front and rear both are at the same place then a front and rear both shifted to "front=-1" and "rear=-1" otherwise "front==size-1"(where size is the size of queue whose both ends are joint) then shift "front=0" or at last you increase your front with "front=front+1" to delete. 

Implementation of Circular Queue
 Implementation of Circular Queue

Algorithm

For Insertion

  1. Start
  2. If ((rear = = size-1 && front==0) || (rear = = front - 1))
  3.        printf("Circular Queue is Overflow");
  4. Else If (rear = = size -1 && front ! = 0) 
  5.            rear=0;
  6.            CQ[rear]=item;
  7.        Else If (front = = -1)
  8.            front=0; 
  9.            rear=0;
  10.            Q[rear]=item;
  11. End   

For Deletion

  1. Start
  2. If (front = = -1)
  3.        printf("Circular Queue is Underflow");
  4. Else
  5.       item=CQ[front];
  6.        If (front = = rear)
  7.            front = -1;
  8.            rear = -1;
  9.        Else If (front = = size - 1)
  10.            front=0;
  11.        Else
  12.            front = front +1;
  13.     printf("%d is delete");
  14. End

Menu driven program for circular queue in c

#include<stdio.h>
 
#define size 5
 
void cinsert();
void cdelete();
void cdisplay();
int cqueue[size];
int rear = - 1;
int front = - 1;

main()
{
    int choice;
    while (1)
    {
        printf("\n 1.Insert element to CIRCULAR QUEUE \n");
        printf("2.Delete element from CIRCULAR QUEUE \n");
        printf("3.Display all elements of CIRCULAR QUEUE \n");
        printf("4.Quit \n");
        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:cinsert();
                   break;
            case 2:cdelete();
                   break;
            case 3:cdisplay();
                   break;
            case 4://exit(0);
            default:printf("Wrong choice \n");
        } 
    } 
} 
 
void cinsert()
{
   int item;
   printf("Insert the element in queue : ");
                   scanf("%d", &item);
    if ((rear == size - 1 && front == 0) ||(rear == front-1))
    {
    printf("Circular Queue Overflow \n");
    }
 else 
 {
   if (rear== size -1 && front != 0)
             {
       rear = 0;
                cqueue[rear]=item;
             }
             else
    {
    if(front==-1)
    {
     front=0;
    rear = 0;
        cqueue[rear] = item;
    }
  }
 }
} 
 
void cdelete()
{
 int item;
    if (front == - 1)
    {
        printf("Circular Queue Underflow \n");
    }
    else
    { 
       item = cqueue[front];
        if(front==rear)
        { 
          front=-1;
          rear=-1;
  }
  else if(front==size-1)
  {
   front=0;
  }
  else 
  {
   front=front+1;
  }
  printf("Element deleted from queue is : %d\n", cqueue[front+1]);
     }
} 
 
void cdisplay()
{
 int i;        
   if(front == -1)  
      printf("\nCircular Queue is Empty!!!\n");  
   else  
   {  
      i = front;  
      printf("\nCircular Queue Elements are : \n");  
      if(front <= rear)
   {  
     while(i <= rear)  
        printf("%d \n",cqueue[i++]);  
      }  
      else
   {  
     while(i <= size - 1)  
        printf("%d \n", cqueue[i++]);  
     i = 0;  
     while(i <= rear)  
        printf("%d\n",cqueue[i++]);  
      }  
   }     
} 

Output

circular queue

Share this

0 Comment to "Circular Queue in C - its algorithm, how it's works & menu driven program"

Post a Comment