Friday, June 3, 2016

WAP to implement Double Ended Queue

 #include<stdio.h>
#include<conio.h>
int dq[5],r=-1,f=-1,r2=5,f2=5,max=5;
void insertr(int x)
{
if(r==max-1)
{
printf("Queue is full");
return;
}
r++;
dq[r]=x;
if(f==-1)
f=0;
r2=f;
f2=r;
}
int deletel()
{
int x;
if(f==-1)
{
printf("Queue is empty");
}
else
{
x=dq[f];
if(f==r)
{
f=r=-1;
f2=r2=max;
}
else
{
f++;
f2=r;
r2=f;
}
return x;
}
}
void insertl(int x)
{
if(r2==0)
{
printf("Queue is full");
return;
}
r2--;
dq[r2]=x;
if(f2==max)
f2=max-1;
r=f2;
f=r2;
}
int deleter()
{
int x;
if(f2==max)
{
printf("Queue is empty");
}
else
{
x=dq[f2];
if(f2==r2)
{
f=r=-1;
f2=r2=max;
}
else
{
f2--;
f=r2;
r=f2;
}
return x;
}
}
void display()
{
int i;
if(f==-1)
{
printf("\nQueue is empty");
return;
}
printf("\n Values in queue are:\n");
for(i=f;i<=r;i++)
{
printf("|%d| ",dq[i]);
}
}
void main()
{
int n,x;
clrscr();
do
{
printf("\n1.Insert form Right ");
printf("\n2.Insert form Left ");
printf("\n3.Delete from left ");
printf("\n4.Delete form Right ");
printf("\n5.display ");
printf("\n6.exit");
printf("\nEnter your choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nEnter value :");
scanf("%d",&x);
insertr(x);
break;
case 2:
printf("\nEnter value :");
scanf("%d",&x);
insertl(x);
break;
case 3:
x=deletel();
printf("\n Deleted value is %d",x);
break;
case 4:
x=deleter();
printf("\n Deleted value is %d",x);
break;
case 5:
display();
break;
case 6:
printf("\nThank you");
break;
default:
printf("\nInvalid choice");
}

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

No comments:

Post a Comment