Question
In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high
In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template.
LinkedList.h
#pragma once #include
template
ListNode(T data) { this->data = data; next = nullptr; } };
template
public: LinkedList(); ~LinkedList(); void AppendNode(T data); void InsertNode(T data); void DeleteNode(T data); void DisplayList(); };
template
template
nodePtr = head;
while (nodePtr != nullptr) { nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode; } }
template
newNode = new ListNode
if (head == nullptr) head = newNode; else { nodePtr = head;
while (nodePtr->next != nullptr) { nodePtr = nodePtr->next; }
nodePtr->next = newNode; } }
template
newNode = new ListNode
if (head == nullptr) { head = newNode; } else if (head->data >= newNode->data) { newNode->next = head; head = newNode; } else { currentNode = head; previousNode = nullptr;
while (currentNode != nullptr && currentNode->data < newNode->data) { previousNode = currentNode; currentNode = currentNode->next; }
previousNode->next = newNode; newNode->next = currentNode; } }
template
if (head != nullptr) { if (head->data == data) { currentNode = head->next; delete head; head = currentNode; } else { currentNode = head;
while (currentNode != nullptr && currentNode->data != data) { previousNode = currentNode; currentNode = currentNode->next; }
if (currentNode != nullptr) { previousNode->next = currentNode->next; delete currentNode; } } } }
template
nodePtr = head;
while (nodePtr != nullptr) { cout << nodePtr->data << endl; nodePtr = nodePtr->next; } }
Source.cpp
#include "LinkedList.h" #include
void PauseProgram() { string temp; cout << "Press Enter to Continue" << endl; getline(cin, temp); }
int main() { LinkedList
myList->AppendNode(5); myList->AppendNode(2); myList->AppendNode(7);
myList->DisplayList();
cout << "Sorted List" << endl; LinkedList
myList2->DisplayList();
cout << "Deleting from lists" << endl;
LinkedList
myList3->DeleteNode(9);//Delete with empty list cout << endl;
myList->DeleteNode(5); //Delete first node myList->DisplayList(); cout << endl;
myList2->DeleteNode(5); //Delete middle of list myList2->DisplayList(); cout << endl; myList2->DeleteNode(7); //Delete last node myList2->DisplayList(); cout << endl;
delete myList; delete myList2; delete myList3;
PauseProgram(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started