A double-ended queue(deque) is a linear list in which additions and deletions may be made
at either end. Obtain a data representation mapping a deque into a one-dimensional array.
Write C++ program to simulate deque with functions to add and delete elements from either
end of the deque.
#include<iostream> //Header Files
using namespace std;
char que[5]; //.......Double Ended Queue
int size = 5; //.......Size of Queue
int front = -1; //.......Front End
int rear = -1; //.......Rear End
int que_full() //.......To check Queue is Full
{
if (rear == size-1)
return 1;
else
return 0;
}
int que_empty() //.......To check Queue is Empty
{
if(front == rear == -1 || front == rear)
return 1;
else
return 0;
}
void insert_AtRear(char job) //.......To Insert at Rear in DEQueue
{
rear++;
que[rear] = job;
}
void insert_AtFront(char job) //.......To Insert at Front in DEQueue
{
que[front] = job;
front--;
}
char del_AtFront() //.......To Delete From Front From DEQueue
{
char ch;
front++;
ch = que[front];
return ch;
}
char del_AtRear() //.......To Delete From Rear From DEQueue
{
char ch;
ch = que[rear];
rear--;
return ch;
}
void display() //.......To Display Queue.
{
cout<<"\n\n Front---> ";
for(int i = front+1;i <= rear;i++)
cout<<" "<<que[i];
cout<<" <---Rear";
}
int main()
{
int choice;
char job,ans;
do
{
cout<<"\n\n ** Menu **";
cout<<"\n\n 1. Insert at Rear in DEQueue";
cout<<"\n\n 2. Delete From Front in DEQueue";
cout<<"\n\n 3. Insert at Front in DEQueue";
cout<<"\n\n 4. Delete From Rear in DEQueue";
cout<<"\n\n 5. Display DEQueue";
cout<<"\n\n Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n\n Enter the Job: ";
cin>>job;
insert_AtRear(job);
break;
case 2: cout<<"\n\n Deleted Job: "<<del_AtFront();
break;
case 3: cout<<"\n\n Enter the Job: ";
cin>>job;
insert_AtFront(job);
break;
case 4: cout<<"\n\n Deleted Job: "<<del_AtRear();
break;
case 5: display();
break;
default: cout<<"\n\n Invalid choice.....!!!";
}
cout<<"\n\n Do u wanna continue ?? (y/n) : ";
cin>>ans;
}while(ans == 'y');
cout<<"\n\n";
return 0;
}
at either end. Obtain a data representation mapping a deque into a one-dimensional array.
Write C++ program to simulate deque with functions to add and delete elements from either
end of the deque.
OUTPUT FOR THE PROGRAM |
using namespace std;
char que[5]; //.......Double Ended Queue
int size = 5; //.......Size of Queue
int front = -1; //.......Front End
int rear = -1; //.......Rear End
int que_full() //.......To check Queue is Full
{
if (rear == size-1)
return 1;
else
return 0;
}
int que_empty() //.......To check Queue is Empty
{
if(front == rear == -1 || front == rear)
return 1;
else
return 0;
}
void insert_AtRear(char job) //.......To Insert at Rear in DEQueue
{
rear++;
que[rear] = job;
}
void insert_AtFront(char job) //.......To Insert at Front in DEQueue
{
que[front] = job;
front--;
}
char del_AtFront() //.......To Delete From Front From DEQueue
{
char ch;
front++;
ch = que[front];
return ch;
}
char del_AtRear() //.......To Delete From Rear From DEQueue
{
char ch;
ch = que[rear];
rear--;
return ch;
}
void display() //.......To Display Queue.
{
cout<<"\n\n Front---> ";
for(int i = front+1;i <= rear;i++)
cout<<" "<<que[i];
cout<<" <---Rear";
}
int main()
{
int choice;
char job,ans;
do
{
cout<<"\n\n ** Menu **";
cout<<"\n\n 1. Insert at Rear in DEQueue";
cout<<"\n\n 2. Delete From Front in DEQueue";
cout<<"\n\n 3. Insert at Front in DEQueue";
cout<<"\n\n 4. Delete From Rear in DEQueue";
cout<<"\n\n 5. Display DEQueue";
cout<<"\n\n Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n\n Enter the Job: ";
cin>>job;
insert_AtRear(job);
break;
case 2: cout<<"\n\n Deleted Job: "<<del_AtFront();
break;
case 3: cout<<"\n\n Enter the Job: ";
cin>>job;
insert_AtFront(job);
break;
case 4: cout<<"\n\n Deleted Job: "<<del_AtRear();
break;
case 5: display();
break;
default: cout<<"\n\n Invalid choice.....!!!";
}
cout<<"\n\n Do u wanna continue ?? (y/n) : ";
cin>>ans;
}while(ans == 'y');
cout<<"\n\n";
return 0;
}
For more such posts click the link:-http://svencrai.com/G8W
Tags
CODING