Chapter 15 Review Exercise Solutions
R15.1
The code prints:

Tom
Diana
Harry

+--------+
|        +
+--------+

+--------+   +--------+
|        +-->|Harry   +
+--------+   +--------+

+--------+   +--------+   +--------+
|        +-->|Diana    +-->|Harry   +
+--------+   +--------+   +--------+

+--------+   +--------+   +--------+   +--------+
|        +-->|Tom     +-->|Diana    +-->|Harry   +
+--------+   +--------+   +--------+   +--------+

+--------+   +--------+   +--------+
|        +-->|Diana    +-->|Harry   +
+--------+   +--------+   +--------+

+--------+   +--------+
|        +-->|Harry   +
+--------+   +--------+

+--------+
|        +
+--------+
R15.2
The code prints:

Harry
Tom
Diana

+--------+
|        +
+--------+

+--------+   +--------+
|        +-->|Harry   +
+--------+   +--------+

+--------+   +--------+   +--------+
|        +-->|Diana    +-->|Harry   +
+--------+   +--------+   +--------+

+--------+   +--------+   +--------+   +--------+
|        +-->|Tom     +-->|Diana    +-->|Harry   +
+--------+   +--------+   +--------+   +--------+

+--------+   +--------+   +--------+
|        +-->|Tom     +-->|Diana    +
+--------+   +--------+   +--------+

+--------+   +--------+
|        +-->|Diana    +
+--------+   +--------+

+--------+
|        +
+--------+
R15.3
The code prints:

Diana
Tom
Harry

+--------+
|        +
+--------+

+--------+   +--------+
|        +-->|Harry   +
+--------+   +--------+

+--------+   +--------+   +--------+
|        +-->|Harry   +-->|Diana    +
+--------+   +--------+   +--------+

+--------+   +--------+   +--------+   +--------+
|        +-->|Tom     +-->|Harry   +-->|Diana    +
+--------+   +--------+   +--------+   +--------+

+--------+   +--------+   +--------+
|        +-->|Tom     +-->|Harry   +
+--------+   +--------+   +--------+

+--------+   +--------+
|        +-->|Harry   +
+--------+   +--------+

+--------+
|        +
+--------+
R15.4
iterator.add("Tom"); 	// T|
iterator.add("Diana"); 	// TD|
iterator.add("Harry"); 	// TDH|
iterator = staff.listIterator(); 	// |TDH
if (iterator.next().equals("Tom")) // T|DH
   iterator.remove(); 	// |DH
while (iterator.hasNext())
   System.out.println(iterator.next()) // D|H and DH|
/*
   prints:
   Diana
   Harry
*/
R15.5
iterator.add("Tom"); // T|
iterator.add("Diana"); // TD|
iterator.add("Harry"); // TDH|
iterator = staff.listIterator(); // |TDH
iterator.next(); // T|DH
iterator.next(); // TD|H
iterator.add("Romeo"); // TDR|H
iterator.next(); // TDRH|
iterator.add("Juliet"); // TDRHJ|
iterator = staff.listIterator(); // |TDRHJ
iterator.next(); // T|DRHJ
iterator.remove(); // |DRHJ
while (iterator.hasNext())
   System.out.println(iterator.next())
/*
   prints:
   Diana
   Romeo
   Harry
   Juliet
*/
R15.6
Insertion:

Removal:

R15.7
Insertion:

Removal:

R15.8
Lists allow insertion and removal in the middle without having to move the remaining elements. That is, insertion and removal are O(1) for lists, O(n) for arrays, where n is the number of elements. Arrays allow access of arbitrary elements in constant time. To access an element in the middle of the list, you must follow all links from the beginning to the desired element. That is, element access is O(1) for arrays, O(n) for lists.
R15.9
Using a sorted array and binary search for the lookup is best. It speeds up the lookup to log2 10000 operations (about 13), whereas searching in a linked list requires traversal of, on average, half the list, or 3000 - 5000 operations. Because we expect several hundred lookups a day (say 200), we can expect maybe 40,000 such operations a year (figuring 200 working days per year). What about the added cost of inserting new employees? Up to 4,000 times, there may be a cost of moving up, on average, half the employee records, when using an array implementation. This higher cost occurs much less frequently than lookups, so we aren't as concerned about it.
R15.10
It depends. If appointments are mostly inserted, displayed and removed, then a linked list would be better. If they are frequently searched by a single criterion (such as time), then an array would be better.
R15.11
The cards should be stored in a queue because the first card in is the first card out (FIFO).
R15.12
The strings are printed in alphabetical order.
i.e.
First stack:
Z
Y
X
.
.
.
C
B
A
After popping first stack into second stack:
A
B
C
.
.
.
X
Y
Z

Popping and printing the second stack will result in:
ABC...XYZ

R15.13
Using a queue, the cells in the maze will be visited in the following order: 
1, 4, 5, 6, 2, 9, 13, 12, 11, 16, 18, 19, 20, 21, 22, 17, 14, 15, 10, 8, 7, 3

R15.14
Using a stack, the cells in the maze will be visited in the following order: 
1, 4, 5, 6, 9, 13, 12, 11, 16, 18, 19, 20, 21, 22, 17, 14, 15, 10, 8, 7, 3