// From the software distribution accompanying the textbook // "A Practical Introduction to Data Structures and Algorithm Analysis, // Third Edition (C++)" by Clifford A. Shaffer. // Source code Copyright (C) 2007-2011 by Clifford A. Shaffer. // This is the file to include in your code if you want access to the // complete DList template class // First, get the declaration for the base list class #include "list.h" // This is the declaration for DList. It is broken up because the // methods that appear in the book are in a separate file. // Linked list implementation template class LList: public List { private: Link* head; // Pointer to list header Link* tail; // Pointer to list tailer Link* curr; // Pointer ahead of current element int cnt; // Size of list void init() { // Intialization helper method curr = head = new Link; head->next = tail = new Link(head, NULL); cnt = 0; } void removeall() { // Return link nodes to free store while(head != NULL) { curr = head; head = head->next; delete curr; } } public: LList(int size=defaultSize) { init(); } // Constructor ~LList() { removeall(); } // Destructor // Destructor void clear() { removeall(); init(); } // Clear list void moveToStart() // Place curr at list start { curr = head; } void moveToEnd() // Place curr at list end { curr = tail->prev; } // Move fence one step right; no change if right is empty void next() { if (curr != tail->prev) curr = curr->next; } int length() const { return cnt; } // Return the position of the current element int currPos() const { Link* temp = head; int i; for (i=0; curr != temp; i++) temp = temp->next; return i; } // Move down list to "pos" position void moveToPos(int pos) { Assert ((pos>=0)&&(pos<=cnt), "Position out of range"); curr = head; for(int i=0; inext; } const E& getValue() const { // Return current element if(curr->next == tail) return NULL; return curr->next->element; } // Include those methods that are different from singly linked list // Insert "it" at current position void insert(const E& it) { curr->next = curr->next->prev = new Link(it, curr, curr->next); cnt++; } // Append "it" to the end of the list. void append(const E& it) { tail->prev = tail->prev->next = new Link(it, tail->prev, tail); cnt++; } // Remove and return current element E remove() { if (curr->next == tail) // Nothing to remove return NULL; E it = curr->next->element; // Remember value Link* ltemp = curr->next; // Remember link node curr->next->next->prev = curr; curr->next = curr->next->next; // Remove from list delete ltemp; // Reclaim space cnt--; // Decrement cnt return it; } // Move fence one step left; no change if left is empty void prev() { if (curr != head) // Can't back up from list head curr = curr->prev; } };