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 queue given in this chapter.
(b) Write this function as a member function to the pointer-based ADT queue given in this chapter.
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 departure event.
4. Carrano, 4th edition, Chapter 8, Exercise 11. The section “Class Tem-plates” 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 queue given in this chapter.
Solution:
int Queue::getNumberOfElements() const{ return count;
}
(b) Write this function as a member function to the pointer-based ADT queue given in this chapter.
Solution:
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.) Solution: (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.)
Solution:
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 departure event.
Time Action bankQueue anEventList
0 Read file, place event in anEventList (empty) A-5-9 5 Update anEventList and bankQueue:
Customer 1 enters bank
5-9 (empty)
Customer 1 begins transaction, create departure event
5-9 D-14
Read file, place event in anEventList 5-9 A-7-2 D-14 7 Update anEventList and bankQueue:
Customer 2 enters bank
5-9 7-5 D-14
Read file, place event in anEventList 5-9 7-5 D-14 A-14-5 14 Update anEventList and bankQueue:
Customer 1 departs
7-5 A-14-5
Customer 2 begins transaction, create departure event
7-5 A-14-5 D-19
Update anEventList and bankQueue: Customer 3 enters bank
7-5 14-5 D-19
Read file, place event in anEventList 7-5 14-5 D-19 A-30-5 19 Update anEventList and bankQueue:
Customer 2 departs
14-5 A-30-5
Customer 3 begins transaction, create departure event
14-5 D-24 A-30-5
24 Update anEventList and bankQueue: Customer 3 departs
(empty) A-30-5
30 Update anEventList and bankQueue: Customer 4 enters bank
30-5 (empty)
Customer 4 begins transaction, create departure event
30-5 D-35
Read file, place event in anEventList 30-5 A-32-5 D-35 32 Update anEventList and bankQueue:
Customer 5 enters bank
30-5 32-5 D-35
Read file, place event in anEventList 30-5 32-5 A-34-5 D-35 34 Update anEventList and bankQueue:
Customer 6 enters bank
30-5 32-5 34-5 D-35
Read file, no more customers 30-5 32-5 34-5 D-35 35 Update anEventList and bankQueue:
Customer 4 departs
32-5 34-5 (empty)
Customer 5 begins transaction, create departure event
32-5 34-5 D-40
40 Update anEventList and bankQueue: Customer 5 departs
34-5 (empty)
Customer 6 begins transaction, create departure event
34-5 D-45
45 Update anEventList and bankQueue: Customer 6 departs
4. Carrano, 4th edition, Chapter 8, Exercise 11. The section “Class Tem-plates” 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.
Solution:
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.)
Solution: 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;