Double-ended Queue in C++ Using Array

Implementation of Linear Double Ended Queue in C++ Using Arrays and Classes
Assignment Question ☺☺ 

See Also: Circular Double Ended Queue (circular D-Queue)
#include<iostream.h>
#include<conio.h>
#define Max 5
class D_Queue{
private:
int front,rfront,rrear,rear,max;
int d_queueary[Max];
public:
D_Queue()
{
clrscr();
front=-1,rfront=0;rear=-1,rrear=0;
//cout<<"\nEnter the size of D-Queue: ";
//cin>>max;
//d_queueary[max];
}
void insert_left(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
rear=rear+1;
d_queueary[rear]=n;
}
void delete_left()
{
if(rear==-1)
cout<<"\n Queue is Empty\n";
else
cout<<"\nDeleted Value = "<<d_queueary[rear];
rear=rear-1;
}
void insert_right(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
{
for(rrear=rear;rrear>=0;rrear--)
   {
   d_queueary[rrear+1]=d_queueary[rrear];
   }
d_queueary[0]=n;
rear=rear+1;
}
}
void delete_right()
{
if(rear==-1)
cout<<"\n Queue is Empty \n ";
else
{       cout<<"\nDeleted Value = "<<d_queueary[0];
for(rfront=0;rfront<=rear;rfront++)
 {
 d_queueary[rfront]=d_queueary[rfront+1];
 }
rear=rear-1;
}
}
void display()
{
cout<<"\npcphunt.blogspot.com\n";
for(int i=0;i<=rear;i++)
{
 cout<<"\nValue is = "<<d_queueary[i];
}
}
};
void main()
{
int value,a;
D_Queue obj;
while(getch()!=27)
{
cout<<"\n\n\t\t\tSelect  An  Option\n\n\n\t|`````````````````````````````|\n\n\t| 1  `   Insertion at Right   |\n\n\t| 2  `   Insertion at Left    |\n\n\t| 3  `   Deleletion at Right  |\n\n\t| 4  `   Deletion at Left     |\n\n\t| 5  `   Display              |\n\n\t```````````````````````````````";
cin>>a;
 clrscr();
switch(a)
{
case 1: {
cout<<"\nRight Insertion`` Enter the Value : ";
cin>>value;
obj.insert_right(value);
break;
}
case 2: {
cout<<"\nLeft Insertion`` Enter the value : ";
cin>>value;
obj.insert_left(value);
break;
}
case 3: {
obj.delete_right();        break;
}
case 4: {
obj.delete_left();         break;
}
case 5:{
obj.display();             break;
}
}   //switch
cout<<"\n Any Key=Continue  || Escap = Exit ";
}   //while

}

Copyright◘2014@pcphunt.blogspot.com






Here its the same code as in above but in this code i put some efforts to show how this circular queue works by shewing boxes and values in them . For batter understanding you must run this code. ☺


#include<iostream.h>
#include<conio.h>
//#define Max 5
class D_Queue{
private:
int front,rfront,rrear,rear,max;
int *d_queueary;
public:
D_Queue()
{
clrscr();
front=-1,rfront=0;rear=-1,rrear=0;
cout<<"\nEnter the size of D-Queue: ";
cin>>max;
//d_queueary[max];
}
void insert_right(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
rear=rear+1;
d_queueary[rear]=n;
}
void delete_right()
{
if(rear==-1)
cout<<"\n Queue is Empty\n";
else
cout<<"\nDeleted Value = "<<d_queueary[rear];
rear=rear-1;
}
void insert_left(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
{
for(rrear=rear;rrear>=0;rrear--)
   {
   d_queueary[rrear+1]=d_queueary[rrear];
   }
d_queueary[0]=n;
rear=rear+1;
}
}
void delete_left()
{
if(rear==-1)
cout<<"\n Queue is Empty \n ";
else
{       cout<<"\nDeleted Value = "<<d_queueary[0];
for(rfront=0;rfront<=rear;rfront++)
 {
 d_queueary[rfront]=d_queueary[rfront+1];
 }
rear=rear-1;
}
}
void display()
{       for(int j=1;j<=max;j++){
gotoxy(j*5,11);
cout<<"|````|";
gotoxy(j*5,12);
cout<<"|    |";
gotoxy(j*5,13);
cout<<"``````";
 }
for(int i=0,z=5;i<=rear;i++)
{ gotoxy(z+1,12);
 z=z+5;
 cout<<d_queueary[i];
}
}
};
void main()
{
int value,a;
D_Queue obj;
while(getch()!=27)
{
cout<<"\n\n\t\t\tSelect  An  Option\n\n\n\t|`````````````````````````````|\n\n\t| 1  `   Insertion at Right   |\n\n\t| 2  `   Insertion at Left    |\n\n\t| 3  `   Deleletion at Right  |\n\n\t| 4  `   Deletion at Left     |\n\n\t| 5  `   Display              |\n\n\t```````````````````````````````";
cin>>a;
 clrscr();
switch(a)
{
case 1: {
cout<<"\nRight Insertion`` Enter the Value : ";
cin>>value;
obj.insert_right(value); obj.display();
break;
}
case 2: {
cout<<"\nLeft Insertion`` Enter the value : ";
cin>>value;
obj.insert_left(value);
obj.display();
break;
}
case 3: {
obj.delete_right();  obj.display();      break;
}
case 4: {
obj.delete_left();    obj.display();     break;
}
case 5:{
obj.display();             break;
}
}   //switch
cout<<"\n\n Any Key=Continue  || Escap = Exit ";
}   //while

}



2 comments

My Instagram