Circular D - Queue Using Arrays

Implementation of Circular D-Queue (Double-Ended-Queue) in C++ Using Classes and Arrays :
-----------------------------------------------------
Assignment Question ☺ ☺


See Also:  Linear Double Ended Queue


#include<iostream.h>
#include<conio.h>
class d_queue{
private:
int rfront,rrear,lfront,lrear,max;
int *queue;
public:
d_queue(){ clrscr();
    lfront=lrear=-1;
   cout<<"\n\n\tEnter the size of Queue: ";
   cin>>max;
   for(int z=0;z<=max;z++){queue[z]=0;}
   rfront=max;rrear=max+1;
   }

void insert_left(int n)
{
   if(lfront==-1 && lrear==-1)
   { lfront=lrear=0;
     queue[lrear]=n;
   }
   else if( queue[lrear+1]==0  &&  lrear+1!=rrear)
   {  lrear=lrear+1;
      queue[lrear]=n;
   }
   else if(lrear+1==lfront)
   {cout<<"\n\n\n\t\t\a Error:\a\a Left side is Full \n";}
    else if(queue[lrear+1]!=0 && lfront==0)
   {   cout<<"\n\n\n\t\t\a\a\aError:  Left side is Full \n";  }
   else if( lfront!=0  && queue[lrear+1]!=0 || lrear+1==rrear)
   {   lrear=0;
       queue[lrear]=n;
    }

    else
    {   lrear=lrear+1;
        queue[lrear]=n;
    }

}
void insert_right(int n)
{

 if( queue[rrear-1]==0 && rrear-1!=lrear)
 {  rrear=rrear-1;
     queue[rrear]=n;
 }
 else if(rrear==rfront+1)
 { cout<<"\n\n\n\t\t\a\aError:  Right side is Full \n";}
 else if( queue[rrear-1]!=0/* || rrear-1==lrear*/ && rfront==max)
 {  cout<<"\n\n\n\t\t\a\a\aError: 1 Right side is Full \n ";  }

 else if(  queue[rrear-1]!=0 ||  rrear-1==lrear && rfront!=max)
 {  rrear=max;     cout<<"1";
    queue[rrear]=n;
 }

  else
  {
  rrear=rrear-1;
   queue[rrear]=n;
  }
  }
void delete_left()
{
  if(lfront==-1 && lrear==-1 && rrear==max)
  {  cout<<"\n\n\t\a\a Queue is Empty";  }
  else if(lrear==lfront)
  {queue[lfront]=0;
  lfront=lrear=-1;    }
  else if(lfront==rrear)
  {  lfront=0;       }
  else{
       cout<<"\n\t Item Deleted: "<<queue[lfront];
       queue[lfront]=0;
       lfront=lfront+1;
       }
}
void delete_right()
{
  if(rrear==max+1 && rfront==max+1 && lrear==-1)
  {  cout<<"\n\n\t\a Queue is Empty";   }
  else if(rrear==rfront)
  { queue[rfront]=0;
  rrear=rfront=max+1;  }
  else if( rfront==lrear)
  { rfront=max;   }
  else{
       cout<<"\n\t Item Deleted: "<<queue[rfront+1];
       queue[rfront]=0;
       rfront=rfront-1;
 }
}
void display()
{   {if(lrear+1==rrear && lfront==0 && rfront==max){gotoxy(5,8);cout<<"WARNING: \a\a\a  Dont try to insert any other value without deletion";}}

    for(int j=1;j<=max+1;j++){
 gotoxy(j*5,10);
 cout<<"   "<<j-1;
 gotoxy(j*5,11);
 cout<<"|````|";
 gotoxy(j*5,12);
 cout<<"|    |";
 gotoxy(j*5,13);
 cout<<"``````";
   }
for(int p=0,z=5;p<=max;p++,z=z+5){
  gotoxy(z+1,12);
  if(queue[p]==0){cout<<" ";}else{ cout<<queue[p];}
  if(p==rrear){gotoxy(z+1,14);cout<<"RR";}
  if(p==rfront){gotoxy(z+1,14);cout<<"RF";}
  if(p==lrear){gotoxy(z+1,14);cout<<"LR";}
  if(p==lfront){gotoxy(z+1,14);cout<<"LF";}
  }//for
  gotoxy(5,15);
  cout<<"\n\nRight Rear:  "<<rrear<<"\t\tLeft Rear:   "<<lrear;
  cout<<"\n\nRight Front: "<<rfront<<"\t\tLeft Front:  "<<lfront;

}  ~d_queue(){}
};
void main()
{
int a,value;
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\n\n\t Any Key=Continue  || Escap = Exit ";
 }   //while
}





|`````|`````|````|````|````|`````|`````|``````|`````|
|      |      |     |     |     |       |      |       |       |
`````````````````````````````````````````````````
  ^                 ^    ^                           ^
LF              LR  RR                        RF

No comments

Powered by Blogger.