Program to Implement Input Restricted Dequeue using Array in c++

/* Program to implement input restricted dequeue using array in c++*/
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 5
class queue
{
int q[MAX];
int left,right;
public:
queue()
{
left=-1;
right=-1;
}

void display();
void insert(int x);
int delete_left();
int delete_right();
};
void queue:: display()
{
int l=left,r=right;
if(l<=r)
{
while(l<=r)
{
cout<<q[l]<<“\t”;
l++;
}
}
else
{
while(l<=MAX-1)
{
cout<<q[l]<<“\t”;
l++;
}
l=0;
while(l<=r)
{
cout<<q[l]<<“\t”;
l++;
}
}
}
void queue:: insert(int x)
{
if((left==0 && right==MAX-1)||(left==right+1))
{
cout<<“\n queue full “;
return;
}
else
{
if(left==-1)
{
left=0;
right=0;
}
else if(right==MAX-1)
right=0;
else
right++;
q[right]=x;
}
}
int queue:: delete_left()
{
int ele;
if(left==-1)
{
cout<<“\n queue empty”;
return 0;
}
ele=q[left];
if(left==right)
{
left=-1;
right=-1;
}
else if(left==MAX-1)
{
left=0;
}
else
left++;

return ele;
}
int queue:: delete_right()
{
int ele;
ele=q[right];
right–;
return ele;
}
void main()
{
int ch;
queue obj;
int ele;
clrscr();
do
{
cout<<“\n1.insert \n2.Delete from left \n3.Dlete from right \n4.Display \n5.Exit \n”;
cout<<“\n enter Choice “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n enter Data “;
cin>>ele;
obj.insert(ele);
break;
case 2:
ele=obj.delete_left();
cout<<“\n ele= “<<ele;
break;
case 3:
ele=obj.delete_right();
cout<<“\n ele= “<<ele;
break;
case 4:
obj.display();
break;

}
}while(ch!=5);
}

Leave a Reply