impl: cll iterator

This commit is contained in:
Václav Přibík
2025-10-18 15:10:06 +02:00
parent a6136413b9
commit d29d8b156e
2 changed files with 31 additions and 2 deletions

View File

@@ -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

View File

@@ -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;
}
}