Thursday 10 October 2013

PROGRAM OF DOUBLE ENDED QUEUE IN C++

               DOUBLE ENDED QUEUE              

#include<iostream.h>
#include<conio.h>
#include<process.h>
int arr[10];
int max=10,left=-1,right=-1;
void inr()
{
int data;
cout<<"Enter the data you want to insert : ";
cin>>data;
if(left==0 && right==max-1)
{
cout<<"\noverload";
exit(0);
}
else if(left==-1)
{
left=0;
right=0;
}
else if(right==max-1)
right=0;
else
right=right+1;
arr[right]=data;
}
void inl()
{
int data;
cout<<"Enter the data you want to insert : ";
cin>>data;
if(left==0 && right==max-1)
{
cout<<"overflow";
exit(0);
}
if(left==-1)
{
left=0;
right=0;
}
else if(left==0)
left=max-1;
else
left=left-1;
arr[left]=data;
}
void view()
{
if(left!=-1)
{
cout<<"Queue is :";
for(int i=0;i<max;i++)
{
if(arr[i])
cout<<arr[i]<<endl;
}
}
}
void der()
{
if(left==-1)
{
cout<<"underflow";
exit(0);
}
arr[right]=0;
if(left==right)
{
left=-1;
right=-1;
}
else if(right==0)
right=max-1;
else
right=right+1;
}
void del()
{
if(left==-1)
{
cout<<"Underflow";
exit(0);
}
arr[left]=0;
if(left==right)
{
left=-1;
right=-1;
}
else if(left==max-1)
left=0;
else
left=left+1;
}
void main()
{
clrscr();
char ans;
int ch;
cout<<"MENU";
cout<<"\n1).Insert in right";
cout<<"\n2).Insert in left";
cout<<"\n3).Delete from right";
cout<<"\n4).Delete from left";
do
{
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
      inr();
      view();
      break;
case 2:
       inl();
       view();
       break;
case 3:
       der();
       view();
       break;
case 4:
       del();
       view();
       break;
default: cout<<"\n you made a wrong choice";
}
cout<<"Do you want more y for yes : ";
cin>>ans;
}while(ans=='y');
getch();
}



 

No comments:

Post a Comment