Question: I need this c++ code to be completed ! Thanx #include #include #include #include using std::cin; using std::cout; using std::cerr; using std::endl; using std::string; using

I need this c++ code to be completed ! Thanx

I need this c++ code to be completed ! Thanx #include #include

#include

#include

#include

#include

using std::cin;

using std::cout;

using std::cerr;

using std::endl;

using std::string;

using std::stringstream;

//************************************************************************

//A class I designed to help keep track of how much memory you allocate

//Do not modify, this is not part of your assignment, it just helps test it.

//For this to work, a class needs to inherit off of this one.

//Then this does the rest of the work, since it

//overloads new, new[], delete, and delete[].

//************************************************************************

class ManageMemory {

public:

static std::size_t getTotalSize() {

std::size_t total = 0;

std::map::iterator iter;

for (iter = mapOfAllocations.begin(); iter != mapOfAllocations.end(); ++iter) {

total += iter->second;

}

return total;

}

//I overloaded the new and delete keywords so I could manually track allocated memory.

void* operator new(std::size_t x){

void *ptr = ::operator new(x);

mapOfAllocations[ptr] = x;

return ptr;

}

void* operator new[](std::size_t x) {

void *ptr = ::operator new[](x);

mapOfAllocations[ptr] = x;

return ptr;

}

void operator delete(void* x) {

mapOfAllocations.erase(x);

::operator delete(x);

}

void operator delete[](void* x) {

mapOfAllocations.erase(x);

::operator delete[](x);

}

private:

static std::map mapOfAllocations;

};

std::map ManageMemory::mapOfAllocations;

//******************

//The node class

//******************

template

class Node : public ManageMemory {

public:

T data;

Node *forward{ nullptr };

Node *backward{ nullptr };

};

//******************

// The linked list base class

//******************

template

class LinkedListBase : public ManageMemory {

public:

string getStringFromList();

string getStringBackwardsFromList();

T getFifthElement() const { cerr

void insertNewFifthElement(const T& value) { cerr

void deleteFifthElement() { cerr

protected:

Node *front{ nullptr };

Node *back{ nullptr };

int currentSize{ 0 };

};

//This method helps return a string representation of all nodes in the linked list, do not modify.

template

string LinkedListBase::getStringFromList() {

stringstream ss;

if (!front) {

ss

}

else {

Node *currentNode = front;

ss data;

currentNode = currentNode->forward;

while (currentNode) {

ss data;

currentNode = currentNode->forward;

};

}

return ss.str();

}

//This method helps return a string representation of all nodes in the linked list, do not modify.

template

string LinkedListBase::getStringBackwardsFromList() {

stringstream ss;

if (!front) {

ss

}

else {

Node *currentNode = back;

ss data;

currentNode = currentNode->backward;

while (currentNode) {

ss data;

currentNode = currentNode->backward;

};

}

return ss.str();

}

//******************

//The singly linked list base class

//******************

template

class SinglyLinkedListBase : public LinkedListBase {

public:

//public members of the SinglyLinkedList class

~SinglyLinkedListBase();

void insertFront(const T&);

void insertBack(const T&);

};

template // destructor

SinglyLinkedListBase::~SinglyLinkedListBase() {

Node *temp;

while (this->front) {

temp = this->front;

this->front = this->front->forward;

delete temp;

}

this->back = nullptr;

this->currentSize = 0;

}

template

void SinglyLinkedListBase::insertFront(const T& value) {

Node *temp = new Node();

temp->data = value;

if (!this->front) {

temp->forward = this->front;

}

else {

this->back = temp;

}

this->front = temp;

this->currentSize++;

}

template

void SinglyLinkedListBase::insertBack(const T& value) {

Node* temp = new Node;

temp->data = value;

if (!this->front) {

this->front = temp;

}

else {

this->back->forward = temp;

}

this->back = temp;

this->currentSize++;

}

//******************

//The double linked list base class

//******************

template

class DoublyLinkedListBase : public LinkedListBase {

public:

//public members of the SinglyLinkedList class

~DoublyLinkedListBase();

void insertFront(const T&);

void insertBack(const T&);

};

template // destructor

DoublyLinkedListBase::~DoublyLinkedListBase() {

Node *temp;

while (this->front) {

temp = this->front;

this->front = this->front->forward;

delete temp;

}

this->back = nullptr;

this->currentSize = 0;

}

template

void DoublyLinkedListBase::insertFront(const T& value) {

Node *temp = new Node();

temp->data = value;

if (!this->front) {

this->front->backward = temp;

temp->forward = this->front;

}

else {

this->back = temp;

}

this->front = temp;

this->currentSize++;

}

template

void DoublyLinkedListBase::insertBack(const T& value) {

Node* temp = new Node;

temp->data = value;

if (!this->front) {

this->front = temp;

}

else {

this->back->forward = temp;

temp->backward = this->back;

}

this->back = temp;

this->currentSize++;

}

//**********************************

//Write your code below here

//**********************************

template

class SinglyLinkedList : public SinglyLinkedListBase {

public:

//TODO: Declare your methods here.

};

//TODO: Define your methods here.

template

class DoublyLinkedList : public DoublyLinkedListBase {

public:

//TODO: Declare your methods here.

};

//TODO: Define your methods here.

//**********************************

//Write your code above here

//**********************************

//This helps with testing, do not modify.

bool checkTest(string testName, string whatItShouldBe, string whatItIs) {

if (whatItShouldBe == whatItIs) {

cout

return true;

}

else {

cout

return false;

}

}

//This helps with testing, do not modify.

bool checkTest(string testName, int whatItShouldBe, int whatItIs) {

if (whatItShouldBe == whatItIs) {

cout

return true;

}

else {

cout

return false;

}

}

//This helps with testing, do not modify.

bool checkTestMemory(string testName, int whatItShouldBe, int whatItIs) {

if (whatItShouldBe == whatItIs) {

cout

return true;

}

else {

cout

return false;

}

}

//This helps with testing, do not modify.

void testGetFifthElement() {

SinglyLinkedList *si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testgetFifthElement #1", "10 11 12 13 14 15 16 17 18 19", si->getStringFromList());

//Test retrieving item.

int item = si->getFifthElement();

checkTest("testgetFifthElement #2", 14, item);

delete si;

si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testgetFifthElement #3", "10 11 12 13 14", si->getStringFromList());

//Test retrieving item.

item = si->getFifthElement();

checkTest("testgetFifthElement #4", 14, item);

delete si;

si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testgetFifthElement #5", "10 11 12 13", si->getStringFromList());

//Try to access out of bounds.

string caughtError = "";

try {

item = si->getFifthElement();

}

catch (int) {

caughtError = "caught";

}

checkTest("testgetFifthElement #6", "caught", caughtError);

delete si;

SinglyLinkedList *ss = new SinglyLinkedList;

ss->insertBack("Multi Pass");

ss->insertBack("Lelu Dallas");

ss->insertBack("BIG BADA BOOM");

ss->insertBack("Bruce Willis");

ss->insertBack("Fried Chicken");

ss->insertBack("EEEAAAAAAAeeeaaaaaEEeeAAAEEaaaaAA");

checkTest("testgetFifthElement #7", "Fried Chicken", ss->getFifthElement());

delete ss;

DoublyLinkedList *di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testgetFifthElement #8", "10 11 12 13 14 15 16 17 18 19", di->getStringFromList());

//Test retrieving item.

item = di->getFifthElement();

checkTest("testgetFifthElement #9", 14, item);

delete di;

di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testgetFifthElement #10", "10 11 12 13 14", di->getStringFromList());

//Test retrieving item.

item = di->getFifthElement();

checkTest("testgetFifthElement #11", 14, item);

delete di;

di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testgetFifthElement #12", "10 11 12 13", di->getStringFromList());

//Try to access out of bounds.

caughtError = "";

try {

item = di->getFifthElement();

}

catch (int) {

caughtError = "caught";

}

checkTest("testgetFifthElement #13", "caught", caughtError);

delete di;

SinglyLinkedList *ds = new SinglyLinkedList;

ds->insertBack("Multi Pass");

ds->insertBack("Lelu Dallas");

ds->insertBack("BIG BADA BOOM");

ds->insertBack("Bruce Willis");

ds->insertBack("Fried Chicken");

ds->insertBack("EEEAAAAAAAeeeaaaaaEEeeAAAEEaaaaAA");

checkTest("testgetFifthElement #14", "Fried Chicken", ds->getFifthElement());

delete ds;

}

//This helps with testing, do not modify.

void testInsertNewFifthElement() {

SinglyLinkedList *si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testInsertNewFifthElement #1", "10 11 12 13 14 15 16 17 18 19", si->getStringFromList());

//Test inserting an item

si->insertNewFifthElement(97);

checkTest("testInsertNewFifthElement #2", "10 11 12 13 97 14 15 16 17 18 19", si->getStringFromList());

delete si;

si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testInsertNewFifthElement #3", "10 11 12 13 14", si->getStringFromList());

//Test inserting an item

si->insertNewFifthElement(97);

checkTest("testInsertNewFifthElement #4", "10 11 12 13 97 14", si->getStringFromList());

delete si;

si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testInsertNewFifthElement #5", "10 11 12 13", si->getStringFromList());

//Test inserting an item

si->insertNewFifthElement(97);

checkTest("testInsertNewFifthElement #6", "10 11 12 13 97", si->getStringFromList());

delete si;

DoublyLinkedList *di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testInsertNewFifthElement #7", "10 11 12 13 14 15 16 17 18 19", di->getStringFromList());

checkTest("testInsertNewFifthElement #8", "19 18 17 16 15 14 13 12 11 10", di->getStringBackwardsFromList());

//Test inserting an item

di->insertNewFifthElement(97);

checkTest("testInsertNewFifthElement #9", "10 11 12 13 97 14 15 16 17 18 19", di->getStringFromList());

checkTest("testInsertNewFifthElement #10", "19 18 17 16 15 14 97 13 12 11 10", di->getStringBackwardsFromList());

delete di;

di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testInsertNewFifthElement #11", "10 11 12 13 14", di->getStringFromList());

//Test inserting an item

di->insertNewFifthElement(97);

checkTest("testInsertNewFifthElement #12", "10 11 12 13 97 14", di->getStringFromList());

checkTest("testInsertNewFifthElement #13", "14 97 13 12 11 10", di->getStringBackwardsFromList());

delete di;

di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testInsertNewFifthElement #14", "10 11 12 13", di->getStringFromList());

//Test inserting an item

di->insertNewFifthElement(97);

checkTest("testInsertNewFifthElement #15", "10 11 12 13 97", di->getStringFromList());

checkTest("testInsertNewFifthElement #16", "97 13 12 11 10", di->getStringBackwardsFromList());

delete di;

}

//This helps with testing, do not modify.

void testDeleteFifthElement() {

// Note from the instructor: Please do not delete the actual movie. It's very good and shouldn't be removed.

SinglyLinkedList *si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testDeleteFifthElement #1", "10 11 12 13 14 15 16 17 18 19", si->getStringFromList());

//Test deleting an item

si->deleteFifthElement();

checkTest("testDeleteFifthElement #2", "10 11 12 13 15 16 17 18 19", si->getStringFromList());

delete si;

si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testDeleteFifthElement #3", "10 11 12 13 14 15", si->getStringFromList());

//Test deleting an item

si->deleteFifthElement();

checkTest("testDeleteFifthElement #4", "10 11 12 13 15", si->getStringFromList());

delete si;

si = new SinglyLinkedList;

for (int i = 10; i

si->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testDeleteFifthElement #5", "10 11 12 13 14", si->getStringFromList());

//Test deleting an item

si->deleteFifthElement();

checkTest("testDeleteFifthElement #6", "10 11 12 13", si->getStringFromList());

delete si;

DoublyLinkedList *di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testDeleteFifthElement #7", "10 11 12 13 14 15 16 17 18 19", di->getStringFromList());

checkTest("testDeleteFifthElement #8", "19 18 17 16 15 14 13 12 11 10", di->getStringBackwardsFromList());

//Test deleting an item

di->deleteFifthElement();

checkTest("testDeleteFifthElement #9", "10 11 12 13 15 16 17 18 19", di->getStringFromList());

checkTest("testDeleteFifthElement #10", "19 18 17 16 15 13 12 11 10", di->getStringBackwardsFromList());

delete di;

di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testDeleteFifthElement #11", "10 11 12 13 14 15", di->getStringFromList());

//Test deleting an item

di->deleteFifthElement();

checkTest("testDeleteFifthElement #12", "10 11 12 13 15", di->getStringFromList());

checkTest("testDeleteFifthElement #13", "15 13 12 11 10", di->getStringBackwardsFromList());

delete di;

di = new DoublyLinkedList;

for (int i = 10; i

di->insertBack(i);

}

//Test just to make sure the data went in the list.

checkTest("testDeleteFifthElement #14", "10 11 12 13 14", di->getStringFromList());

//Test deleting an item

di->deleteFifthElement();

checkTest("testDeleteFifthElement #15", "10 11 12 13", di->getStringFromList());

checkTest("testDeleteFifthElement #16", "13 12 11 10", di->getStringBackwardsFromList());

delete di;

}

void pressAnyKeyToContinue() {

cout

//Linux and Mac users with g++ don't need this

//But everyone else will see this message.

cin.get();

}

int main() {

//Each of these checks how many bytes you have used.

checkTestMemory("Memory Leak/Allocation Test #1", 0, ManageMemory::getTotalSize());

//For your assignment, write the code to make these three methods work

//You should not modify the code here in main.

testGetFifthElement();

checkTestMemory("Memory Leak/Allocation Test #2", 0, ManageMemory::getTotalSize());

testInsertNewFifthElement();

checkTestMemory("Memory Leak/Allocation Test #3", 0, ManageMemory::getTotalSize());

testDeleteFifthElement();

checkTestMemory("Memory Leak/Allocation Test #4", 0, ManageMemory::getTotalSize());

pressAnyKeyToContinue();

return 0;

}

Assignment: Complete the .cpp file so that all tests succeed. Please read the instructions for these methods carefully. I will be grading based on your ability to meet this requirements. In the following file, complete the following methods for both the singly linked list and doubly linked list. This method returns the data at the fifth node of a linked list (the count starts at get Fifth Element() 1, not at 0). It should throw int error (i.e. throw 1;) if there is no fifth element. void insertNewFifthElement (const T& value). the existing 4th and 5th nodes, so that the original 5h node becomes a 6th node. T const. This method inserts a node containing value between . void deleteFifthElmen). This method deletes the 5h node. If there was a 6h node, the 4 node now points to the 6th node. If there was no 6th node, the 4th node becomes the new back node A major need in this assignment is iterating to the 4th or 5th node. Example code on how to get a pointer to the 5th node is as follows: Node?T> *currentNode front; int currentNodeNum1; while (currentNodeNum forward; The above code won't work for all scenarios you will face, but it will get you started in the right direction. For each of these metods, remember to draw out the algorithm on paper, and trace the process through an exact sequence of steps. Also, it's highly effective to organize methods into sections of scenarios, going from the most specific to the most general. Note that because this class inherits from a base class, in order to access the three data members (count, front, back), you need to always use this->. Do not create those three data members again in the derived class. (The detailed reason why is given here: https://isocpp.org/wiki/faq templates#mondependent-name-lookup-members) Assignment: Complete the .cpp file so that all tests succeed. Please read the instructions for these methods carefully. I will be grading based on your ability to meet this requirements. In the following file, complete the following methods for both the singly linked list and doubly linked list. This method returns the data at the fifth node of a linked list (the count starts at get Fifth Element() 1, not at 0). It should throw int error (i.e. throw 1;) if there is no fifth element. void insertNewFifthElement (const T& value). the existing 4th and 5th nodes, so that the original 5h node becomes a 6th node. T const. This method inserts a node containing value between . void deleteFifthElmen). This method deletes the 5h node. If there was a 6h node, the 4 node now points to the 6th node. If there was no 6th node, the 4th node becomes the new back node A major need in this assignment is iterating to the 4th or 5th node. Example code on how to get a pointer to the 5th node is as follows: Node?T> *currentNode front; int currentNodeNum1; while (currentNodeNum forward; The above code won't work for all scenarios you will face, but it will get you started in the right direction. For each of these metods, remember to draw out the algorithm on paper, and trace the process through an exact sequence of steps. Also, it's highly effective to organize methods into sections of scenarios, going from the most specific to the most general. Note that because this class inherits from a base class, in order to access the three data members (count, front, back), you need to always use this->. Do not create those three data members again in the derived class. (The detailed reason why is given here: https://isocpp.org/wiki/faq templates#mondependent-name-lookup-members)

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!