F Example Of Circular Queue Program | CodeTheta

Example Of Circular Queue Program

April 16, 2015


/* http://native-code.blogspot.com */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
void cqinsert(int cque[MAX],int *front,int *rear,int *count)
{
 int item;
 if((*front==0 && *rear==MAX-1)||(*front==(*rear)+1))
 {
  printf("\nQueue is full! Overflow");
  getch();
 }
 else
 {
  printf("\nEnter an item %d to be inserted:",*count+1);
  scanf("%d",&item);
  if(*front==-1)
  {
   *front=0;
   *rear=0;
  }
  else if(*rear==MAX-1)
   *rear=0;
       else
   (*rear)++;
  cque[*rear]=item;
  (count)++;
 }
}
void cqdelete(int cque[MAX],int *front,int *rear,int *count)
{
 int item;
 if(*front==-1)
 {
  printf("\nQueue is empty ! Underflow");
  getch();
 }
 else
 { item=cque[*front];
  printf("\nDeleted item is %d", item);
  getch();
  if(*front==*rear)//queue has only one element
  {
   *front=-1;
   *rear=-1;
  }
  else if(*front==MAX-1)
   *front=0;
        else
   (*front)++;
       (*count)++;
 }
}
void cqdisplay(int cque[MAX],int *front,int *rear,int *count)
{
 int i,c;
 for(c=*count,i=(*front);c!=0;c--)
 {
  if(i==MAX)
  {
   i=0;
   printf("%d->",cque[i++]);
  }
 }
 getch();
}
void main()
{
int cque[MAX],front=-1,rear=-1,count=0;
int choice;
clrscr();
while(1)
{
        printf("\n/* http://native-code.blogspot.com */");
 printf("\nCircular Queue Operations: ");
 printf("\n1.Insert into Circular Queue");
 printf("\n2.Delete from Circular Queue");
 printf("\n3.Display from Circular Queue");
 printf("\n4.Exit");
 printf("\nEnter as your choice");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1: printf("Insert operation\n");
   cqinsert(cque,&front,&rear,&count);
   break;
  case 2: printf("Deletion operation");
   cqdelete(cque,&front,&rear,&count);
   break;
  case 3: printf("Queue elements");
   cqdisplay(cque,&front,&rear,&count);
   break;
  case 4: exit(0);
   }
}
}
/* http://native-code.blogspot.com */

Post a Comment