struct Node { string info; Node * next; Node (const string & s, Node * ptr = 0) : info(s), next(ptr) { } }; int Count(Node * list, const string & key) // precondition: list has a header node // postcondition: returns # occurrences of key in list { int total = 0; list = list->next; // skip header while (list) { if (list->info == key) { total++; } list = list->next; } return total; } int Count(Node * list, const string & key) // precondition: list has a header node // postcondition: returns # occurrences of key in list { int total = 0; while (list->next != 0) // node after exists? { list = list->next; // move to next node if (list->info == key) { total++; } } return total; } // solution below is O(n^2) for n-node list, // makes exactly n^2 comparisons string Modal(Node * list) // precondition: list has header node // postcondition: returns the modal string in list { string modStr = ""; // modal string int modCount = 0; // # times modal string occurs Node * first = list; // first node (header) list = list->next; // skip header to look at info while (list) { int occ = Count(first,list->info); // # times info occurs if (occ > modCount) { modCount = occ; modStr = list->info; } list = list->next; } return modStr; } // solution below is also O(n^2) for an n node list // but makes (n-1)n/2 comparisons. Each real node // of list is treated as a header node in calling Count, // the +1 added in call is for list->info which is skipped // since the node list is treated as a header node by Count string Modal(Node * list) // precondition: list has header node // postcondition: returns the modal string in list { string modStr = ""; // modal string int modCount = 0; // # times modal string occurs int occ; list = list->next; // skip header to look at info while (list) { occ = Count(list,list->info) + 1; // # info occurs AFTER list if (occ > modCount) // add one for list->info { modCount = occ; modStr = list->info; } list = list->next; } return modStr; } void Compress(Node * list) // precondition: no header node, list is sorted // postcondition: all duplicates removed { while (list && list->next) // at least two nodes? { if (list->info == list->next->info) // remove list->next { Node * temp = list->next; // remove temp list->next = temp->next; // link around delete temp; } else { list = list->next; // not a dupe, goto next node } } } void Reverse(Stack & s, int n) // precondition: n <= s.Size(), s contains at least n elements // postcondition: top n elements of s are reversed { Queue q; int k; for(k=0; k < n; k++) { q.Enqueue(s.Top()); s.Pop(); } for(k=0; k < n; k++) { s.Push(q.GetFront()); s.Dequeue(); } } void Reverse(Stack & s, int n) // precondition: n <= s.Size(), s contains at least n elements // postcondition: top n elements of s are reversed { Vector v(n); // store n elements int k; for (k=0; k < n; k++) { s.Pop(v[k]); } for(k=0; k < n; k++) { s.Push(v[k]); } } void Reverse(Stack & s, int n) // precondition: n <= s.Size(), s contains at least n elements // postcondition: top n elements of s are reversed { Stack copy(s); int k; for(k=0; k < n; k++) { s.Pop(); } for(k=0; k < n; k++) { s.Push(copy.Top()); copy.Pop(); } } TNode * MakeTree(DNode * list) // precondition: list is NULL-terminated, NO header node // postcondition: returns a pointer to the root of a binary search // tree containing same values in list { if (list == 0) return 0; // take care of empty list DNode * mid = MiddleNode(list); DNode * before = mid->prev; // only really need this one DNode * after = mid->next; if (before) before->next = 0; // unlink so first half 0-terminated if (after) after->prev = 0; // not necessary! TNode * tree = new TNode(mid->info, // root is middle MakeTree(list), // left half, left subtree MakeTree(after)); // ditto for right half if (before) before->next = mid; // relink list back together if (after) after->prev = mid; return tree; }