Question: In this task, we are given a complete implementation of a doubly linked list, and we are asked to implement 2 data structures, namely Queue,
In this task, we are given a complete implementation of a doubly linked list, and we are asked to implement data structures, namely Queue, and Stack.
A queue is a FIFO firstinfirstout container, with operations enqueue, and dequeue for putting in data at the front of the queue, and taking out data from the back of the queue, respectively.
A stack is a LIFO lastinfirstout container, with operations push and pop, for putting in data at the fronttop of the stack, and taking out data from the fronttop respectively.
Both data structures have a peek method, to see the value at the fronttop but not take it out, as well as the usual size and isEmpty methods.#ifndef LINKEDLISTH
#define LINKEDLISTH
#include
#include
#include
template
class LinkedList;
template
struct Link
T data;
Link next;
Link prev;
Link
data ;
next nullptr;
prev nullptr;
LinkT data
thisdata data;
next nullptr;
prev nullptr;
;
template
std::ostream& operatorstd::ostream& os const LinkedList& list
Link curr list.front;
while curr nullptr
os currdata;
if currnext nullptr os ;
curr currnext;
return os;
template
class LinkedList
protected:
Link front;
Link back;
int count;
public:
LinkedList
front nullptr;
back nullptr;
count ;
void appendT value
if front nullptr
Appending to an empty list
front new Linkvalue;
back front;
else
Appending to list with elements
Link temp new Linkvalue;
tempprev back;
backnext temp;
back temp;
count;
void prependT value
if front nullptr
Prepending to an empty list
front new Linkvalue;
back front;
else
Prepending to a list with elements
Link temp new Linkvalue;
tempnext front;
frontprev temp;
front temp;
count;
T removeFirst
if front nullptr
throw std::logicerrorCan not remove from an empty list";
if count
T x frontdata;
delete front;
front nullptr;
back nullptr;
count;
return x;
else
T x frontdata;
Link old front;
front frontnext;
frontprev nullptr;
delete old;
count;
return x;
T peek const
if front nullptr
throw std::logicerrorCan not peek into an empty list";
return frontdata;
T removeLast
Your code goes here
if front nullptr
throw std::logicerrorCan not remove from an empty list";
if front back
T x frontdata;
delete front;
front nullptr;
back nullptr;
count;
return x;
else
T x backdata;
Link old back;
back backprev;
backnext nullptr;
delete old;
count;
return x;
int size const
return count;
bool isEmpty const
return count ;
void reverse
Link oldFront front;
front back;
back oldFront;
Link curr front;
whilecurr nullptr
Link temp currnext;
currnext currprev;
currprev temp;
curr currnext;
friend std::ostream& operatorstd::ostream& os const LinkedList& list;
friend struct TestLinkedList;
;
#endif#ifndef QUEUEH
#define QUEUEH
#include "LinkedList.h
template
class Queue;
template
std::ostream& operatorstd::ostream& os const Queue& q;
template
class Queue
Your code here
;
template
std::ostream& operatorstd::ostream& os const Queue& q
Your code here
#endif#ifndef STACKH
#define STACKH
#include "LinkedList.h
template
class Stack;
template
std::ostream& operatorstd::ostream& os const Stack& q;
template
class Stack
Your code here
;
template
std::ostream& operatorstd::ostream& os const Stack& q
Your code here
#endif I don't know what i need to do for this.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
