Java Reference
In-Depth Information
74 /**
75
Constructs an iterator that points to the front
76
of the linked list.
77 */
78
public
LinkedListIterator()
79 {
80 position =
null
;
81 previous =
null
;
82 }
83
84 /**
85
Moves the iterator past the next element.
86 @return
the traversed element
87 */
88
public
Object next()
89 {
90
if
(!hasNext())
91
throw new
NoSuchElementException();
92 previous = position;
//
Remember for remove
93
94
if
(position ==
null
)
95 position = first;
96
else
97 position = position.next;
98
99
return
position.data;
100 }
101
102 /**
103
Tests if there is an element after the iterator
104
position.
105 @return true
if there is an element
after the iterator
106
position
107 */
108
public boolean
hasNext()
109 {
110
if
(position ==
null
)
111 return first !=
null
;
112
else
113 return position.next !=
null
;
114 }