#ifndef DLL_H_ #define DLL_H_ 1 // Oldham, Jeffrey D. // 2000Mar13 // CS1321 // Modified by: // Massingill, Berna L. // 2000 Apr 05 // Doubly-Linked List Implementation // This class implements a doubly-linked list. Each link is allocatd // from dynamic memory. The list is represented as a circular-linked // list with a sentinel link. // Templatized and modified to include an iterator class; // see **** comments. #include template // **** added to "templatize" class class dll { public: private: // **** nested link class now private class link { public: item_type itm; link * prev; // pointers to other links link * next; }; public: class iterator { // **** new nested class for iterator public: friend class dll; // dll class needs access to iterator // class private parts // constructors iterator(void) { linkPtr = 0; return; } iterator(link * p) { linkPtr = p; return; } // forward and backward traversal iterator& operator++(void) { // prefix (++p) linkPtr = linkPtr->next; return *this; } iterator operator++(int) { // postfix (p++) iterator answer = *this; linkPtr = linkPtr->next; return answer; } iterator& operator--(void) { // prefix (--p) linkPtr = linkPtr->prev; return *this; } iterator operator--(int) { // postfix (p--) iterator answer = *this; linkPtr = linkPtr->prev; return answer; } // member access item_type & operator*(void) const { return linkPtr->itm; } item_type * operator->(void) const { return &(operator*()); } // comparison operators friend bool operator==(const iterator & p1, const iterator & p2) { return p1.linkPtr == p2.linkPtr; } friend bool operator!=(const iterator & p1, const iterator & p2) { return !(p1 == p2); } private: link * linkPtr; // iterator contains pointer to link }; // end of nested iterator class // constructor dll(void) { create(); return; } // copy constructor dll(const dll & d) { copy(d); return; } // assignment operator dll& operator=(const dll & d) { if (this != &d) { destroy(); copy(d); } return *this; } // Insert the item before the link. // **** first parameter, return type now iterators, not link pointers. iterator insert(const iterator & pos, const item_type & i) { link * n = new link; n->itm = i; link * afterInsert = pos.linkPtr; // Change new link's pointers. n->prev = afterInsert->prev; n->next = afterInsert; // Change the surrounding links' pointers. n->next->prev = n; n->prev->next = n; return iterator(n); } // Delete the specified item, which may not be the sentinel. // **** parameter, return type now iterators, not link pointers. iterator erase(const iterator & pos) { link * n = pos.linkPtr; iterator answer(n->next); // Change the surrounding links' pointers. n->prev->next = n->next; n->next->prev = n->prev; // Change new link's pointers. (not actually necessary) n->prev = 0; n->next = 0; delete n; return answer; } ~dll(void) { // Delete all the links. destroy(); } // **** functions now return iterators, not link pointers // (and they should not be const, because a calling // program could use the iterator to modify the list) iterator begin(void) { return iterator(sentinel->next); } iterator end(void) { return iterator(sentinel); } private: link * sentinel; // holds onto the list // Create an empty list. void create(void) { sentinel = new link; // sentinel->itm need not be set sentinel->prev = sentinel; sentinel->next = sentinel; return; } // Destroy this list; void destroy(void) { while (sentinel->next != sentinel) erase(sentinel->next); delete sentinel; } // Copy the given list into this one. void copy(const dll & d) { create(); for (link * pos = d.sentinel->next; pos != d.sentinel; pos = pos->next) insert(sentinel, pos->itm); return; } }; #endif // DLL_H_