In: Computer Science
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works.
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.
ASSIGNMENT 1:::
=============
#include <iostream>
using namespace std;
template <class DataType>
struct Node
{
DataType data;
Node<DataType> *next;
};
template <class DataType>
class LinkedList
{
public:
LinkedList();
LinkedList (const LinkedList<DataType> & applist);
~LinkedList();
LinkedList<DataType> & operator = (const
LinkedList<DataType> & rmlist);
void insert (const DataType & element);
bool first (DataType & listEll);
inline bool getNext (DataType & listEll);
int find (const DataType & element);
bool retrieve (DataType & element);
bool remove (DataType & element);
bool isEmpty() const;
void makeEmpty();
void print();
private:
Node<DataType> *start;
Node<DataType> *end;
Node<DataType> *current;
inline void deepCopy (const LinkedList<DataType> &
real);
};
template <class DataType>
void LinkedList<DataType>::print()
{
Node<DataType>* curr = start;
cout<<"START --> ";
while (curr)
{
cout<<curr->data<<" --> ";
curr = curr->next;
}
cout<<"NULL"<<"\n\n";
}
template <class DataType>
LinkedList<DataType>::LinkedList()
: start(0), current(0) , end(0){}
template <class DataType>
LinkedList<DataType>::LinkedList (const
LinkedList<DataType> & applist)
{
deepCopy( applist );
}
template <class DataType>
LinkedList<DataType>::~LinkedList()
{
makeEmpty( );
}
template <class DataType>
LinkedList<DataType> &
LinkedList<DataType>::operator = (const
LinkedList<DataType> & rmlist)
{
if (this == &rlist)
return *this;
makeEmpty();
deepCopy (rmlist);
return *this;
}
template <class DataType>
void LinkedList<DataType>::insert(const DataType&
parameter)
{
current = 0;
Node<DataType>* node = new Node<DataType>;
node->data = parameter;
if (!start)
{
start = node;
end = node;
return;
}
end->next = node;
end=end->next;
end->next = 0;
}
template <class DataType>
bool LinkedList<DataType>::first(DataType&
parameter)
{
if (!start) return false;
parameter = start->data;
current = start;
return true;
}
template <class DataType>
bool LinkedList<DataType>::getNext(DataType&
parameter)
{
if (!current) return false;
current = current->next;
if (!current) return false;
parameter = current->data;
return true;
}
template <class DataType>
int LinkedList<DataType>::find(const DataType&
parameter)
{
int position = -1;
DataType temp;
if (first(temp)) do
{
++position;
if (parameter == temp)
return position;
} while (getNext(temp));
return position;
}
template <class DataType>
bool LinkedList<DataType>::retrieve(DataType&
parameter)
{
if (find(parameter) == -1)
return false;
parameter = current->data;
return true;
}
template <class DataType>
bool LinkedList<DataType>::remove(DataType&
parameter)
{
Node<DataType>* p;
Node<DataType>* pre;
for (pre = 0, p = start; p; pre = p, p = p->next)
if (p->data == parameter)
break;
if (!p) return false;
if (pre) pre->next = p->next; else start = p->next;
if (p == current) current = current->next;
parameter = p->data;
delete p;
return true;
}
template <class DataType>
bool LinkedList<DataType>::isEmpty() const
{
return start == 0;
}
template <class DataType>
void LinkedList<DataType>::makeEmpty()
{
while (start)
{
current = start->next;
delete start;
start = current;
} }
template <class DataType>
inline void LinkedList<DataType>::deepCopy( const
LinkedList<DataType> & real )
{
start = current = NULL;
if ( original.start == NULL )
return;
Node<DataType> *copyptr = start = new
Node<DataType>;
Node<DataType> *realptr = real.start;
copyptr->data = realptr->data;
if ( realptr == real.current )
current = copyptr;
while (realptr->next != NULL ) {
realptr = originalptr->next;
copyptr->next = new Node<DataType>;
copyptr = copyptr->next;
copyptr->data = realptr->data;
if ( realptr == real.current )
current = copyptr;
}
copyptr->next = NULL;
}
int main()
{
int remove, n;
cout<<"First linked list of INTEGERS: \n\n";
LinkedList<int> ll_Int = LinkedList<int>();
ll_Int.insert(1);
ll_Int.insert(2);
ll_Int.insert(3);
ll_Int.insert(4);
ll_Int.insert(5);
remove=3;
ll_Int.remove(remove);
ll_Int.print();
n = 2;
cout<<"\nPosition of ("<<n<<") in the array
:"<<(ll_Int.find(n))<<"\n\n";
//creating second linked list of datatype char
char r, n1;
cout<<"\n\nSecond linked list of CHARACTERS: \n\n";
LinkedList<char> ll_Char = LinkedList<char>();
ll_Char.insert('a');
ll_Char.insert('b');
ll_Char.insert('c');
ll_Char.insert('d');
ll_Char.insert('e');
r='c';
ll_Char.remove(r);
ll_Char.print();
n1 = 'a';
cout<<"\nPosition of ("<<n1<<") in the array
:"<<(ll_Char.find(n1))<<"\n\n";
return 0;
}
ASSIGNMENT 2
#include <iostream>
#include<string>
#include <iomanip>
using namespace std;
template <class DataType>
struct Node
{
DataType data;
Node<DataType> *next;
};
template <class DataType>
class LinkedList
{
public:
Node<DataType> *start;
LinkedList();
LinkedList (const LinkedList<DataType> & applist);
~LinkedList();
LinkedList<DataType> & operator = (const
LinkedList<DataType> & rmlist);
void insert (const DataType & element);
bool first (DataType & listEll);
inline bool getNext (DataType & listEll);
int find (const DataType & element);
bool retrieve (DataType & element);
bool remove (DataType & element);
bool isEmpty() const;
void makeEmpty();
void print();
private:
Node<DataType> *end;
Node<DataType> *current;
inline void deepCopy (const LinkedList<DataType> &
real);
};
template <class DataType>
void LinkedList<DataType>::print()
{
Node<DataType>* curr = start;
cout<<"START --> ";
while (curr)
{
cout<<curr->data<<" --> ";
curr = curr->next;
}
cout<<"NULL"<<"\n\n";
}
template <class DataType>
LinkedList<DataType>::LinkedList()
: start(0), current(0) , end(0){}
template <class DataType>
LinkedList<DataType>::LinkedList (const
LinkedList<DataType> & applist)
{
deepCopy( applist );
}
template <class DataType>
LinkedList<DataType>::~LinkedList()
{
makeEmpty( );
}
template <class DataType>
LinkedList<DataType> &
LinkedList<DataType>::operator = (const
LinkedList<DataType> & rmlist)
{
if (this == &rmlist)
return *this;
makeEmpty();
deepCopy (rmlist);
return *this;
}
template <class DataType>
void LinkedList<DataType>::insert(const DataType&
parameter)
{
current = 0;
Node<DataType>* node = new Node<DataType>;
node->data = parameter;
if (!start)
{
start = node;
end = node;
end->next = 0;
return;
}
end->next = node;
end=end->next;
end->next = 0;
}
template <class DataType>
bool LinkedList<DataType>::first(DataType&
parameter)
{
if (!start) return false;
parameter = start->data;
current = start;
return true;
}
template <class DataType>
bool LinkedList<DataType>::getNext(DataType&
parameter)
{
if (!current) return false;
current = current->next;
if (!current) return false;
parameter = current->data;
return true;
}
template <class DataType>
int LinkedList<DataType>::find(const DataType&
parameter)
{
int position = -1;
DataType temp;
if (first(temp)) do
{
++position;
if (parameter == temp)
return position;
} while (getNext(temp));
return position;
}
template <class DataType>
bool LinkedList<DataType>::retrieve(DataType&
parameter)
{
if (find(parameter) == -1)
return false;
parameter = current->data;
return true;
}
template <class DataType>
bool LinkedList<DataType>::remove(DataType&
parameter)
{
Node<DataType>* p;
Node<DataType>* pre;
for (pre = 0, p = start; p; pre = p, p = p->next)
if (p->data == parameter)
break;
if (!p) return false;
if (pre) pre->next = p->next; else start = p->next;
if (p == current) current = current->next;
parameter = p->data;
delete p;
return true;
}
template <class DataType>
bool LinkedList<DataType>::isEmpty() const
{
return start == 0;
}
template <class DataType>
void LinkedList<DataType>::makeEmpty()
{
while (start != end)
{
current = start->next;
delete start;
start = current;
}
delete start;
}
template <class DataType>
inline void LinkedList<DataType>::deepCopy( const
LinkedList<DataType> & real )
{
start = current = NULL;
if ( real.start == NULL )
return;
Node<DataType> *copyptr = start = new
Node<DataType>;
Node<DataType> *realptr = real.start;
copyptr->data = realptr->data;
if ( realptr == real.current )
current = copyptr;
while ( realptr->next != NULL ) {
realptr = realptr->next;
copyptr->next = new Node<DataType>;
copyptr = copyptr->next;
copyptr->data = realptr->data;
if ( realptr == real.current )
current = copyptr;
}
copyptr->next = NULL;
}
class WeatherStats
{
private:
string month;
double amntRainPerMonth;
double amntSnowPerMonth;
int sunnyDayPerMonth;
public:
WeatherStats()
{
amntRainPerMonth = 0;
amntSnowPerMonth = 0;
sunnyDayPerMonth = 0;
month = "";
}
string getMonth()
{
return month;
}
double getAmntRainPerMonth()
{
return amntRainPerMonth;
}
double getAmntSnowPerMonth()
{
return amntSnowPerMonth;
}
double getSunnyDayPerMonth()
{
return sunnyDayPerMonth;
}
void setMonth(string d)
{
month = d;
}
void setAmntRainPerMonth(double d)
{
amntRainPerMonth = d;
}
void setAmntSnowPerMonth(double d)
{
amntSnowPerMonth = d;
}
void setSunnyDayPerMonth(int d)
{
sunnyDayPerMonth = d;
}
};
void print(LinkedList<WeatherStats> &ll_stats)
{
Node<WeatherStats> *temp = ll_stats.start;
cout<<"\nStats: \n\n";
cout<<setw(20)<<"Month"<<setw(15)<<" Amount
Of Rain"<<setw(15)<<" Amount Of
Snow"<<setw(15)<<" SunnyDays\n\n";
while (temp)
{
cout<<setw(20)<<temp->data.getMonth()<<setw(15)<<temp->data.getAmntRainPerMonth()<<setw(15)<<temp->data.getAmntSnowPerMonth()<<setw(15)<<temp->data.getSunnyDayPerMonth()<<"\n";
temp=temp->next;
}
}
void calculate(LinkedList<WeatherStats>
&ll_stats)
{
Node<WeatherStats> *temp = ll_stats.start;
cout<<"\n\nCalculations: \n\n";
string maxMonth, minMonth;
double maxRain, minRain;
maxRain = temp->data.getAmntRainPerMonth();
minRain = maxRain;
maxMonth = minMonth = temp->data.getMonth();
while (temp)
{
if(temp->data.getAmntRainPerMonth() < minRain)
{
minRain = temp->data.getAmntRainPerMonth();
minMonth = temp->data.getMonth();
}
if(temp->data.getAmntRainPerMonth() > maxRain)
{
maxRain = temp->data.getAmntRainPerMonth();
maxMonth = temp->data.getMonth();
}
temp=temp->next;
}
cout<<"Maximum rain :"<<maxRain<<" in month:
"<<maxMonth<<"\n";
cout<<"Minimum rain :"<<minRain<<" in month:
"<<minMonth<<"\n";
//////////////////////
temp = ll_stats.start;
double maxSnow, minSnow;
maxSnow = temp->data.getAmntSnowPerMonth();
minSnow = maxSnow;
maxMonth = minMonth = temp->data.getMonth();
while (temp)
{
if(temp->data.getAmntSnowPerMonth() < minSnow)
{
minSnow = temp->data.getAmntSnowPerMonth();
minMonth = temp->data.getMonth();
}
if(temp->data.getAmntSnowPerMonth() > maxSnow)
{
maxSnow = temp->data.getAmntSnowPerMonth();
maxMonth = temp->data.getMonth();
}
temp=temp->next;
}
cout<<"Maximum snow :"<<maxSnow<<" in month:
"<<maxMonth<<"\n";
cout<<"Minimum snow :"<<minSnow<<" in month:
"<<minMonth<<"\n";
//////////////////////
temp = ll_stats.start;
int maxSunny, minSunny;
maxSunny = temp->data.getSunnyDayPerMonth();
minSunny = maxSunny;
maxMonth = minMonth = temp->data.getMonth();
while (temp)
{
if(temp->data.getSunnyDayPerMonth() < minSunny)
{
minSunny = temp->data.getSunnyDayPerMonth();
minMonth = temp->data.getMonth();
}
if(temp->data.getSunnyDayPerMonth() > maxSunny)
{
maxSunny = temp->data.getSunnyDayPerMonth();
maxMonth = temp->data.getMonth();
}
temp=temp->next;
}
cout<<"Maximum sunnydays :"<<maxSunny<<" in
month: "<<maxMonth<<"\n";
cout<<"Minimum sunnydays :"<<minSunny<<" in
month: "<<minMonth<<"\n";
}
int main()
{
int n;
string month;
double amntRainPerMonth;
double amntSnowPerMonth;
int sunnyDayPerMonth;
LinkedList<WeatherStats> ll_stats = LinkedList<WeatherStats>();
cout<<"How many month's data do you want to enter:
";
cin>>n;
cout<<"\n\n";
for(int i=0; i<n; i++)
{
WeatherStats temp = WeatherStats();
cout<<"Enter "<<(i+1)<<" data: \n";
cout<<"\nEnter Month: ";
cin>>month;
cout<<"\nEnter amount of rain: ";
cin>>amtRainPerMonth;
cout<<"\nEnter amount of snow: ";
cin>>amtSnowPerMonth;
cout<<"\nEnter amount of sunnyDays: ";
cin>>sunnyDaysPerMonth;
temp.setMonth(month);
temp.setAmntRainPerMonth(amtRainPerMonth);
temp.setAmntSnowPerMonth(amtSnowPerMonth);
temp.setSunnyDayPerMonth(sunnyDaysPerMonth);
ll_stats.insert(temp);
cout<<"\n\n";
}
print(ll_stats);
calculate(ll_stats);
return 0;
}