#ifndef LAB4ALIST_H #define LAB4ALIST_H #include #include "lab4list.h" using namespace std; // What type our list is holding. Most elegantly this would be // done using templates, but for the purposes of this exercise that // is slightly overkill and just tends to create such nasty error // messages that I won't subject you all to that. typedef int val_t; const int DEFAULT_SIZE = 32; // Predeclare that we will be defining a class ADTList class ArrayList; // We will also be defining ArrayListIterator class ArrayListIterator; // We will also want to implement a stream-output operator ostream& operator << (ostream& outStream, ArrayList& toPrint); // The interface for the Abstract Data Type List. class ArrayList : public ADTList { friend class ArrayListIterator; friend ostream& operator << (ostream& outStream, ArrayList& toPrint); public: // Default constructor ArrayList(); // Destructor ~ArrayList(); // Insert val in position pos. Pos = 0 implies "insert at head", // pos = size() implies "insert at tail" void insert(val_t val, unsigned int pos); // Remove the element in position pos void remove(unsigned int pos); // Return the element in position pos val_t retrieve(unsigned int pos); // Is there anything in this list? bool isEmpty(); // How much is there in this list? unsigned int size(); // Iterator stuff ArrayListIterator begin(); ArrayListIterator end(); private: unsigned int used; unsigned int allocated; val_t* values; }; // The iterator helper for an array list. Arrays don't really need // complicated iterators, but this helps us maintain encapsulation. class ArrayListIterator { // Only ArrayList can edit our private members friend class ArrayList; public: // Default constructor ArrayListIterator(); // Copy constructor ArrayListIterator(const ArrayListIterator& other); // Comparison operators bool const operator == (ArrayListIterator& other); bool operator != (const ArrayListIterator& other); // Dereference operators val_t operator *(); // Iterator manipulators ArrayListIterator& operator ++ (); ArrayListIterator& operator -- (); private: // If we do this as a pointer then we don't have to worry about the // list resizing after the iterator has been created. unsigned int* max; // The position we are currently pointing at unsigned int pos; // The array list we are attached to ArrayList* alist; }; #endif // LAB4ALIST_H