Solutions to Test 1 Fall 1996 Problem 1 Part A int Max(const Stack & numbers) // precondition: numbers is not empty // postcondition: returns the maxmimum value on the stack numbers { Stack temp(numbers); int val; int max = temp.Top(); Top.Pop(); while (!temp.IsEmpty()) { val = temp.Top(); if (val > max) max = val; temp.Pop(); } return max; } Part B template Etype Stack::Max() // precondition: stack is not empty // postcondition: returns maximum value on the stack { int k; Etype max = myArray[0]; for (k=1; k<= myTop; k++) { if (myArray[k] > max) max = myArray[k]; } return max; } Problem 2 1. O(N^2P) 2. O(N^2) 3. a) O(N) b) O(1) c) O(N) d) O(N) e) O(N) 4. T(N) = N T(N-1) + cN log N T(0) = c; OR T(N) = N T(N-1) + cN log N + cN T(0) = c; OR T(N) = N T(N-1) + O(N log N) T(0) = c; Problem 3 Part A bool IsMember(SNode * list, string name) // postcondition: returns true if name is a member of the list, // otherwise returns false { while (list != NULL) { if (name == list->name) return true; list = list -> next; } return false; } Part B void PrintCompanies (CNode * list, string student) // postcondition: Prints a list of companies on one line that // student is interviewing with. { while (list != NULL) { if (IsMember(list->first), student) cout << list->name << " "; list = list->next; } cout << endl; } Part C void CopyList(Cnode * list, string company1, string company2) // precondition: company1 is a company name in list // postcondition: makes a copy of company1's student list and inserts the // list to the immediate right of company1 in list with the new // company name company2. The students in company2's list do not // have to be in the same order as they are in company1's list. { while (list->name != name) // find company1 { list = list->next; } list->next = new CNode(company2, list->next); CNode * newComp = list->next; SNode * temp = list->first; // ptr for company 1 list while (temp != NULL) { newComp->first = new SNode(temp->new, newComp->first); temp = temp->next; } } Problem 4 Part A int Polynomial::LowestDegree() // postcondition: returns the degree of the smallest term { Node * temp = myPoly; while (temp->next!=NULL) // move to last node { temp = temp->next; } return temp->degree; } Part B void Polynomial::Swap(Polynomial & Poly) // postcondition: Swap the values of two Polynomials { Node * temp = myPoly; myPoly = Poly.myPoly; Poly.myPoly = temp; int tempsize = myNumNodes; myNumNodes = Poly.myNumNodes; Poly.myNumNodes = tempsize; } Part C Only the private variables would be copied. The myNumNodes variable of B would be a copy of the myNumNodes for A, which is fine. However, the myPoly pointer for B would be a copy of the myPoly pointer for A, and thus both pointers would be pointing to the SAME linked list. The individual nodes were not copied, just the pointers. Part D void Process(Polynomial & P); void DoIt(Polynomial Q); main () { Polynomial Poly(3.2, 4.5, 2.1); // constructor Polynomial B(A); // copy constructor Polynomial C = B; // copy constructor Polynomial D; // constructor D = Poly; // none Process(C); // none DoIt(D); // copy constructor } Part E A destructor is invoked when a variable goes out of scope. If a destructor is not written, one will be provided, however the pointer will be destructed, and the nodes in the linked list will not be returned to the heap. Part F void Polynomial::Compress() { Node * prev = myPoly; Node * temp = myPoly->next; // myPoly is never NULL while (temp !=NULL) { while(temp != NULL && prev->degree == temp->degree) // need to compress { prev->coeff += temp->coeff; prev->next = temp->next; delete temp; myNumNodes --; temp = prev->next; } if (temp != NULL) { temp = temp ->next; prev = prev ->next; } } RemoveZeros(); }