Question: Writing a wrapper Given the code attached here, you need to write a class that implements a specific data structure. You don't need to write
Writing a wrapper
Given the code attached here, you need to write a class that implements a specific data structure. You don't need to write a lot of code for this, just enough to warp around the code provided to you.
THE CODE:
Build a queue giNode.cpp
#include
#include
#include
#include
#include
#include
template
class Node
public:
T element;
Node next;
Node : elementT nextnullptr
NodeT input : elementinput nextnullptr
NodeT input, Node nextNode : elementinput nextnextNode
std::string toString const
std::ostringstream oss;
oss element ;
return oss.str;
T getElement const
return thiselement;
Node clone const
return new Nodethiselement, thisnext;
;
givLinkedList.cpp
#include
#include
#include
#include
#include
#include
#include "Node.cpp
template
class MyLinkedList
public:
int size;
Node head;
MyLinkedList : size headnew Node
MyLinkedListT input : size headnew NodeT new Nodeinput
MyLinkedListconst std::vector& input : sizeinputsize
headnew NodeT chelperinput
Node chelperconst std::vector& input
if inputempty
return new Node;
else if inputsize
return new Nodeinput;
else
std::vector partialvecinputbegin input.end;
return new Nodeinput chelperpartialvec;
T atIndexint x const
if x x thissize return T;
Node temp thishead;
for int i ; i x; i temp tempnext;
return tempgetElement;
T getFirst const
if headnext nullptrreturn headnextelement;
else return T;
T getLast const
return thisatIndexthissize ;
static std::vector toArrayconst MyLinkedList& input
std::vector result;
Node temp input.head;
for int i ; i input.size; i
temp tempnext;
result.pushbacktempelement;
return result;
static MyLinkedList concatconst MyLinkedList& a const MyLinkedList&
b
std::vector partialResult;
if asize && bsize return MyLinkedList;
if asize partialResult toArrayb;
else
partialResult toArraya;
std::vector bArray toArrayb;
partialResult.insertpartialResultend bArray.begin
bArray.end;
return MyLinkedListpartialResult;
MyLinkedList addFirstT input
return MyLinkedList::concatMyLinkedListinput this;
MyLinkedList addLastT input
return MyLinkedList::concatthis MyLinkedListinput;
MyLinkedList removeFirst
if thissize return MyLinkedList;
else
std::vector elements toArraythis;
elements.eraseelementsbegin;
return MyLinkedListelements;
std::string toString const
std::string result ;
Node temp thishead;
for int i ; i thissize; i
temp tempnext;
result temptoString;
return result;
functionmember to get the size of the list
int getSize const
return size;
functionmember to print the elements of the list
void display const
Node current head;
while current nullptr
std::cout currentdata ;
current currentnext;
std::cout "nullptr" std::endl;
;
given the code provided above
The code doesn't really require you to do much but what you must keep in mind is that this linked list is similar to the Matrix project and the complexfraction object we did for the labs every time you perform an operation. So you need to do the same the same thing as we did there.
The methods you need are:
addFirstT input
addLastT input
removeFirst
atIndexint x
Methods you need to implement:
enqueue
dequeue
Peek
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
