#ifndef DLL_H_ #define DLL_H_ 1 // Oldham, Jeffrey D. // 2000Mar13 // CS1321 // 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. // Revisions: // JDO 2000Mar25: Changed insert() return to link *. #include // a doubly-linked list class template class dll { private: class link { public: item_type itm; link * prev; // pointers to other links link * next; }; public: class dll_iterator { public: friend class dll; typedef item_type* pointer; typedef item_type& reference; dll_iterator(void) { curPos = 0; return; } dll_iterator(link * l) { curPos = l; return; } // forward and backward traversal dll_iterator& operator++(void) /* prefix */ { curPos = curPos->next; return *this; } dll_iterator operator++(int) /* postfix */ { dll_iterator answer = *this; curPos = curPos->next; return answer; } dll_iterator& operator--(void) /* prefix */ { curPos = curPos->prev; return *this; } dll_iterator operator--(int) /* postfix */ { dll_iterator answer = *this; curPos = curPos->prev; return answer; } // member access reference operator*(void) const { return (*curPos).itm; } pointer operator->(void) const { return &(operator*()); } // equality operators friend bool operator==(const dll_iterator &d1, const dll_iterator &d2) { return d1.curPos == d2.curPos; } friend bool operator!=(const dll_iterator &d1, const dll_iterator &d2) { return !(d1 == d2); } private: link * curPos;// position with the linked list }; typedef dll_iterator iterator; 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 iterator. iterator insert(const iterator & pos, const item_type & i) { link * n = new link; n->itm = i; // Change new link's pointers. n->prev = pos.curPos->prev; n->next = pos.curPos; // 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. iterator erase(iterator pos) { link * n = pos.curPos; iterator answer = ++pos; // 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(); } 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_