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() : element(T()), next(nullptr){}
Node(T input) : element(input), next(nullptr){}
Node(T input, Node* nextNode) : element(input), next(nextNode){}
std::string toString() const {
std::ostringstream oss;
oss <<"<"<< element <<">";
return oss.str();
}
T getElement() const {
return this->element;
}
Node* clone() const {
return new Node(this->element, this->next);
}
};
givLinkedList.cpp
#include
#include
#include
#include
#include
#include
#include "Node.cpp"
template
class MyLinkedList {
public:
int size;
Node* head;
MyLinkedList() : size(0), head(new Node()){}
MyLinkedList(T input) : size(1), head(new Node(T(), new Node(input))){}
MyLinkedList(const std::vector& input) : size(input.size()),
head(new Node(T(), chelper(input))){}
Node* chelper(const std::vector& input)
{
if (input.empty())
{
return new Node();
}
else if (input.size()==1)
{
return new Node(input[0]);
}
else
{
std::vector partial_vec(input.begin()+1, input.end());
return new Node(input[0], chelper(partial_vec));
}
}
T atIndex(int x) const
{
if (x <0|| x >= this->size) return T();
Node* temp = this->head;
for (int i =0; i <= x; ++i) temp = temp->next;
return temp->getElement();
}
T getFirst() const
{
if (head->next != nullptr)return head->next->element;
else return T();
}
T getLast() const
{
return this->atIndex(this->size -1);
}
static std::vector toArray(const MyLinkedList& input)
{
std::vector result;
Node* temp = input.head;
for (int i =0; i < input.size; ++i)
{
temp = temp->next;
result.push_back(temp->element);
}
return result;
}
static MyLinkedList concat(const MyLinkedList& a, const MyLinkedList&
b)
{
std::vector partialResult;
if (a.size ==0 && b.size ==0) return MyLinkedList();
if (a.size ==0)partialResult = toArray(b);
else
{
partialResult = toArray(a);
std::vector bArray = toArray(b);
partialResult.insert(partialResult.end(), bArray.begin(),
bArray.end());
}
return MyLinkedList(partialResult);
}
MyLinkedList addFirst(T input)
{
return MyLinkedList::concat(MyLinkedList(input), this);
}
MyLinkedList addLast(T input)
{
return MyLinkedList::concat(this, MyLinkedList(input));
}
MyLinkedList removeFirst()
{
if (this->size <=1) return MyLinkedList();
else {
std::vector elements = toArray(this);
elements.erase(elements.begin());
return MyLinkedList(elements);
}
}
std::string toString() const
{
std::string result ="";
Node temp = this->head;
for (int i =0; i < this->size; ++i)
{
temp = temp->next;
result += temp->toString();
}
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 << current->data <<"->";
current = current->next;
}
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 complex/fraction 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:
addFirst(T input)
addLast(T input)
removeFirst()
atIndex(int 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!