Thursday, June 2, 2016

WAP to implement Circular Queue

#include<stdio.h>
#include<conio.h>
int cq[5],r=-1,f=-1,max=4;
void insert(int x)
{
if((r==max && f==0) || f==r+1)
{
printf("Queue is full");
return;
}
if(r==max)
r=0;
else
r++;
cq[r]=x;
if(f==-1)
f=0;
}
int del()
{
int x;
if(f==-1)
{
printf("Queue is empty");
}
else
{
x=cq[f];
if(f==r)
{
f=-1;
r=-1;
}
else
{
if(f==max)
f=0;
else
f++;
}
return x;
}
}
void display()
{
int i;
if(f==-1)
{
printf("Queue is empty");
return;
}
printf("\n Values in queue are:\n");
if(f<=r)
{
for(i=f;i<=r;i++)
{
printf("|%d| ",cq[i]);
}
}
else
{
for(i=f;i<=max;i++)
{
printf("|%d| ",cq[i]);
}
for(i=0;i<=r;i++)
{
printf("|%d| ",cq[i]);
}
}
}
void main()
{
int n,x;
clrscr();
do
{
printf("\n1.Insert ");
printf("\n2.delete ");
printf("\n3.display ");
printf("\n4.exit");
printf("\nEnter your choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nEnter value :");
scanf("%d",&x);
insert(x);
break;
case 2:
x=del();
printf("\n Deleted value is %d",x);
break;
case 3:
display();
break;
case 4:
printf("\nThank you");
break;
default:
printf("\nInvalid choice");
}

}while(n!=4);
getch();
}

No comments:

Post a Comment