Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

-------------------------------------------- #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 {

image text in transcribed

--------------------------------------------

#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* link{ nullptr };
};
//******************
// 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* head{ nullptr };
Node* tail{ nullptr };
unsigned int count{ 0 };
};
template
LinkedListBase::~LinkedListBase() {
Node* temp = head;
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::getStringFromList() {
stringstream ss;
if (!this->head) {
ss
}
else {
Node* currentNode = this->head;
ss data;
currentNode = currentNode->link;
while (currentNode) {
ss data;
currentNode = currentNode->link;
};
}
return ss.str();
}
template
T LinkedListBase::getLast() const {
if (this->tail) {
return this->tail->data;
}
else {
throw 1;
}
}
template
void LinkedListBase::pushFront(const T& data) {
if (!head) {
// Scenario: The list is empty
Node* temp = new Node();
temp->data = data;
this->head = temp;
this->tail = temp;
count++;
}
else {
// Scenario: One or more nodes
Node* temp = new Node();
temp->data = data;
temp->link = this->head;
this->head = temp;
count++;
}
}
template
void LinkedListBase::pushBack(const T& data) {
if (!head) {
//Scenario: The list is empty
Node* temp = new Node();
temp->data = data;
this->head = temp;
this->tail = temp;
count++;
}
else {
Node* temp = new Node();
temp->data = data;
this->tail->link = temp;
this->tail = temp;
count++;
}
}
template
void LinkedListBase::popFront() {
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 = nullptr;
temp = this->head->link;
delete this->head;
this->head = temp;
count--;
}
}
template
void LinkedListBase::popBack() {
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 = nullptr;
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions

Question

Explain the steps involved in training programmes.

Answered: 1 week ago

Question

What are the need and importance of training ?

Answered: 1 week ago

Question

socialist egalitarianism which resulted in wage levelling;

Answered: 1 week ago

Question

soyuznye (all-Union, controlling enterprises directly from Moscow);

Answered: 1 week ago