Node * Copy(Node * first) // postcondition: returns a copy of the list pointed to by first { if (first == 0) return 0; else return new Node(first->info, Copy(first->next)); } Node * Copy(Node * first) // postcondition: returns a copy of the list pointed to by first { if (first != 0) { Node * anchor = new Node(first->info); // new first (fencepost) Node * tail = anchor; first = first->next; // first one done // invariant: tail-> last node created in new list while (first) { tail->next = new Node(first->info); // link new node in tail = tail->next; // advance both ptrs first = first->next; } return anchor; } } void MoveToFront(Node * list, const string & word) // precondition: list has a dummy/header node // postcondition: first occurrence of word in list has been moved // to the front of list as the first node { // use "look ahead method" Node * front = list; // remember front while (list->next) { if (list->next->info == word) { Node * toFront = list->next; // move this one list->next = toFront->next; // now it's unlinked toFront->next = front->next; // points to first non-header front->next = toFront; // header points to it return; } list = list->next; } } template void ReverseStack(const Stack & s, Stack & copy) // precondition: s is a1, a2, ..., an (a1 on top of stack) // postcondition: copy is an, ..., a2, a1 (an on top of stack) { Stack temp(s); // copy of s to pop copy.MakeEmpty(); // make sure copy is empty // loop until temp is empty, popping all elements onto copy while (! temp.IsDone()) { copy.Push(temp.Top()); temp.Pop(); } } template Stack & Stack::operator = (const Stack & rhs) { Stack temp; // temporary stack MakeEmpty(); // make myself empty // copy elements from rhs onto temp (reversed), then back to myself // so that I'm a copy of rhs [note: use ReverseStack] ReverseStack(rhs,temp); ReverseStasck(temp,*this); return *this; } int Length(const Sequence & s) // postcondition: returns number of elements in s { SequenceIterator iter(s); int count = 0; for(iter.First(); ! iter.IsDone(); iter.Next()) { count++; } return count; } bool operator < (const Sequence & lhs, const Sequence & rhs) // postcondition: returns true if lhs < rhs, otherwise returns false { Sequenceiterator left(lhs); Sequenceiterator right(rhs); if (Length(lhs) != Length(rhs)) return false; left.First(); right.First(); while (! right.IsDone()) { if (left.Current() >= right.Current()) return false; left.Next(); right.Next(); } return true; } // alternative: use comma operator and for-loop for(left.First(), right.First(); ! left.IsDone(); left.Next(),right.Next()) bool operator > (const Sequence & lhs, const Sequence & rhs) // postcondition: returns true if lhs > rhs, otherwise returns false { return rhs < lhs; } void Partition(Node * list, Node * & first, Node * & second) // precondition: list->info == X // postcondition: all nodes <= X in first, nodes > X in second { first = second = 0; // be safe, initialize if (list == 0) return; // nothing to do string pivot = list->info; // pivot on first element // invariant: first = front of list of nodes <= x // second = front of list of nodes > x // (each node is added to FRONT of first,second while (list != 0) { Node * hold = list->next; // next one to process if (list->info <= pivot) { list->next = first; // new front node in first first = list; } else { list->next = second; // new front node in second second = list; } list = hold; } }