1. Engineering
  2. Computer Science
  3. in c for the provided template linked list class create...

Question: in c for the provided template linked list class create...

Question details

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

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

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

template <class T>
class LinkedList
{
   private:
       ListNode<T>* head;

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


template <class T>
LinkedList<T>::LinkedList()
{
   head = nullptr;
}

template <class T>
LinkedList<T>::~LinkedList()
{
   ListNode<T>* nextNode = nullptr;
   ListNode<T>* nodePtr = nullptr;

   nodePtr = head;

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

       delete nodePtr;

       nodePtr = nextNode;
   }
}

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

   newNode = new ListNode<T>(data);

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

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

       nodePtr->next = newNode;
   }
}

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

   newNode = new ListNode<T>(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 <class T>
void LinkedList<T>::DeleteNode(T data)
{
   ListNode<T>* currentNode = nullptr;
   ListNode<T>* 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 <class T>
void LinkedList<T>::DisplayList()
{
   ListNode<T> *nodePtr = nullptr;

   nodePtr = head;

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

Source.cpp

#include "LinkedList.h"
#include <string>
using namespace std;

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

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

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

   myList->DisplayList();

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

   myList2->DisplayList();

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

   LinkedList<int>* myList3 = new LinkedList<int>();

   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;
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution