void Prepend(Node * list, int value) // precondition: list points to last node of NON-empty circularly-linked list // (first node is list->next) // postcondition: new first node with info == value added to // the circularly-linked list whose last node is list { list->next = new Node(value,list->next); } void RemoveLast(Node * & list) // precondition: list points to last node of NON-empty circularly-linked list // (first node is list->next) // postcondition: last node is removed (returned to heap) and list // points to the new last node { Node * oldLast = list; if (list->next == list) // one-node list? { delete list; list = 0; } else { // advance list to new last node while (list->next != oldLast) { list = list->next; } list->next = oldLast->next; delete oldLast; } } bool DigitSeq::Contains(int num) const // postcondition: returns true if num is in sequence (*this) // returns false otherwise { DigitSeqIterator iter(*this); for(iter.First(); ! iter.IsDone(); iter.Next()) { if (iter.Current() == num) return true; } return false; } void DigitSeq::Insert(int num) // postcondition: if num is NOT contained in sequence/set then // num is inserted into the sequence/set { if (! Contains(num)) { Prepend(num); } } bool operator < (const DigitSeq & s, const DigitSeq & t) // postcondition: returns true if s is a subset of t, false otherwise { DigitSeqIterator iter(s); for(iter.First(); ! iter.IsDone(); iter.Next()) { if (! t.Contains(iter.Current())) return false; } return true; } bool operator == (const DigitSeq & s, const DigitSeq & t) { return s < t && t < s; } int NumStudents(DormNode * dorm) // precondition: dorm != 0 // postcondition: returns the number of students in dorm { StudentNode * list = dorm->students; int count = 0; while (list) { count++; list = list->next; } return count; } int NumDormStudents(DormNode * list, string dormName) // postcondition: returns the number of students in the dormitory // in list with name // dormName (and 0 if there is no such dormitory in list) { while (list != 0 && list->name != dormName) { list = list->next; } if (list == 0) return 0; else return NumStudents(list); } string PopularName(DormNode * dorm) // precondition: dorm != 0 // postcondition: returns the name that appears the most frequently // in the dorm; if there is a tie, return any of the // most frequently occuring names; if no students in dorm return "" { string maxName = ""; int maxCount = 0; int count; StudentNode * list = dorm->students; while (list != 0) { count = SameName(list); if (count > maxCount) { maxCount = count; maxName = list->name; } list = list->next; } return maxName; } void Shuffle(Node * & list) // precondition: list does not have a header node, // number of nodes in list is even. // list = a1, a2, a3, ... ,aN // postcondition: list = a1, a3, a5, ... a(N-1), a2, a4, ... aN // { if (list != 0) { Node * even = list->next; Node * odd = list; Node * evenTail = even; Node * oddTail = odd; while (evenTail->next != 0) { oddTail = oddTail->next = evenTail->next; evenTail = evenTail->next = oddTail->next; } oddTail->next = even; } }