Assignment 1 Design your own linked list class that works as a template class. I
ID: 3764883 • Letter: A
Question
Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide a member function to search the list for an element in the list. The search should return the index (location) of the item in the list. So if it is the first element in the list then it should return 0. If the item is not in the list, it should return -1. Have main create two instances of the linked list with different data types and show that all of the functions work correctly. Assignment 2 In this program you will use the linked list created in Assignment 1. First you will create a class that holds information about the weather for a given month which should be called WeatherStats. It should have data members that are doubles to hold the amount of rain for the month and the amount of snow for the month. It will also have a data member that holds the number of sunny days during the month. It should provide accessors and mutators for the private data members. Main will create an instance of the linked list with a data type of the WeatherStats (LinkedList). The program should ask the user for how many months they wish to enter weather statistics for. The program will then prompt the user for that information (rain, snow and sunny days). The data needs to be stored in the WeatherStats class and it should be appended to the linked list. Main must then call a function that displays the data in the list; this function will call the display function in the linked list. Main will call a function that determines the month with the largest and smallest amount of rain, snow and sunny days. This function should not be part of the linked list. It should appear in the same file as main. A function will need to be added to the linked list that provides an item from the list. The function in the linked list will return the object stored in the list. The function in main will need to request each item in the list one at a time. Another solution is to create a derived class of the linked list, which does all of the work for finding the largest and smallest.
Explanation / Answer
//Starting of header file "LinkedList.h"
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream.h>
template <class T>
class LinkedList
{
private:
class ListNode
{
friend LinkedList<T>;
T value;
ListNode *next;
ListNode(T value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode * head;
public:
int val;
LinkedList()
{head = NULL;}
~LinkedList();
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList();
};
template<class T>
void LinkedList<T>::appendNode(T val)
{
if (head == NULL)
head = new ListNode(val);
else
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr ->next != NULL)
nodePtr = nodePtr->next;
nodePtr->next = new ListNode(val);
}
}
template<class T>
void LinkedList<T>::displayList()
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
template<class T>
void LinkedList<T>::insertNode(T value)
{
ListNode *nodePtr, *previousNodePtr;
if (head == NULL || head->value >= val)
{
head = new ListNode(val, head);
}
else
{
previousNodePtr = head;
nodePtr = head->next;
while(nodePtr != NULL && nodePtr->value < val)
{
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
previousNodePtr->next = new ListNode(val, nodePtr);
}
}
template<class T>
void LinkedList<T>::deleteNode(T val)
{
ListNode *nodePtr, *previousNodePtr;
if(!head)
return;
if (head->value == val)
{
nodePtr = head;
head = head->next;
delete nodePtr;
}
else
{
nodePtr = head;
while (nodePtr != NULL && nodePtr->value != val)
{
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr)
{
previousNodePtr->next = nodePtr->next;
delete nodePtr;
}
}
}
template<class T>
LinkedList<T>::~LinkedList()
{
ListNode *nodePtr, *nextNodePtr;
nodePtr = head;
while (nodePtr != NULL)
{
nextNodePtr = nodePtr->next;
delete nodePtr;
nodePtr = nextNodePtr;
}
}
#endif
//End of Header File
//Starting of main file.
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include "LinkedList.h"
int main()
{
LinkedList<int> list;
list.appendNode(1);
list.appendNode(3);
list.appendNode(2);
list.appendNode(5);
cout << "Here are the initial integers: " << endl;
list.displayList();
cout << endl;
cout << "now inserting the integer 5 " << endl;
list.insertNode(4);
cout << "Here are the nodes now. ";
list.displayList();
cout << endl;
cout << "Now deleting the last node. ";
list.deleteNode(5);
cout << "Here are the nodes left. ";
list.displayList();
//cout << "Here are the values in reverse order: " << endl;
//
//list.displayBackwards();
return 0;
}
//End of main file.