Pages

Monday, 8 November 2021

Program to illustrate Dequeue using Arrays in c++

 Input & Output Restricted Double Ended Queue ( DEQUE ) :

        It is also called as double ended queue, in which insertion and deletion operations are performed from both the ends i.e we can insert an element from rear end or from the front end , we can delete an element from rear end or from the front end. Hence it is commonly referred to as DEQUE,there are two types of Deque.
1.Input Restricted Deque
2.Output Restricted Deque

The following are the operation performed in Input Restricted Deque
 1.Insert at rear
 2.Delete from front
 3.Delete from rear

The following are the operation performed in Output Restricted Deque
 1.Insert at rear
 2.Insert at front
 3.Delete from front


 


/* Program on input restricted dequeue using array*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 5
int dqi[MAX],i,front = -1,rear = -1,item;
class rkdequeuei
{
public:
int insert_rear();
int del_front();
int del_rear();
int display();
};
int rkdequeuei::insert_rear()
{
    if((front == 0 && rear == MAX-1) || (front==rear+1)||(rear==MAX-1))
{
 cout<<"\n DEQueue Overflow\n";
 return rear;
}
   if(front == -1)  /* if queue is initially empty */
{
 front = 0;
}
rear = rear+1;
cout<<"Input the element for adding in dequeue : ";
cin>>item;
dqi[rear] = item ;
return rear;
}/*End of insert_rear()*/

int rkdequeuei::del_front()
{
if (front == -1)
{
cout<<"DEQueue Underflow\n";
return front;
}
       cout<<"Element deleted from dequeue is : "<<dqi[front];
dqi[front]='\0';
if(front == rear) /*Queue has only one element */
    {
     front = -1;
     rear = -1;
    }
else
  front = front+1;
       return front;
}/*End of del_front()*/
int rkdequeuei::del_rear()
{
if (front == -1)
{
 cout<<"DEQueue Underflow\n";
 return front;
}
cout<<"\n Element deleted from dequeue is :"<<dqi[rear];
dqi[rear]='\0';
if(front == rear) /*queue has only one element*/
{
 front = -1;
}
rear=rear-1;
return rear;
}/*End of del_rear() */
int rkdequeuei:: display()
{
 if((front==-1 && rear==-1) || (front==-1))
 {
  cout<<"\n dequeue is empty \n";
  return rear;
 }
 else
 cout<<"\n Dequeue elements are:";
 for(i=0;i<=MAX-1;i++)
 {
  if(dqi[i]=='\0')
  {
  cout<<"\t";
  }
  else
  cout<<dqi[i]<<"  ";
 }
 cout<<"\n front ="<<front<<" and rear = "<<rear;
 return rear;
}/*End of display() */
void main()
{
 int choice;
 rkdequeuei idq;
 clrscr();
while(1)
{
cout<<"\n1.Insert at rear\n";
cout<<"2.Delete from front\n";
cout<<"3.Delete from rear\n";
cout<<"4.Display\n";
cout<<"5.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
 case 1:
idq.insert_rear();
break;
 case 2:
idq.del_front();
break;
 case 3:
idq.del_rear();
break;
 case 4:
idq.display();
break;
 case 5:
exit(1);
 default:
cout<<"Wrong choice\n";
}/*End of switch*/
}/*End of while*/
}/*End of main() */

Output:


/* Program on output restricted dequeue using array*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 5
int dqo[MAX],i,item,front = -1,rear = -1;

class rkdequeueo
{
public:
int insert_front();
int insert_rear();
int del_front();
int display();
};

int rkdequeueo::insert_rear()
{

if(front == 0 && rear == MAX-1)
{
   cout<<"DEQueue Overflow\n";
   return rear;
}
if(rear == MAX-1 )
{
cout<<"\n Insertion is not possible\n";
return rear;
}
if (front == -1)  /* if queue is initially empty */
{
   front = 0;
}
rear = rear+1;
cout<<"Input the element for adding in queue : ";
cin>>item;
dqo[rear] = item ;
return rear;
}/*End of insert_rear()*/

int rkdequeueo::insert_front()
{
if((front==0 && rear == MAX-1) || (front == rear+1))
{
cout<<"DEQueue Overflow \n";
return front;
}
if(front == 0 )
{
cout<<"\n Insertion is not possible\n";
return front;
}
if (front == -1)/*If queue is initially empty*/
  {
   front=0;
   rear = 0;
  }
else
front=front-1;
cout<<"Input the element for adding in queue : ";
cin>>item;
dqo[front] = item ;
return front;
}/*End of insert_front()*/

int rkdequeueo::del_front()
{
if (front == -1)
{
cout<<"DEQueue Underflow\n";
return front;
}
cout<<"Element deleted from queue is : "<<dqo[front];
dqo[front]='\0';
if(front == rear) /*Queue has only one element */
{
front = -1;
rear=-1;
}
else
if(front == MAX-1)
front = 0;
else
front = front+1;
return front;
}/*End of delete_left()*/

int rkdequeueo::display()
{
 if(front==-1 && rear==-1)
 {
  cout<<"\nDequeue is empty";
  return rear;
 }
 cout<<"\nDequeue elements are:";
 for(i=0;i<=MAX-1;i++)
 {
  if(dqo[i]=='\0')
  {
  cout<<"\t";
  }
  else
  cout<<dqo[i]<<"  ";
 }
 cout<<"\n front = "<<front <<" and rear ="<<rear;
 return rear;
}

void main()
{
int choice;
rkdequeueo odq;
clrscr();
while(1)
{
cout<<"\n MENU  \n";
cout<<"1.Insert at rear\n";
cout<<"2.Insert at front\n";
cout<<"3.Delete from front\n";
cout<<"4.Display\n";
cout<<"5.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
 case 1:
odq.insert_rear();
break;
 case 2:
odq.insert_front();
break;
 case 3:
odq.del_front();
break;
 case 4:
odq.display();
break;
 case 5:
exit(1);
 default:
cout<<"Wrong choice\n";
}/*End of switch*/
}/*End of while*/
}/*End of main() */

Output:

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...