// 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 LList template class // First, get the declaration for the base list class #include "list.h" // This is the declaration for LList. It is split into two parts // because it is too big to fit on one book page // Linked list implementation template class LList: public List { private: Link* head; // Pointer to list header Link* tail; // Pointer to last element Link* curr; // Access to current element int cnt; // Size of list void init() { // Intialization helper method curr = tail = head = new Link; 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 void print() const; // Print list contents void clear() { removeall(); init(); } // Clear list // Insert "it" at current position void insert(const E& it) { curr->next = new Link(it, curr->next); if (tail == curr) tail = curr->next; // New tail cnt++; } void append(const E& it) { // Append "it" to list tail = tail->next = new Link(it, NULL); cnt++; } // Remove and return current element E remove() { Assert(curr->next != NULL, "No element"); E it = curr->next->element; // Remember value Link* ltemp = curr->next; // Remember link node if (tail == curr->next) tail = curr; // Reset tail curr->next = curr->next->next; // Remove from list delete ltemp; // Reclaim space cnt--; // Decrement the count return it; } void moveToStart() // Place curr at list start { curr = head; } void moveToEnd() // Place curr at list end { curr = tail; } // Move curr one step left; no change if already at front void prev() { if (curr == head) return; // No previous element Link* temp = head; // March down list until we find the previous element while (temp->next!=curr) temp=temp->next; curr = temp; } // Move curr one step right; no change if already at end void next() { if (curr != tail) curr = curr->next; } int length() const { return cnt; } // Return length // 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 Assert(curr->next != NULL, "No value"); return curr->next->element; } };