#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
const int MAX_LIST_SIZE = 50;
typedef double* ArrayPtr;
class List
{
public:
List();
// Initializes the List object to an empty list with size MAX_LIST_SIZE
List(int maxSize);
// Initializes the List object to be a list with at most
// maxSize entries.
void add_item(double number);
// Precondition: number has a value
// Postcondition: The argument number has been added to
// the list. If the list was full before adding, its maximum
// size is double what it was before adding.
bool full() const;
// Returns true if the list if full; false otherwise.
friend ostream& operator <<(ostream& outs, const List& theObject);
// Overloads the << operator so it can be used to output
// values of type List.
// Precondition: If outs is a file stream then outs has
// already been connected to a file.
int get_size() const;
// Returns the number of values in the list.
int get_max() const;
// Returns the maximum number of entries in the list.
double get_last ();
// Precondition: The List object is defined and non-empty.
// Returns the last value in the list.
void delete_last();
// Precondition: The List object is defined.
// Postcondition: If the list was nonempty, the last item has
// been deleted from the list array and size has been decremented;
// otherwise the list remains empty. If deletion of the item
// caused the size of the list to drop below half the maximum,
// then the maximum is reduced to half the original and the
// amount of space used to store the array is half what it was.
private:
ArrayPtr list;
int max; // maximum number of elements
int size; // number of array positions filled
};
int main()
{
//
// Variable declarations
//
double num; // a number to be added to the list
char ans; // loop control
int size;
cout << endl << "Testing the List Class" << endl << endl;
//
// Get the maximum number of entries and create a List object
//
cout << "What is the maximum number of entries in the list? ";
cin >> size;
cout << endl;
List testList(size);
//
// Let the user test add_item, delete_last, and get_last as long
// as he/she wishes
//
do
{
cout << endl;
cout << "Enter a to add to the list, d to delete the last entry, " << endl;
cout << "p to print the last item (without deleting), or s to stop: ";
cin >> ans;
if (ans == 'a' || ans == 'A')
{
//
// Get the number to be added, add it, then output the list
//
cout << "Enter a number to add to the list: ";
cin >> num;
testList.add_item(num);
}
else if (ans == 'd' || ans == 'D')
testList.delete_last();
else if (ans == 'p' || ans == 'P')
cout << endl << "The last item in the list is "
<< testList.get_last() << endl;
else if (ans != 's' && ans != 'S')
cout << "Not a valid response - try again!" << endl;
cout << endl << "Current List" << endl;
cout << "Capacity: " << testList.get_max() << " Size: "
<< testList.get_size() << endl;
cout << "List entries: " << testList << endl;
}
while (ans != 's' && ans != 'S');
return 0;
}
List::List()
{
size = 0;
max = MAX_LIST_SIZE;
}
List::List (int maxSize)
{
max = maxSize;
size = 0;
list = new double[maxSize];
}
void add_item(double number)
{
//???????????????
}
double get_last()
{
//??????????????
}
void delete_last()
{
//??????????????
}
bool List::full() const
{
return (size == max);
}
ostream& operator <<(ostream& outs, const List& theObject)
{
for (int i = 0; i < theObject.size; i++)
outs << theObject.list[i] << " ";
return outs;
}
int List::get_size() const
{
return size;
}
int List::get_max() const
{
return max;
}