void MergeStacks(const Stack & first, const Stack & second, Stack & result) // precondition: # elements in first >= # elements in second, result is empty // postcondition: result = merge of first and second (starting with first) { Stack copy1(first); Stack copy2(second); result.MakeEmpty(); // be sure! while (! copy2.IsEmpty()) { result.Push(copy1.Top()); result.Push(copy2.Top()); copy1.Pop(); copy2.Pop(); } // finish copy1 if necessary while (! copy1.IsEmpty()) { result.Push(copy1.Top()); copy1.Pop(); } // result is backwards, pop off into copy1 then copy back to result while (! result.IsEmpty()) { copy1.Push(result.Top()); result.Pop(); } result = copy1; } // problem 2 part A Tuple & Tuple::operator += (const Tuple & t) // precondition: my tuple value = a, t.Size() == Size() // postcondition: my tuple value = a + t, the value a+t is returned { if (mySize == t.mySize) // check to be sure { int k; for(k=0; k < mySize; k++) // add each vector element { myList[k] += t.myList[k]; } } return *this; } // Part B Tuple operator + (const Tuple & t1, const Tuple & t2) // precondition: t1.Size() == t2.Size() // postcondition: returns t1 + t2 { Tuple temp(t1); temp += t2; return temp; } // Part C // create an assignment operator as shown below Tuple & operator = (const Vector & v) // pre: v.Length() == mySize // post: assigns values in v to corresponding elements of Tuple // Part D: in a nutshell: random access // Problem 3, Part A bool IsMember(Member * list, string name) // postcondition: returns true if name is in list // otherwise returns false { while (list) { if (list->name == name) return true; list = list->next; } return false; } Member * FindClubList(Club * clubList, string clubname) // postcondition: returns a pointer to the list of members in the // club clubname (returns 0 if no club named clubname) { while (clubList) { if (clubList->clubname == clubname) return clubList->list; clubList = clubList->next; } return 0; } void CloneClubs(Club * & clubList, string club1, string club2, string newName) // precondition: no club named newName is in clubList // postcondition: a club named newName is added to clubList, the // members of the new club are all members from // clubs club1 and club2, with no member listed twice { clubList = new ClubList(newName,0, clubList); // add new club at front Member * first = FindClubList(clubList,club1); // find clubs Member * second = FindClubList(clubList,club2); // add all members of first club, and selected members of second while (first) { // add all members of first to front of new club clubList->list = new Member(first->name,clubList->list); first = first->next; } while (second) { if (! IsMember(clubList->list,second->name)) { clubList->list = new Member(second->name,clubList->list); } second = second->next; } } // problem 4 MultiQueue::Dequeue() // postcondition: "first" student is removed from queue // EXCEPTION raised if queue is empty { EXCEPTION( IsEmpty( ), "Queue is empty" ); int k=0; while(myQueues[k].IsEmpty()) // at least one queue non-empty { k++; } myQueues[k].Dequeue(); } void MultiQueue::Withdraw(const Student & s) // postcondition: student with ssnum s.ssnum is dequeued, // relative order of other students unchanged { Queue temp; Student stud; int qnum = s.class; // queue student should be in // remove all students from queue, into temp, but skip student s while (!myQueues[qnum].IsEmpty()) { stud = myQueues[qnum].GetFront(); if (stud.ssnum != s.ssnum) { temp.Enqueue(stud); } myQueues[qnum].Dequeue(); } myQueues[qnum] = temp; // copy back } // alternate solution void MultiQueue::Withdraw(const Student & s) // postcondition: student with ssnum s.ssnum is dequeued, // relative order of other students unchanged { Queue * temp = &myQueues[s.class]; // pointer to right queue Student first = temp->GetFront(); // first student in queue temp->Dequeue(); // remove first student if (first.ssnum != s.ssnum) // first student withdrawn? { temp->Enqueue(first); // first shall be last // dequeue all students, skipping s, until back to first while (temp->GetFront().ssnum != first.ssnum) { if (temp->GetFront().ssnum != s.ssnum) { temp->Enqueue(GetFront()); } temp->Dequeue(); } } }