Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using namespace std;

template class ListNode { public: T data; ListNode* next;

ListNode(T data) { this->data = data; next = nullptr; } };

template class LinkedList { private: ListNode* head;

public: LinkedList(); ~LinkedList(); void AppendNode(T data); void InsertNode(T data); void DeleteNode(T data); void DisplayList(); };

template LinkedList::LinkedList() { head = nullptr; }

template LinkedList::~LinkedList() { ListNode* nextNode = nullptr; ListNode* nodePtr = nullptr;

nodePtr = head;

while (nodePtr != nullptr) { nextNode = nodePtr->next;

delete nodePtr;

nodePtr = nextNode; } }

template void LinkedList::AppendNode(T data) { ListNode* newNode = nullptr; ListNode* nodePtr = nullptr;

newNode = new ListNode(data);

if (head == nullptr) head = newNode; else { nodePtr = head;

while (nodePtr->next != nullptr) { nodePtr = nodePtr->next; }

nodePtr->next = newNode; } }

template void LinkedList::InsertNode(T data) { ListNode* newNode = nullptr; ListNode* currentNode = nullptr; ListNode* previousNode = nullptr;

newNode = new ListNode(data);

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 void LinkedList::DeleteNode(T data) { ListNode* currentNode = nullptr; ListNode* previousNode = nullptr;

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 void LinkedList::DisplayList() { ListNode *nodePtr = nullptr;

nodePtr = head;

while (nodePtr != nullptr) { cout << nodePtr->data << endl; nodePtr = nodePtr->next; } }

Source.cpp

#include "LinkedList.h" #include using namespace std;

void PauseProgram() { string temp; cout << "Press Enter to Continue" << endl; getline(cin, temp); }

int main() { LinkedList* myList = new LinkedList();

myList->AppendNode(5); myList->AppendNode(2); myList->AppendNode(7);

myList->DisplayList();

cout << "Sorted List" << endl; LinkedList* myList2 = new LinkedList(); myList2->InsertNode(5); myList2->InsertNode(2); myList2->InsertNode(7); myList2->InsertNode(6);

myList2->DisplayList();

cout << "Deleting from lists" << endl;

LinkedList* myList3 = new 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

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_2

Step: 3

blur-text-image_3

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

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions