Data Structures, Practice Homework 2, with Solutions (not to be handed in) 1. Carrano, 4th edition, Chapter 7, Exercise 4. Consider a function int Queue::getNumberOfElements() const that returns the number of elements in a queue without changing the queue. (a) Write this function as a member function to the array-based ADT (b) Write this function as a member function to the pointer-based ADT 2. Carrano, 4th edition, Chapter 7, Exercise 10. An operation that displays the contents of a queue can be useful during program debugging. Add a display operation to the ADT queue such that (a) display uses only ADT queue operation, and so it is independent of the queue s implementation. (So write this one in pseudocode.) (b) display assumes and uses the pointer-based implementation of the ADT queue. (So write this one in C++, as another member function of pointer-based implementation of class Queue.) 3. Carrano, 4th edition, Chapter 7, Exercise 12. With the following data, hand-trace the execution of the bank-line simulation that this chapter describes. Each line of data contains an arrival time and a transaction time. Show the state of the queue and the event list at each step. 5 9 7 5 14 5 30 5 32 5 34 5 Note that at time 14 there is a tie between the execution of an arrival event and a. 4. Carrano, 4th edition, Chapter 8, Exercise 11. The section Class Templates describes a class template for List. Using this template, write C++ statements that define an ADT list of integers, and prompt the user to enter those integers into the list.
5. Carrano, 4th edition, Chapter 8, (part of) Exercise 12. Overload the assignment operator = for the pointer-based implementation of the class Stack. Recall two stacks are considered to be equal if they have the same number of elements, and exactly the same element in each place from the top to the bottom. (Hint: Study the copy constructors, and the implementation for class List on page 426.)
Solutions 1. Carrano, 4th edition, Chapter 7, Exercise 4. Consider a function int Queue::getNumberOfElements() const that returns the number of elements in a queue without changing the queue. (a) Write this function as a member function to the array-based ADT int Queue::getNumberOfElements() const{ return count; (b) Write this function as a member function to the pointer-based ADT int Queue::getNumberOfElements() const{ int i=0; for (QueueNode* tempptr = frontptr; tempptr!= NULL; tempptr = tempptr->next) ++i; return i; 2. Carrano, 4th edition, Chapter 7, Exercise 10. An operation that displays the contents of a queue can be useful during program debugging. Add a display operation to the ADT queue such that (a) display uses only ADT queue operation, and so it is independent of the queue s implementation. (So write this one in pseudocode.) (The queue whose items are to be displayed is called aqueue.) tempqueue.createqueue(); // dequeue all items from aqueue, print them, and store them in // tempqueue while (!aqueue.isempty()){ aqueue.dequeue(x) cout << x << " " tempqueue.enqueue(x)
cout << endl; // put items back into aqueue while (!tempqueue.isempty()){ tempqueue.dequeue(x) aqueue.enqueue(x) tempqueue.destroyqueue() (b) display assumes and uses the pointer-based implementation of the ADT queue. (So write this one in C++, as another member function of pointer-based implementation of class Queue.) void Queue::display() const{ for (QueueNode* tempptr = frontptr; tempptr!= NULL; tempptr = tempptr->next) cout << tempptr->item << " "; cout << endl; 3. Carrano, 4th edition, Chapter 7, Exercise 12. With the following data, hand-trace the execution of the bank-line simulation that this chapter describes. Each line of data contains an arrival time and a transaction time. Show the state of the queue and the event list at each step. 5 9 7 5 14 5 30 5 32 5 34 5 Note that at time 14 there is a tie between the execution of an arrival event and a.
Time Action bankqueue aneventlist 0 Read file, place event in aneventlist (empty) A-5-9 5 Update aneventlist and bankqueue: 5-9 (empty) Customer 1 enters bank Customer 1 begins transaction, create 5-9 D-14 Read file, place event in aneventlist 5-9 A-7-2 D-14 7 Update aneventlist and bankqueue: 5-9 7-5 D-14 Customer 2 enters bank Read file, place event in aneventlist 5-9 7-5 D-14 A-14-5 14 Update aneventlist and bankqueue: 7-5 A-14-5 Customer 1 departs Customer 2 begins transaction, create 7-5 A-14-5 D-19 Update aneventlist and bankqueue: 7-5 14-5 D-19 Customer 3 enters bank Read file, place event in aneventlist 7-5 14-5 D-19 A-30-5 19 Update aneventlist and bankqueue: 14-5 A-30-5 Customer 2 departs Customer 3 begins transaction, create 14-5 D-24 A-30-5 24 Update aneventlist and bankqueue: (empty) A-30-5 Customer 3 departs 30 Update aneventlist and bankqueue: 30-5 (empty) Customer 4 enters bank Customer 4 begins transaction, create 30-5 D-35 Read file, place event in aneventlist 30-5 A-32-5 D-35 32 Update aneventlist and bankqueue: 30-5 32-5 D-35 Customer 5 enters bank Read file, place event in aneventlist 30-5 32-5 A-34-5 D-35 34 Update aneventlist and bankqueue: 30-5 32-5 34-5 D-35 Customer 6 enters bank Read file, no more customers 30-5 32-5 34-5 D-35 35 Update aneventlist and bankqueue: 32-5 34-5 (empty) Customer 4 departs Customer 5 begins transaction, create 32-5 34-5 D-40 40 Update aneventlist and bankqueue: 34-5 (empty) Customer 5 departs Customer 6 begins transaction, create 34-5 D-45 45 Update aneventlist and bankqueue: Customer 6 departs (empty) (empty)
4. Carrano, 4th edition, Chapter 8, Exercise 11. The section Class Templates describes a class template for List. Using this template, write C++ statements that define an ADT list of integers, and prompt the user to enter those integers into the list. List<int> alist; for (int i=1; i<=5; ++i){ int x; cout << "Enter an integer: "; cin >> x; alist.insert(i,x); 5. Carrano, 4th edition, Chapter 8, (part of) Exercise 12. Overload the assignment operator = for the pointer-based implementation of the class Stack. (Hint: Study the copy constructors.) Note this solution does not contain any error handling in the case of a new failure. Stack& Stack::operator= (const Stack& rhs){ // remove contents of lhs stack while (!isempty()) pop(); // this is the code from the copy constructor if (rhs.topptr == NULL) topptr = NULL; else{ // copy first node topptr = new StackNode; topptr->item = rhs.topptr->item; // copy rest of stack StackNode* newptr = topptr; // new stack pointer for (StackNode* origptr = rhs.topptr->next; origptr!= NULL; origptr = origptr->next){ newptr->next = new StackNode; newptr = newptr->next; newptr->item = origptr->item; // end for newptr->next = NULL; // end else return *this; // end operator= function