1.Using the UnsortedList class given in Week 5 Day 1 Example Programs, what is the output of the following program if we enter the 5 strings as:
sunny hello summer winter warm
#include < iostream>
#include < string>
#include "UnsortedList.h"
using namespace std;
int main()
{
UnsortedListstringList;
string str;
cout << "Please enter 5 strings: ";
for (int counter = 0; counter < 5; counter++)
{
cin >> str;
stringList.insert(str);
}
stringList.sort();
for (int counter = 0; counter < stringList.getLength();
counter++)
cout << stringList[counter] << endl;
return 0;
}
2.Using the UnsortedList class given in Week 5 Day 1 Example Programs, write a client function splitLists which divides the list into two lists according to the given item. The precondition of the function is: the list has been initialized and is not empty. The postconditions are: list1 contains all the items of the list whose values are less than or equal to the given item; list2 contains all the items of the list whose keys are greater than the given item. Implement the splitLists function as a client function (not a member function or a friend function of the UnsortedList class) whose declaration is:
template < class ItemType>
void splitLists(UnsortedList< ItemType>& list,
const ItemType& item,
UnsortedList< ItemType>& list1, UnsortedList< ItemType>& list2);
3.Using the SortedList (sorted array-based list) class given in Week 5 Day 3 Example Programs, write a client function mergeLists which merges two sorted lists into a third sorted list. The preconditions of the function are: list1 and list2 have been initialized and are sorted; list1 and list2 do not have any items in common. The postconditions are: the result is a sorted list that contains all of the items from list1 and list2.
(a)Write the prototype for mergeLists.
(b)Write the function definition for mergeLists, using the SortedList class.
4.Use the SortedList class (sorted array-based list) given in Week 5 Day 3 Example Programs. What is the output of the following program?
#include < iostream>
#include < string>
#include "SortedList.h"
#include "PersonType.h"
using namespace std;
int main()
{
SortedList< PersonType> personList;
PersonType person1("George", "Mason", 1234567);
personList.insert(person1);
PersonType person2("Albert", "Sayed", 1232089);
personList.insert(person2);
PersonType person3("Eugene", "Kobayoshi", 1237212);
personList.insert(person3);
PersonType person4("Jack", "Kobayoshi", 1237252);
personList.insert(person4);
PersonType person5("Takayama", "Isuzu", 1234522);
personList.insert(person5);
cout << "The input list is:n";
for (int i = 0; i < personList.getLength(); i++)
personList[i].print();
cout << endl;
personList.remove(person4);
cout << "After the delete operation, the list is:n";
for (int i = 0; i < personList.getLength(); i++)
personList[i].print();
return 0;
}
Note: In the above program, the PersonType.h is given as follows.
//**********************************************************
// SPECIFICATION FILE (PersonType.h)
// This file gives the specification of a PersonType class
//**********************************************************
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
#include < iostream>
#include < string>
#include < iomanip>
using namespace std;
class PersonType
{
public:
PersonType(string first = "None", string last = "None",
int a = 1230000)
{ firstName = first; lastName = last; id = a; }
void print()
{ cout << setw(15) << firstName << setw(15)
<< lastName << setw(10) << id << endl; }
bool operator <(const PersonType& right) const
{ return (lastName < right.lastName); }
bool operator >(const PersonType& right) const
{ return (lastName > right.lastName); }
private:
string firstName, lastName;
int id;
};
#endif
5.Consider the PersonType class in Problem 4. We now want to include a fourth private data member int key to indicate the key member (when key equals 1, firstName is the key; when key equals 2, lastName is the key; when key equals 3, id is the key). Modify your PersonType.h correspondingly according to this requirement (set the default value of the parameter corresponding to key in the constructor as int k = 2). Clearly show your updated PersonType.h file in your answer. Then show your outputs for the following program when you enter the key as
(a)1
(b)2
(c)3
#include < iostream>
#include < string>
#include "SortedList.h"
#include "PersonType.h"
using namespace std;
int main()
{
int key;
cout << "Enter the key (1 or 2 or 3): ";
cin >> key;
SortedList< PersonType> personList;
PersonType person1("George", "Mason", 1234567, key);
personList.insert(person1);
PersonType person2("Albert", "Sayed", 1232089, key);
personList.insert(person2);
PersonType person3("Eugene", "Kobayoshi", 1237212, key);
personList.insert(person3);
PersonType person4("Jack", "Kobayoshi", 1237252, key);
personList.insert(person4);
PersonType person5("Takayama", "Isuzu", 1234522, key);
personList.insert(person5);
cout << "The input list is:n";
for (int i = 0; i < personList.getLength(); i++)
personList[i].print();
cout << endl;
personList.remove(person4);
cout << "After the delete operation, the list is:n";
for (int i = 0; i < personList.getLength(); i++)
personList[i].print();
return 0;
}