Question: -------------------------------------------- #include #include #include using std::string; using std::cin; using std::cout; using std::cerr; using std::endl; using std::stringstream; //****************** //The Node class //****************** template class Node {

--------------------------------------------
| #include | |
| #include | |
| #include | |
| using std::string; | |
| using std::cin; | |
| using std::cout; | |
| using std::cerr; | |
| using std::endl; | |
| using std::stringstream; | |
| //****************** | |
| //The Node class | |
| //****************** | |
| template | |
| class Node { | |
| public: | |
| T data{}; | |
| Node | |
| }; | |
| //****************** | |
| // The linked list base class | |
| //****************** | |
| template | |
| class LinkedListBase { | |
| public: | |
| ~LinkedListBase(); | |
| T getFifthElement() const { cerr | |
| void insertNewFifthElement(const T& data) { cerr | |
| void deleteFifthElement() { cerr | |
| void swapFifthAndSeventhElement() { cerr | |
| T getLast() const; | |
| void pushFront(const T& data); | |
| void pushBack(const T& data); | |
| void popFront(); | |
| void popBack(); | |
| string getStringFromList(); | |
| protected: | |
| Node | |
| Node | |
| unsigned int count{ 0 }; | |
| }; | |
| template | |
| LinkedListBase | |
| Node | |
| while (temp) { | |
| head = head->link; | |
| delete temp; | |
| temp = head; | |
| } | |
| } | |
| //This method helps return a string representation of all nodes in the linked list, do not modify. | |
| template | |
| string LinkedListBase | |
| stringstream ss; | |
| if (!this->head) { | |
| ss | |
| } | |
| else { | |
| Node | |
| ss data; | |
| currentNode = currentNode->link; | |
| while (currentNode) { | |
| ss data; | |
| currentNode = currentNode->link; | |
| }; | |
| } | |
| return ss.str(); | |
| } | |
| template | |
| T LinkedListBase | |
| if (this->tail) { | |
| return this->tail->data; | |
| } | |
| else { | |
| throw 1; | |
| } | |
| } | |
| template | |
| void LinkedListBase | |
| if (!head) { | |
| // Scenario: The list is empty | |
| Node | |
| temp->data = data; | |
| this->head = temp; | |
| this->tail = temp; | |
| count++; | |
| } | |
| else { | |
| // Scenario: One or more nodes | |
| Node | |
| temp->data = data; | |
| temp->link = this->head; | |
| this->head = temp; | |
| count++; | |
| } | |
| } | |
| template | |
| void LinkedListBase | |
| if (!head) { | |
| //Scenario: The list is empty | |
| Node | |
| temp->data = data; | |
| this->head = temp; | |
| this->tail = temp; | |
| count++; | |
| } | |
| else { | |
| Node | |
| temp->data = data; | |
| this->tail->link = temp; | |
| this->tail = temp; | |
| count++; | |
| } | |
| } | |
| template | |
| void LinkedListBase | |
| if (!head) { | |
| // Scenario: The list is empty | |
| cout | |
| return; | |
| } | |
| else if (this->head == this->tail) { | |
| // Scenario: One node list | |
| this->tail = nullptr; | |
| delete this->head; | |
| this->head = nullptr; | |
| count--; | |
| } | |
| else { | |
| // Scenario: General, at least two or more nodes | |
| Node | |
| temp = this->head->link; | |
| delete this->head; | |
| this->head = temp; | |
| count--; | |
| } | |
| } | |
| template | |
| void LinkedListBase | |
| if (!this->head) { | |
| // Scenario: The list is empty | |
| cout | |
| return; | |
| } | |
| else if (this->head == this->tail) { | |
| // Scenario: One node list | |
| this->tail = nullptr; | |
| delete this->head; | |
| this->head = nullptr; | |
| count--; | |
| } | |
| else { | |
| // Scenario: General, at least two or more nodes | |
| Node | |
| temp = head; | |
| while (temp->link != this->tail) { | |
| temp = temp->link; // This is like i++; | |
| } | |
| // temp is now at the second to tail node | |
| delete this->tail; | |
| this->tail = temp; | |
| this->tail->link = nullptr; | |
| count--; | |
| } | |
| } | |
| //********************************** | |
| //Write your code below here | |
| //********************************** | |
| template | |
| class SinglyLinkedList : public LinkedListBase | |
| public: | |
| // TODO, your methods declarations here | |
| }; | |
| // TODO, your method definitions here | |
| //********************************** | |
| //Write your code above here | |
| //********************************** |
For each of these methods, remember to draw out the algorithm on paper, and trace the process through an exact sequence of steps. Also, it is highly effective to organize methods into sections of scenarios, going from the most specific to the most general. The lecture videos give many hints and strategies. Complete the following methods for the singly linked list. - T getFifthelement () const. This method returns the data at the fifth node of a linked list (the count starts at 1 , not at 0 ). It should throw an std::out_of range if there is no fifth element. - void insertNewFifthElement (const T\& value). This method inserts a node containing value between the existing 4th and 5th nodes, so that the original 5th node becomes a 6th node. If the collection has only 4 values, then insert it as a new last value. If the collection has only 3 or fewer values, don't insert. - void deleteFifthElement () . This method deletes the 5th node. If there was a 6th node, the 4th node now points to the 6th node. If there was no 6th node, the 4th node becomes the new back node. - void swapFifthAndSeventhElement (). This method rearranges the 5th and 7t nodes. You are not allowed to swap the data in the nodes, you may instead only rearrange pointers. No illustration is given here. A straightforward start to solve this method utilizes four temporary pointers, with one each pointing to the fourth, fifth, sixth, and seventh node. Note that because this class inherits from a base class, to access the data members, 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/templatesthnondependent-namelookup-members)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
