Using C++ Coding Please help me with the following questions. Provide a linked i
ID: 3593693 • Letter: U
Question
Using C++ Coding Please help me with the following questions.
Provide a linked implementation of a deque and name it LinkedDeque (use singly linked list). It can be a template/generic class or you can set it up with a certain data type. Use a test driver to try out your LinkedDeque by adding and removing values from both ends. Set up a vector using extendable array and name it ArrayVector (use an extendable array). It can be a template/generic class or you can set it up with a certain data type. Use a test driver to try out your ArrayVector by performing various operations.
Lab question 1: Would you consider an array implementation for the deque? Explain why or why not.
Lab question 2: Explain how you can go about using your LinkedDeque to implement a queue.
Explanation / Answer
//////////////////////////////////////////////DEQUE BY LINKEDLIST///////////////////////////////////////////
#include<iostream>
#include<stdlib.h>
using namespace std;
class node
{
public:
int data;
class node *next;
class node *prev;
};
class dqueue: public node
{
node *head,*tail;
int top1,top2;
public:
dqueue()
{
top1=0;
top2=0;
head=NULL;
tail=NULL;
}
void push(int x)
{
node *temp;
int ch;
if(top1+top2 >=5)
{
cout <<"dqueue overflow";
return ;
}
if( top1+top2 == 0)
{
head = new node;
head->data=x;
head->next=NULL;
head->prev=NULL;
tail=head;
top1++;
}
else
{
cout <<" Add element 1.FIRST 2.LAST Enter Your Choice:";
cin >> ch;
if(ch==1)
{
top1++;
temp=new node;
temp->data=x;
temp->next=head;
temp->prev=NULL;
head->prev=temp;
head=temp;
}
else
{
top2++;
temp=new node;
temp->data=x;
temp->next=NULL;
temp->prev=tail;
tail->next=temp;
tail=temp;
}
}
}
void pop()
{
int ch;
cout <<"Delete 1.First Node 2.Last Node Enter Your Choice:";
cin >>ch;
if(top1 + top2 <=0)
{
cout <<" Dqueue under flow";
return;
}
if(ch==1)
{
head=head->next;
head->prev=NULL;
top1--;
}
else
{
top2--;
tail=tail->prev;
tail->next=NULL;
}
}
void display()
{
int ch;
node *temp;
cout <<"Display From 1.STARTING 2.ENDING Enter Your Choice";
cin >>ch;
if(top1+top2 <=0)
{
cout <<"under flow";
return ;
}
if (ch==1)
{
temp=head;
while(temp!=NULL)
{
cout << temp->data <<" ";
temp=temp->next;
}
}
else
{
temp=tail;
while( temp!=NULL)
{
cout <<temp->data << " ";
temp=temp->prev;
}
}
}
};
main()
{
dqueue d1;
int ch;
while (1)
{
cout <<"1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter Your Choice:";
cin >>ch;
switch(ch)
{
case 1:
cout <<"Enter the Element: ";
cin >> ch;
d1.push(ch);
break;
case 2:
d1.pop();
break;
case 3:
d1.display();
break;
case 4:
exit(1);
}
}
}
///////////////////////////////////Vector IN C++//////////////////////////////////////
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> g1;
vector <int> :: iterator i;
vector <int> :: reverse_iterator ir;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end : ";
for (i = g1.begin(); i != g1.end(); ++i)
cout << *i << ' ';
cout << endl << endl;
cout << "Output of rbegin and rend : ";
for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << ' ' << *ir;
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.