Java Reference
In-Depth Information
1 package weiss.nonstandard;
2
3 // LinkedListIterator class; maintains "current position"
4 //
5 // CONSTRUCTION: Package visible only, with a ListNode
6 //
7 // ******************PUBLIC OPERATIONS*********************
8 // void advance( ) --> Advance
9 // boolean isValid( ) --> True if at valid position in list
10 // AnyType retrieve --> Return item in current position
11
12 public class LinkedListIterator<AnyType>
13 {
14 /**
15 * Construct the list iterator
16 * @param theNode any node in the linked list.
17 */
18 LinkedListIterator( ListNode<AnyType> theNode )
19 { current = theNode; }
20
21 /**
22 * Test if the current position is a valid position in the list.
23 * @return true if the current position is valid.
24 */
25 public boolean isValid( )
26 { return current != null; }
27
28 /**
29 * Return the item stored in the current position.
30 * @return the stored item or null if the current position
31 * is not in the list.
32 */
33 public AnyType retrieve( )
34 { return isValid( ) ? current.element : null; }
35
36 /**
37 * Advance the current position to the next node in the list.
38 * If the current position is null, then do nothing.
39 */
40 public void advance( )
41 {
42 if( isValid( ) )
43 current = current.next;
44 }
45
46 ListNode<AnyType> current; // Current position
47 }
figure 17.7
The
LinkedListIterator
class
 
Search WWH ::




Custom Search