#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; comments // with "****" indicate changes. #include #include // has NULL #include // has assert() template // **** added to "templatize" class class dll { public: class iterator; // **** "forward" declaration (needed // so "link" class knows "iterator" // is also a class). private: // **** nested "link" class private class link { private: friend class iterator; // iterator, dll classes need access to friend class dll; // link class private parts 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 = NULL; } iterator(link * p) { linkPtr = p; } // Forward and backward traversal. // Prefix increment (++p). // Pre: linkPtr does not point to past-end. // Post: linkPtr points to next link. // returns updated linkPtr. iterator& operator++(void) { linkPtr = linkPtr->next; return *this; } // Postfix increment (p++). // Pre: linkPtr does not point to past-end. // Post: linkPtr points to next link. // returns old linkPtr. // Note the unused "int" parameter -- this is how the compiler // distinguishes between the prefix and postfix functions. iterator operator++(int) { iterator answer = *this; linkPtr = linkPtr->next; return answer; } // Prefix decrement (--p). // Pre: linkPtr does not point to first link. // Post: linkPtr points to previous link. // returns updated linkPtr. iterator& operator--(void) { linkPtr = linkPtr->prev; return *this; } // Postfix decrement (p--). // Pre: linkPtr does not point to first link. // Post: linkPtr points to previous link. // returns old linkPtr. // Note the unused "int" parameter -- this is how the compiler // distinguishes between the prefix and postfix functions. iterator operator--(int) { iterator answer = *this; linkPtr = linkPtr->prev; return answer; } // Member access. // Pre: linkPtr points to a link. // Post: returns reference to link's item. item_type & operator*(void) const { return linkPtr->itm; } // Pre: linkPtr points to link. // Post: returns pointer to link's item. 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 // ---- Main dll 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; } // Destructor. ~dll(void) { // Delete all the links. destroy(); } // Insert the item before the link. // Post: a link with value "i" has been added before link *pos. // returns pointer to newly-added 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 link. // Pre: *pos is not the sentinel link. // Post: link *pos has been removed from the list. // returns pointer to link following *pos. // **** 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; } // Get start of list (analogous to STL classes' begin()). // Post: returns pointer to first link in list, or to sentinel if // list is empty. // **** function now returns iterator, not link pointer, // and we provide two versions, one "const" and one not. iterator begin(void) { return iterator(sentinel->next); } const iterator begin(void) const { return iterator(sentinel->next); } // Get past-end of list (analogous to STL classes' end()). // Post: returns pointer to sentinel link. // **** function now returns iterator, not link pointer, // and we provide two versions, one "const" and one not. iterator end(void) { return iterator(sentinel); } const iterator end(void) const { return iterator(sentinel); } // **** pred(), succ() functions no longer needed. private: link * sentinel; // sentinel (dummy) link, always present // Create an empty list. // Pre: sentinel does not point to anything; no "link" objects are // allocated for this list. // Post: sentinel points to the "sentinel" node, which is the only // link in the list. void create(void) { sentinel = new link; // sentinel->itm need not be set sentinel->prev = sentinel; sentinel->next = sentinel; return; } // Destroy this list. // Pre: sentinel points to sentinel link for list. // Post: all allocated "link" objects have been freed. void destroy(void) { while (sentinel->next != sentinel) erase(sentinel->next); delete sentinel; } // Copy the given list into this one. // Pre: sentinel does not point to anything; no "link" objects are // allocated for this list. // Post: list is a copy of list "d". 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_