Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this program you will use the linked list created in Assignment 1 (provided b

ID: 3764739 • Letter: I

Question

In this program you will use the linked list created in Assignment 1 (provided below). 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<WeatherStats>). 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.

#include <iostream>
#include <set>

using namespace std;

template<class T>
class Node
{
public:
T data;
Node<T> * next;
Node<T>(const T& d):data(d), next() {}
Node<T>(const Node<T>& copyNode) : data(copyNode.data), next() {}

private:
Node<T>& operator=(const Node<T>&);
};

template<class T>
class LinkedList
{
public:

Node<T> * head;
Node<T> * tail;

LinkedList(const LinkedList& LL);
LinkedList& operator=(LinkedList byValList);
LinkedList(): head(NULL), tail(NULL) {}
LinkedList(Node<T> * newNode) : head(newNode), tail(newNode) {}
~LinkedList();

void insert(T val);
void print();
   int search(int val);
  
};

template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& LL) : head(NULL), tail(NULL)
{
const Node<T> * curr = LL.head;

if (!head && curr)
{
head = new Node<T>(curr->data);
tail = head;
curr = curr->next;
}

while (curr)
{
Node<T> * newNode = new Node<T>(curr->data);
tail->next = newNode;
tail = newNode;
curr = curr->next;
}
}

template<class T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList byValList)
{
std::swap(head, byValList.head);
return *this;
}

template<class T>
LinkedList<T>::~LinkedList()
{
Node<T> * curr = head;
while (head)
{
head = head->next;
delete curr;
curr = head;
}
}

template<class T>
void LinkedList<T>::insert(T val)
{
Node<T> * newNode = new Node<T>(val);
if (tail == NULL)
{
newNode->next = tail;
tail = newNode;
head = newNode;
return;
}
tail->next = newNode;
tail = tail->next;
}

template<class T>
void LinkedList<T>::print()
{
Node<T> * curr = head;
while (curr)
{
cout<<curr->data<<" --> ";
curr = curr->next;
}
cout<<"NULL"<<endl;
}

template<class T>
int LinkedList<T>::search(int val)
{
Node<T>* pNode = head;
  
/* traverse the list */
while (pNode != NULL) {
/* check for the target here */
if(pNode->_value == val)
{
return 1;
}
/* move to the next one */
pNode = pNode->_pNext;
}
return 0;
}

int main()
{
LinkedList<int> List1(new Node<int>(7));
List1.insert(1);
List1.insert(6);
List1.insert(5);
List1.insert(4);

LinkedList<int> List2(new Node<int>(5));
List2.insert(9);
List2.insert(2);

List1.print();
List2.print();

return 0;
}

Explanation / Answer

ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
template <class L>
class LinkedList
{
private:
struct List
{
L value;
struct List *next;
};
List *head;
public:
LinkedList()
{
head = NULL;
}
~LinkedList();
void appendNode(L);
void insertNode(L);
void deleteNode(L);
L searchList(int);
void displayList() const;
};
template <class L>
void LinkedList<L>::appendNode(L num)
{
List *newNode;
List *nodePtr;
newNode = new List;
newNode->value = num;
newNode->next = NULL;
if (!head)
{
head = newNode;
}
else
{
nodePtr = head;
while (nodePtr->next)
{
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
}
}
template <class L>
void LinkedList<L>::insertNode(L num)
{
List *newNode;
List *nodePtr;
List *previousNode = NULL;
newNode = new List;
newNode->value = num;
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodePtr = head;
previousNode = NULL;
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class L>
void LinkedList<L>::deleteNode(L num)
{
List *nodePtr;
List *previousNode;
if (!head)
{
return;
}
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
template <class L>
void LinkedList<L>::displayList() const
{
List *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << " ";
nodePtr = nodePtr->next;
}
}
template <class L>
L LinkedList<L>::searchList(int value)
{
List *nodePtr;
int location = -1;
if (!head)
{
cout << "Element not found in list." << endl;
return -1;
}
nodePtr = head;
while (nodePtr)
{
location++;
if (nodePtr->value == value)
{
return location;
}
else
{
nodePtr = nodePtr->next;
}
}
return -1;
}
template <class L>
LinkedList<L>::~LinkedList()
{
List *nodePtr;
List *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}

weather stats.h

#ifndef WEATHERSTATS_H
#define WEATHERSTATS_H
class WeatherStats
{
private:
double rainFall, snowFall, sunShine;
public:
WeatherStats();
WeatherStats(double, double, double);
void setRainFall(double);
void setSnowFall(double);
void setSunShine(double);
double getRainFall();
double getSnowFall();
double getSunShine();
};
WeatherStats::WeatherStats()
{
rainFall = 0;
snowFall = 0;
sunShine = 0;
}
WeatherStats::WeatherStats(double rain, double snow,double sun)
{
rainFall = rain;
snowFall = snow;
sunShine = sun;
}
void WeatherStats::setRainFall(double rf)
{
rainFall = rf;
}
void WeatherStats::setSnowFall(double sf)
{
snowFall = sf;
}
void WeatherStats::setSunShine(double ss)
{
sunShine = ss;
}
double WeatherStats::getRainFall()
{
return rainFall;
}
double WeatherStats::getSnowFall()
{
return snowFall;
}
double WeatherStats::getSunShine()
{
return sunShine;
}


Main.cpp

#include "WeatherStats.h"
#include "LinkedList.h"
#include <iostream>
#include <list>
using namespace std;
int main()
{
int months;
double rainFall;
double snowFall;
double sunShine;
LinkedList<WeatherStats> weather;
WeatherStats forecast;
cout << "How many months do you wish to enter "<< "weather statistics for? ";
cin >> months;
for (int i = 0; i < months; i++)
{
cout << "Enter the amounts for month " << i+1 << endl;
cout << "Rainfall: ";
cin >> rainFall;
cout << "Snowfall: ";
cin >> snowFall;
cout << "Sunshine: ";
cin >> sunShine;
cout << endl;
forecast.setRainFall(rainFall);
forecast.setSnowFall(snowFall);
forecast.setSunShine(sunShine);
cout<<endl;
}
weather.appendNode(forecast);
weather.displayList();
system("Pause");
}   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote