impl: cll iterator
This commit is contained in:
@@ -10,8 +10,7 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'iterator'");
|
||||
return new CustomLinkedListIterator<T>(this);
|
||||
}
|
||||
|
||||
@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