impl: cll iterator
This commit is contained in:
@@ -10,8 +10,7 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<T> iterator() {
|
public Iterator<T> iterator() {
|
||||||
// TODO Auto-generated method stub
|
return new CustomLinkedListIterator<T>(this);
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'iterator'");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package appointmentplanner.customlist;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class CustomLinkedListIterator<T> implements Iterator<T> {
|
||||||
|
|
||||||
|
private CustomLinkedListImpl<T> list;
|
||||||
|
|
||||||
|
private CustomLinkedListNode<T> lastNode;
|
||||||
|
|
||||||
|
public CustomLinkedListIterator(CustomLinkedListImpl<T> listToIterate) {
|
||||||
|
list = listToIterate;
|
||||||
|
lastNode = list.head;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return lastNode.getNext() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T next() {
|
||||||
|
T item = lastNode.getItem();
|
||||||
|
lastNode = lastNode.getNext();
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user