#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. #include class dll { public: typedef char item_type; class link { public: item_type itm; link * prev; // pointers to other links link * next; }; 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. void insert(link * pos, const item_type & i) { link * n = new link; n->itm = i; // Change new link's pointers. n->prev = pos->prev; n->next = pos; // Change the surrounding links' pointers. n->next->prev = n; n->prev->next = n; return; } // Delete the specified item, which may not be the sentinel. link * erase(link * n) { link * 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(); } link * begin(void) const { return sentinel->next; } link * end(void) const { return 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_