Question

In C++, for the provided template linked list class create a derived class of it which...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include "LinkedList.h"
#include <iostream>

using namespace std;


template <class T>
class LinkedListExtended : public LinkedList<int>{
  
  
   public:
       LinkedListExtended(void){  
           head = NULL;
       }
       T findMin();
       T findMax();
      
};

template <class T>
T LinkedListExtended<T>::findMin()
{
ListNode<T> *nodePtr = head;

T minElement = nodePtr -> data;

while (nodePtr != NULL)
{
if( minElement > nodePtr -> data)
minElement = nodePtr-> data;
     
   nodePtr = nodePtr -> next;
}

return minElement;
}


template <class T>
T LinkedListExtended<T>::findMax()
{
ListNode<T> *nodePtr = head;

T maxElement = nodePtr -> data;

while (nodePtr != nullptr)
{
if( maxElement < nodePtr -> data)
maxElement = nodePtr-> data;
     
   nodePtr = nodePtr -> next;
}

return maxElement;
}

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

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

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

int main()
{
//LinkedList<int>* mySimpleList = new LinkedList<int>();
LinkedListExtended<int>* myList = new LinkedListExtended<int>();
myList->AppendNode(5);
myList->AppendNode(2);
myList->AppendNode(7);
myList->AppendNode(9);

myList->DisplayList();

cout << "Smallest Element : " << myList -> findMin() <<endl;
cout << "Largest Element : " << myList -> findMax() <<endl;


delete myList;
PauseProgram();
return 0;
}

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

$g++ source.cpp

$./a.out

5

2

7

9

Smallest Element : 2

Largest Element : 9

1. Ensure, private section in LinkedList.h is made protected to make things as simple as possible.

Add a comment
Know the answer?
Add Answer to:
In C++, for the provided template linked list class create a derived class of it which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT