Circular Queue Code

//Circular Queue

#include<stdio.h>
#include<process.h>
int front=0,rear=-1,q[5];
int main()
{
 void insertion(int);int deletion();void display();
 int x,data,ch;
 printf("\n\n***MENU For Circular Queue***\n\n1.Insertion\n\n2.Deletion\n\n3.Display\n\n4.Exit\n\n");
 do
 {
  printf("\n\nEnter your choice\n\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("\n\nEnter data to insert in Queue\n\n");
                scanf("%d",&data);
    insertion(data);
    break;
    case 2:
     x=deletion();
     printf("\n\nDeleted data is %d\n\n",x);
     break;
    /*here if you remove break; statememt from program then after deletion
    it will move to case 3 and call dispaly function*/
     case 3:
      display();
      break;
      case 4:
       exit(1);
       default:
        printf("\n\nWrong Choice Try again\n\n");
       
  }
 }while(ch!=4);
 
}
void insertion(int x)
{
 /* case 1- when rear is at end and front is at zero means 
 CQ is Full*/
 if(rear==4&&front==0)
 {
 printf("\nCircular Queue is Overflow\n\n");
    }
    /* Case 2- its intial condition when CQ is empty 
    this is intial insertioin*/
 else if(rear==-1&&front==0)
 {
  rear++;
  q[rear]=x;
 }
 /* Case 3- its very important condition normally in Q and DQ it is Condition 
 of Underflow But here its a condition for Overflow Because in CQ Fifo
 is followed and this condition is only true when CQ is full*/
 else if(rear==front-1)
 {
  printf("\nCircular Queue is Full\n\n");
 }
 /* Case 4- here rear is at end and front is greater than zero means 
 behind front there is place empty for insertion and we know insertion is from 
 rear so we have to make rear zero then insert*/
 else if(rear==4&&front>0)
 {
  rear=0;
  q[rear]=x;
 }
 /* Case 5- its a Normal condition Other than above 4 for inserion*/
 else
 {
  rear++;
  q[rear]=x;
 }
 
}

int deletion()
{
 int x;
 if(rear<0)
 printf("\nCircular Queue is Empty\n\n");
 else
 {
  /* if you are in else part then you atleast one element in the 
  queue and we have to return that value*/
  x=q[front];
  /* here if you are left with single element in the queue so 
  make front and zero at intial postion and return value*/
  if (front==rear)
  {
   front=0;rear=-1;
  }
  /* if front has reached at last deleting the element and rear is coming from the back
  entering rest of the element then we have make front zero*/
  else if(front==4)
  {
   front=0;
  }
  /* after returning value normally we have to move to next value */
  else
  {
   front++;
  }
  return(x);
 } 
}

void display()
{
 int i;
 if(rear==-1&&front==0)
 {
 printf("\nCircular Queue is empty\n\n");
    }
 /* lets rear is greater than and equal to front so there might be only one element inserted 
 in that case both rear and front will be at zero OR both rear and front be at last element of CQ*/
 else if(rear>=front)
 {
  for(i=front;i<=rear;i++)
  {
  printf("%d ",q[i]);
     }
 }
 else
 {
  /* if you are in else it means you more than one element and we will print the CQ from
  front to rear*/
  for(i=front;i<5;i++)
  {
  printf("%d ",q[i]);
     }
  for(i=0;i<=rear;i++)
  {
  printf("%d ",q[i]);
  }
 }
}

No comments:

Post a Comment