Code for Circular Queue


#include<stdio.h>
#include<conio.h>

void qinsert(int q[],int n,int *f,int *r,int x)
{
 if(*r>=(n-1) && *f==0 || *f==*r+1 )
 {
printf("queue is overflow");
  return;
 }
 else
 {
if(*r==n-1 && *f>0)
  {
   *r=0;
   q[*r]=x;
  }
  else
  {
   *r=*r+1;
   q[*r]=x;
  }
 if(*f==-1)
  *f=0;
 }
}
int qdelete(int q[],int n,int *f,int *r)
{
int a;
 if(*f==-1)
  return 0;
 else
 {
  if(*f==*r)
  {
   *f=-1;
   *r=-1;
  }
  else
  {
if(*f==n-1 && *r<*f)
   {
    a=q[*f];
    *f=0;
   }
   else
   {
    a=q[*f];
    *f=*f+1;
   }
  }
   return(a);
 }
}

void display(int q[],int n,int *f,int *r)
{
int i;
 if(*f==-1)
printf("queue is empty");
 else
 {
printf("\nqueue contains:\n");
  if(*f<=*r)
  {
   for(i=*f;i<=*r;i++)
   {
printf("%d\t",q[i]);
   }
  }
  else
  {
   for(i=0;i<=*r;i++)
   {
printf("%d\t",q[i]);
   }
   for(i=*f;i<=n-1;i++)
   {
printf("%d\t",q[i]);
   }
  }
 }
}

void main()
{
int n=5,*f,*r,i,j,k;
int q[5];
 *f=-1;
 *r=-1;
clrscr();
 again:
printf("\n1.insert\n2.delete\n3.display\n4.exit\n");
printf("enter your choice\n");
scanf("%d",&k);
 switch(k)
 {
  case 1:
printf("enter the value to be inserted\n");
scanf("%d",&j);
qinsert(q,n,f,r,j);
  display(q,n,f,r);
goto again;

  case 2:
  j=qdelete(q,n,f,r);
  if(j==0)
printf("queue is underflow");
  else
  {
   display(q,n,f,r);
  }
goto again;

  case 3:
  display(q,n,f,r);
goto again;

case 4:
 goto exit;
 }
exit:
}

Comments

Popular posts from this blog

Code for BUBBLE Sort

Code for Circular Linked List c-programing

Code for SELECTION Sort in C-Programming