// FILE: 98ab3.cpp // PURPOSE: Test implementation of solutions to 1998 AB Exam: Question 3 //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: All the files must be in directory in which you compile // 2. COMMAND LINE: g++ (or gxx) 98ab3.cpp //-------------------------------------------------------------------- #include #include struct ListNode { int info; ListNode * next; ListNode(int val, ListNode * ptr) // constructor : info(val), next(ptr) { } }; ListNode * FirstMin(ListNode * list) { // postcondition: returns pointer to node with minimal value in list // returns pointer to first such node if more than // one node contains the minimal value return list; } void RemoveNext(ListNode * ptr) { // precondition: ptr != NULL } void RemoveDupMins(ListNode * list) { // postcondition: all but first minimal nodes removed from list } void testFirstMin(void); void testRemoveNext(void); void testRemoveDupMins(void); void testRemoveNext(void); void initList(ListNode *&, int); void Print(ListNode *, const string &); void pause(void); void cls(void); int main() { string title("1998 AB Exam: Question 3"); cls(); cout << setw(39 + title.length()/2) << title.c_str() << "\n\n"; testFirstMin(); testRemoveNext(); pause(); cls(); testRemoveDupMins(); pause(); return 0; } ///////////////////////////////////////////// void testFirstMin(void) { ListNode *L1, *L2,*L4; initList(L1, 1); initList(L2, 2); initList(L4, 4); L4->next = NULL; cout << "Testing FirstMin:\n\n"; Print(L1, "L1"); ListNode *p = FirstMin(L1); cout << "After p = FirstMin(L1): p->info = " << p->info << "\n\n"; Print(L2, "L2"); p = FirstMin(L2); cout << "After p = FirstMin(L2): p->info = " << p->info << "\n\n"; Print(L4, "L3"); p = FirstMin(L4); cout << "After p = FirstMin(L3): p->info = " << p->info << endl; } void testRemoveNext(void) { ListNode * L1 = new ListNode(10, new ListNode(5,new ListNode(20,NULL))); ListNode * L2 = new ListNode(10, new ListNode(5,new ListNode(20,NULL))); cout << string(60, '*') << endl; cout << "Testing RemoveNext:\n\n"; ListNode *ptr = L1; cout << "ptr->info = " << ptr->info << " and "; Print(L1, "L1"); RemoveNext(ptr); cout << "After RemoveNext(ptr): "; Print(L1, "L1"); ptr = L2->next->next; cout << "\nptr->info = " << ptr->info << " and "; Print(L2, "L2"); RemoveNext(ptr); cout << "After RemoveNext(ptr): "; Print(L2, "L2"); ptr = L2->next; cout << "\nptr->info = " << ptr->info << " and "; Print(L2, "L3"); RemoveNext(ptr); cout << "After RemoveNext(ptr): "; Print(L2, "L3"); } void testRemoveDupMins(void) { ListNode *L1, *L2, *L3, *L4; initList(L1, 1); initList(L2, 2); initList(L3, 3); initList(L4, 4); cout << "Testing RemoveDupMins:\n\n"; Print(L1, "L1"); RemoveDupMins(L1); cout << "After RemoveDupMins(L1): "; Print(L1, "L1"); Print(L2, "\nL2"); RemoveDupMins(L2); cout << "After RemoveDupMins(L2): "; Print(L2, "L2"); Print(L3, "\nL3"); RemoveDupMins(L3); cout << "After RemoveDupMins(L3): "; Print(L3, "L3"); Print(L4, "\nL4"); RemoveDupMins(L4); cout << "After RemoveDupMins(L4): "; Print(L4, "L4"); } void initList(ListNode *&L, int n) { ListNode * list1 = new ListNode(3, new ListNode(0, new ListNode(12, new ListNode(0,NULL)))); ListNode * list2 = new ListNode(5, new ListNode(4, new ListNode(5, new ListNode(2,NULL)))); ListNode * list3 = new ListNode(10, new ListNode(10, new ListNode(10, new ListNode(20, new ListNode(10,NULL))))); ListNode * list4 = new ListNode(10, new ListNode(10,NULL)); ListNode * A[] = {list1, list2, list3, list4}; L = A[n-1]; } void Print(ListNode * list, const string &Lst) { cout << Lst << "-->"; while (list != NULL) { cout << "[" << list->info << "]-->"; list = list->next; } cout << "NULL" << endl; } void pause(void) { cout << "\nPress ..."; cin.ignore(80, '\n'); } void cls(void) { for (int k = 1; k <= 55; k++) cout << '\n'; }