#ifndef NODE_LIST_H #define NODE_LIST_H #include "VariousExceptions.h" template class NodeList { public: /* * Default Constructor: creates an empty list * */ NodeList(); /* * Copy Constructor * */ NodeList(const NodeList& orig); /* * Overloaded Assignment Operator * */ NodeList& operator=(const NodeList& orig); /* * Destructor * */ ~NodeList(); protected: /******************************************************** * Class Node: this represents a single node of the list */ struct Node { // node in the NodeList Object element; // element Node* prev; // previous node Node* next; // next node Node(const Object& e = Object(), Node* p = NULL, Node* n = NULL) : element(e), prev(p), next(n) { } // constructor }; typedef Node* NodePtr; // pointer to a Node public: /******************************************************** * Class Position: represents a position in a NodeList */ class Position { public: /* * constructor */ Position(const NodeList *l = NULL, NodePtr n = NULL) : container(l), node(n) { } /* * Returns the element at the Position */ Object& element() const throw(InvalidPositionException) { if (node == NULL) throw InvalidPositionException("Null position"); return node->element; } /* * Determines whether the Position is a 'null' position */ bool isNull() const { return node == NULL; } /* * Overload the equality operator */ bool operator==(const Position& other) { return (this->node == other.node && this->container == other.container); } friend class NodeList; // allow access to private member protected: /** * utility to convert Position to node pointer */ NodePtr validate(const NodeList* list) const throw(InvalidPositionException); private: const NodeList *container; NodePtr node; }; /*********** end of class Position ***************************************/ public: /* * returns size of list */ int size() const; /* * is the list empty? */ bool isEmpty() const; /* * return position of first element in list */ Position first() const throw(EmptyContainerException); /* * return position of last element in list */ Position last() const throw(EmptyContainerException); /* * is this the first position? */ bool isFirst(const Position& p) const throw(InvalidPositionException); /* * is this the last position? */ bool isLast(const Position& p) const throw(InvalidPositionException); /* * return position before the given position */ Position before(const Position& p) const throw(BoundaryViolationException, InvalidPositionException); /* * return position after the given position */ Position after(const Position& p) const throw(BoundaryViolationException, InvalidPositionException); /* * insert new element e into list as the first element */ Position insertFirst(const Object& element); /* * insert new element e into list as the last element */ Position insertLast(const Object& element); /* * insert new element before the given position */ Position insertBefore(const Position& p, const Object& element) throw(InvalidPositionException); /* * insert new element after the given position */ Position insertAfter(const Position& p, const Object& element) throw(InvalidPositionException); /* * remove the element at the given position */ void remove(const Position& p) throw(InvalidPositionException); /* * replace the element at the given position */ void replaceElement(const Position& p, const Object& element) throw(InvalidPositionException); /* * swap the elements stored at the given positions */ void swap(const Position& p, const Position& q) throw(InvalidPositionException); private: int sz; // number of items NodePtr header; // head of list sentinel NodePtr trailer; // tail of list sentinel /* * Utilities used for copy constructor, assignment operator, and destructor */ void cloneList(const NodeList& orig); void clearList(); }; #include "NodeList.tcc" #endif