You are on page 1of 3

Java Software Structures, 4th Edition Exercise Solutions, Ch.

Chapter 5 Exercise Solutions

EX 5.1 Hand trace a queue X through the following operations:


X.enqueue(new Integer(14));
X.enqueue(new Integer(11));
Object Y = X.dequeue();
X.enqueue(new Integer(18));
X.enqueue(new Integer(12));
X.enqueue(new Integer(15));
X.enqueue(new Integer(13));
Object Y = X.dequeue();
X.enqueue(new Integer(14));
X.enqueue(new Integer(19));

The queue, after each operation (rear of queue at the left):


14
11 14
11
18 11
12 18 11
15 12 18 11
13 15 12 18 11

EX 5.2 Given the queue X that results from Exercise 5.1, what would be the result of
each of the following?

a.) Y = X.dequeue();
Z = X.dequeue();

Y = 18, Z = 12

b.) X.first();

X.first() would return 15 after the elements 18 and 12 are removed at the step
a.

c.) X.enqueue(25);

The queue will have the form: 25 19 14 13 15

d.) X.first();

X.first() will still return 15.

EX 5.3 What would be the time complexity of the size operation for each of the
implementations if there were not a count variable?
With the count variable, the size operation is O(1) for all implementations. Without the
count variable, the regular array solution and the linked solution would require the
current elements to be counted, making the complexity O(n). For the circular array
solution, the number of elements could be computed using the front and rear pointers

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 5

(taking into account whether the end of the array is being spanned), so it would remain
O(1).

EX 5.4 Under what circumstances could the head and tail references for the linked
implementation or the front and rear references of the array implementation be
equal?

With a linked implementation, head and tail are equal when the queue is empty and they are both
null. They are also equal when there is only one element in the queue.

With a circular array implementation, front and rear are equal when the array is empty or the array
is full.

EX 5.5 Hand trace the ticket counter problem for 22 customers and 4 cashiers. Graph
the total process time for each person. What can you surmise from these
results?

Cutomer Number Arrival time Cashier 1 Cashier 2 Cashier 3 Cashier 4 Wait Time Total Time
1 0 0 0 120
2 15 15 0 120
3 30 30 0 120
4 45 45 0 120
5 60 120 60 180
6 75 135 60 180
7 90 150 60 180
8 105 165 60 180
9 120 240 120 240
10 135 255 120 240
11 150 270 120 240
12 165 285 120 240
13 180 360 180 300
14 195 375 180 300
15 210 390 180 300
16 225 405 180 300
17 240 480 240 360
18 255 495 240 360
19 270 510 240 360
20 285 525 240 360
21 300 600 300 420

Customer wait time is going up. If this system were to continue, customer wait time would
continue to increase without bound.

EX 5.6 Compare and contrast the enqueue method of the LinkedQueue class to the
push method of the LinkedStack class from Chapter 4.
Both add an element to the collection by adding a node to the end of a linked list. For
enqueue, the node is added to the rear of the queue, and the only special case is if the
queue is empty before the add. For push, the node is added to the top of the stack, and
there are no special cases.

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 5

EX 5.7 Describe two different ways the isEmpty method of the LinkedQueue class
could be implemented.
In addition to checking the value of count, a queue will be empty only if the reference
the front of the queue is null, or if the reference to the rear of the queue is null. You
cannot, however, simply check if front and rear are equal, which will occur when
there are zero or one elements in the queue.

EX 5.8 Name five everyday examples of a queue other than those discussed in this
chapter.
(1) Standing in line at the grocery store check out, (2) cars waiting in line at an
intersection, (3) lining kids up in a classroom in the order they arrive, (4) a waiting list to
join a club, and (5) a line at a cafeteria.

EX 5.9 Explain why the array implementation of a stack does not require elements to
be shifted but the noncircular array implementation of a queue does.
All operations on a stack occur on one end of the stack (the top). Therefore, the bottom
of the stack can remain at location 0 of the array at all times. Operations on a queue,
on the other hand, occur at both ends – additions to one end and removals from the
other. If a non-circular array is used, the end can "move away" from index 0, requiring a
shift in the elements to keep the elements rooted at that location.

EX 5.10 Suppose the count variable was not used in the CircularArrayQueue class.
Explain how you could use the values of front and rear to compute the number
of elements in the list.
The difference between the front and rear indexes indicates the number of elements in
the list, but the circular nature of the array must be taken into account. The following
code could be used:

public int size()


{
int result = rear – front;
if (front > rear)
result = MAX – front - rear;
else if (front == rear) && (queue[front] != null)
result = MAX
return result;
}

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

You might also like