Java Reference
In-Depth Information
15.
Repeat the previous exercise, but instead use the following statements:
ListIterator<String> nameIterator = nameList.getIterator();
nameIterator.next();
nameIterator.remove();
nameIterator.next();
nameIterator.next();
nameIterator.previous();
nameIterator.remove();
System.out.println(nameIterator.next());
nameIterator.next();
nameIterator.set("Brittany");
System.out.println("Revised list:");
displayList(nameList);
System.out.println(nameIterator.previous());
System.out.println(nameIterator.next());
16.
Given a list of strings and an iterator
nameIterator
whose data type is
ListIterator
, write statements that add
the string
Bob
after the first occurrence of the string
Sam.
17.
If you wanted to implement the interface
ListIterator
as an inner class iterator by using a linked
implementation, what difficulties would you face?
18.
Implement an iterator for the array-based implementation of a bag, using an inner class. Include a
remove
operation.
19.
Repeat the previous exercise for the linked implementation of a bag.
20.
If you were to add an iterator to the ADT stack, should the iterator support the
remove
operation?
21.
Implement an iterator for the linked implementation of a stack, using an inner class.
22.
Implement an iterator for the array-based implementation of a stack, using an inner class.
P
ROJECTS
1.
Revise the class
LinkedListWithIterator
described in Segment 15.19 so that the inner class
IteratorForLinkedList
provides a
remove
operation. You will need another data field
priorNode
to reference the node before the next one.
2.
Implement all of the methods in the interface
ListIterator
as a separate class iterator.
3.
Implement the interface
ListIterator
as an inner class, but do not support the operations
add
,
remove
, and
set
.
4.
Consider a solitaire matching game in which you have a list of random integer values between 10 and 99. You
remove from the list any pair of consecutive integers whose first or second digits match. If all values are removed,
then you win.
For example, consider the following sequence of 10 integers:
10 82 43 23 89 12 43 84 23 32
The integers in the pair 10 and 82 do not match in either digit and so cannot be removed. However, the integers in
the pair 43 and 23 match in the second digit and are removed, leaving the following sequence:
10 82 89 12 43 84 23 32
Continue checking for pairs from 89, the value after the removed pair. No other pairs have matching integers.
Now return to the beginning of the list and check the pairs. The integers in the pair 82 and 89 match in the first
digit and can be removed:
10 12 43 84 23 32