Hello, please compile source code in C++ for the program below. I ask that you i
ID: 3859062 • Letter: H
Question
Hello, please compile source code in C++ for the program below. I ask that you include comments so that someone without a technical background can understand what is going on. Last, please show which portion of your program is a .h or .cpp file and show the program compiles and runs without error. Thanks and please see below.
PROGRAM AS FOLLOWS:
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.
Then, design a program using the linked list you just created. 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.
Explanation / Answer
Below is your code: -
LnkdList.h
#ifndef H_LnkdList
#define H_LnkdList
#include <iostream>
using namespace std;
template <class DataType>
struct Node
{
DataType data;
Node<DataType> *next;
};
template <class DataType>
class LnkdList
{
public:
Node<DataType> *start;
LnkdList();
LnkdList(const LnkdList<DataType> & aplist);
~LnkdList();
LnkdList<DataType> & operator = (const LnkdList<DataType> & rlist);
void Add(const DataType & element);
bool first(DataType & listEl);
inline bool getNext(DataType & listEl);
int Search(const DataType & element);
bool Retrievedata(DataType & element);
bool Delete(DataType & element);
bool isEmpty() const;
void makeEmpty();
void print();
private:
Node<DataType> *end;
Node<DataType> *current;
inline void deepCopy(const LnkdList<DataType> & original);
};
template <class DataType>
void LnkdList<DataType>::print()
{
Node<DataType>* curr = start;
cout << "START --> ";
while (curr)
{
cout << curr->data << " --> ";
curr = curr->next;
}
cout << "NULL" << " ";
}
template <class DataType>
LnkdList<DataType>::LnkdList()
: start(0), current(0), end(0) {}
template <class DataType>
LnkdList<DataType>::LnkdList(const LnkdList<DataType> & a)
{
deepCopy(a);
}
template <class DataType>
LnkdList<DataType>::~LnkdList()
{
makeEmpty();
}
template <class DataType>
LnkdList<DataType> & LnkdList<DataType>::operator = (const LnkdList<DataType> & r)
{
if (this == &r)
return *this;
makeEmpty();
deepCopy(r);
return *this;
}
template <class DataType>
void LnkdList<DataType>::Add(const DataType& param)
{
current = 0;
Node<DataType>* node = new Node<DataType>;
node->data = param;
if (!start)
{
start = node;
end = node;
end->next = 0;
return;
}
end->next = node;
end = end->next;
end->next = 0;
}
template <class DataType>
bool LnkdList<DataType>::first(DataType& param)
{
if (!start) return false;
param = start->data;
current = start;
return true;
}
template <class DataType>
bool LnkdList<DataType>::getNext(DataType& param)
{
if (!current) return false;
current = current->next;
if (!current) return false;
param = current->data;
return true;
}
template <class DataType>
int LnkdList<DataType>::Search(const DataType& param)
{
int position = -1;
DataType temp;
if (first(temp)) do
{
++position;
if (param == temp)
return position;
} while (getNext(temp));
return position;
}
template <class DataType>
bool LnkdList<DataType>::Retrievedata(DataType& param)
{
if (Search(param) == -1)
return false;
param = current->data;
return true;
}
template <class DataType>
bool LnkdList<DataType>::Delete(DataType& param)
{
Node<DataType>* p;
Node<DataType>* prev;
for (prev = 0, p = start; p; prev = p, p = p->next)
if (p->data == param)
break;
if (!p) return false;
if (prev) prev->next = p->next; else start = p->next;
if (p == current) current = current->next;
param = p->data;
delete p;
return true;
}
template <class DataType>
bool LnkdList<DataType>::isEmpty() const
{
return start == 0;
}
template <class DataType>
void LnkdList<DataType>::makeEmpty()
{
while (start)
{
current = start->next;
delete start;
start = current;
}
}
template <class DataType>
inline void LnkdList<DataType>::deepCopy(const LnkdList<DataType> & ori)
{
start = current = NULL;
if (ori.start == NULL)
return;
Node<DataType> *ptr = start = new Node<DataType>;
Node<DataType> *oriptr = ori.start;
ptr->data = oriptr->data;
if (oriptr == ori.current)
current = ptr;
while (oriptr->next != NULL) {
oriptr = oriptr->next;
ptr->next = new Node<DataType>;
ptr = ptr->next;
ptr->data = oriptr->data;
if (oriptr == ori.current)
current = ptr;
}
ptr->next = NULL;
}
#endif
LnkdList.cpp
#include <iostream>
#include "LnkdList.h"
using namespace std;
int main()
{
char c;
int a, arr[5];
int Delete, n, rem;
cout << "Linked list of INTEGERS: ";
LnkdList<int> ll_Int = LnkdList<int>();
cout << "Enter any five integers to add in the list ";
for (int i = 0;i < 5;i++)
{
cin >> arr[i];
}
ll_Int.Add(arr[0]);
ll_Int.Add(arr[1]);
ll_Int.Add(arr[2]);
ll_Int.Add(arr[3]);
ll_Int.Add(arr[4]);
ll_Int.print();
cout << "Enter any number to Delete from the list: ";
cin >> rem;
Delete = rem;
ll_Int.Delete(Delete);
cout << "The number " << rem << " is Deleted from the list ";
ll_Int.print();
cout << "Enter any number to know the index from the list: ";
cin >> a;
n = a;
cout << " Position of (" << n << ") in the array :" << (ll_Int.Search(n)) << " ";
char r, n1;
cout << " Linked list of CHARACTERS: ";
LnkdList<char> ll_Char = LnkdList<char>();
ll_Char.Add('a');
ll_Char.Add('b');
ll_Char.Add('c');
ll_Char.Add('d');
ll_Char.Add('e');
r = 'c';
ll_Char.Delete(r);
ll_Char.print();
cout << "Enter any character from the list: ";
cin >> c;
n1 = c;
cout << " Position of (" << n1 << ") in the array :" << (ll_Char.Search(n1)) << " ";
system("pause");
return 0;
}
Output: -
Linked list of INTEGERS:
Enter any five integers to add in the list
1
2
3
4
5
START --> 1 --> 2 --> 3 --> 4 --> 5 --> NULL
Enter any number to Delete from the list: 5
The number 5 is Deleted from the list
START --> 1 --> 2 --> 3 --> 4 --> NULL
Enter any number to know the index from the list: 3
Position of (3) in the array :2
Linked list of CHARACTERS:
START --> a --> b --> d --> e --> NULL
Enter any character from the list: d
Position of (d) in the array :2
Press any key to continue . . .
For Part 2 use LnkdList.h from above.
WeatherStats.cpp
#include <iostream>
#include<iomanip>
#include "LnkdList.h"
using namespace std;
class WeatherStats
{
private:
string month;
double amtRainPerMonth;
double amtSnowPerMonth;
int sunnyDaysPerMonth;
public:
WeatherStats()
{
amtRainPerMonth = 0;
amtSnowPerMonth = 0;
sunnyDaysPerMonth = 0;
month = "";
}
string getMonth()
{
return month;
}
double getAmtRainPerMonth()
{
return amtRainPerMonth;
}
double getAmtSnowPerMonth()
{
return amtSnowPerMonth;
}
double getSunnyDaysPerMonth()
{
return sunnyDaysPerMonth;
}
void setMonth(string m)
{
month = m;
}
void setAmtRainPerMonth(double ra)
{
amtRainPerMonth = ra;
}
void setAmtSnowPerMonth(double sn)
{
amtSnowPerMonth = sn;
}
void setSunnyDaysPerMonth(int sun)
{
sunnyDaysPerMonth = sun;
}
};
void print(LnkdList<WeatherStats> &ll_stats)
{
Node<WeatherStats> *temp = ll_stats.start;
cout << " Stats: ";
cout << setw(20) << "Month" << setw(15) << " Amount Of Rain" << setw(15) << " Amount Of Snow" << setw(15) << " SunnyDays ";
while (temp)
{
cout << setw(20) << temp->data.getMonth() << setw(15) << temp->data.getAmtRainPerMonth() << setw(15) << temp->data.getAmtSnowPerMonth() << setw(15) << temp->data.getSunnyDaysPerMonth() << " ";
temp = temp->next;
}
}
void calculate(LnkdList<WeatherStats> &ll_stats)
{
Node<WeatherStats> *temp = ll_stats.start;
cout << " Calculations: ";
string maxMonth, minMonth;
double maxRain, minRain;
maxRain = temp->data.getAmtRainPerMonth();
minRain = maxRain;
maxMonth = minMonth = temp->data.getMonth();
while (temp)
{
if (temp->data.getAmtRainPerMonth() < minRain)
{
minRain = temp->data.getAmtRainPerMonth();
minMonth = temp->data.getMonth();
}
if (temp->data.getAmtRainPerMonth() > maxRain)
{
maxRain = temp->data.getAmtRainPerMonth();
maxMonth = temp->data.getMonth();
}
temp = temp->next;
}
cout << "Maximum rain :" << maxRain << " in month: " << maxMonth << " ";
cout << "Minimum rain :" << minRain << " in month: " << minMonth << " ";
temp = ll_stats.start;
double maxSnow, minSnow;
maxSnow = temp->data.getAmtSnowPerMonth();
minSnow = maxSnow;
maxMonth = minMonth = temp->data.getMonth();
while (temp)
{
if (temp->data.getAmtSnowPerMonth() < minSnow)
{
minSnow = temp->data.getAmtSnowPerMonth();
minMonth = temp->data.getMonth();
}
if (temp->data.getAmtSnowPerMonth() > maxSnow)
{
maxSnow = temp->data.getAmtSnowPerMonth();
maxMonth = temp->data.getMonth();
}
temp = temp->next;
}
cout << "Maximum snow :" << maxSnow << " in the month: " << maxMonth << " ";
cout << "Minimum snow :" << minSnow << " in the month: " << minMonth << " ";
temp = ll_stats.start;
int maxSun, minSun;
maxSun = temp->data.getSunnyDaysPerMonth();
minSun = maxSun;
maxMonth = minMonth = temp->data.getMonth();
while (temp)
{
if (temp->data.getSunnyDaysPerMonth() < minSun)
{
minSun = temp->data.getSunnyDaysPerMonth();
minMonth = temp->data.getMonth();
}
if (temp->data.getSunnyDaysPerMonth() > maxSun)
{
maxSun = temp->data.getSunnyDaysPerMonth();
maxMonth = temp->data.getMonth();
}
temp = temp->next;
}
cout << "Maximum sunnydays :" << maxSun << " in month: " << maxMonth << " ";
cout << "Minimum sunnydays :" << minSun << " in month: " << minMonth << " ";
}
int main()
{
int n;
string month;
double amtR_Month;
double amtS_Month;
int amtSun_Month;
LnkdList<WeatherStats> ll_stats = LnkdList<WeatherStats>();
cout << "For how many months you need to enter the data? ";
cin >> n;
cout << " ";
for (int i = 0; i<n; i++)
{
WeatherStats temp = WeatherStats();
cout << "Month " << (i + 1) << "'s data ";
cout << " Enter Month Name: ";
cin >> month;
cout << " Enter amount of rain for " << month << " :";
cin >> amtR_Month;
cout << " Enter amount of snow for " << month << " :";
cin >> amtS_Month;
cout << " Enter amount of sunnyDays for " << month << " :";
cin >> amtSun_Month;
temp.setMonth(month);
temp.setAmtRainPerMonth(amtR_Month);
temp.setAmtSnowPerMonth(amtS_Month);
temp.setSunnyDaysPerMonth(amtSun_Month);
ll_stats.Add(temp);
cout << " ";
}
print(ll_stats);
calculate(ll_stats);
system("pause");
return 0;
}
Sample Output
For how many months you need to enter the data? 2
Month 1's data
Enter Month Name: Jan
Enter amount of rain for Jan :12
Enter amount of snow for Jan :13
Enter amount of sunnyDays for Jan :4
Month 2's data
Enter Month Name: Feb
Enter amount of rain for Feb :22
Enter amount of snow for Feb :11
Enter amount of sunnyDays for Feb :3
Stats:
Month Amount Of Rain Amount Of Snow SunnyDays
Jan 12 13 4
Feb 22 11 3
Calculations:
Maximum rain :22 in month: Feb
Minimum rain :12 in month: Jan
Maximum snow :13 in the month: Jan
Minimum snow :11 in the month: Feb
Maximum sunnydays :4 in month: Jan
Minimum sunnydays :3 in month: Feb
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.