Introduce ListItemIterator

This commit is contained in:
2021-09-05 15:26:51 +02:00
parent 7e449ded6e
commit 78fcf375a4
4 changed files with 56 additions and 3 deletions

View File

@ -1,7 +1,17 @@
package aud.exam.prep.list; package aud.exam.prep.list;
public class DoubleListItem<T> { public class DoubleListItem<T> implements ListItem<T> {
public T key; public T key;
public DoubleListItem<T> next;
public DoubleListItem<T> prev; public DoubleListItem<T> prev;
public DoubleListItem<T> next;
@Override
public T head() {
return key;
}
@Override
public ListItem<T> tail() {
return next;
}
} }

View File

@ -0,0 +1,8 @@
package aud.exam.prep.list;
public interface ListItem<T> {
T head();
ListItem<T> tail();
}

View File

@ -0,0 +1,24 @@
package aud.exam.prep.list;
import java.util.Iterator;
public class ListItemIterator<T> implements Iterator<T> {
private ListItem<T> current;
public ListItemIterator(ListItem<T> current) {
this.current = current;
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
T t = current.head();
current = current.tail();
return t;
}
}

View File

@ -1,6 +1,17 @@
package aud.exam.prep.list; package aud.exam.prep.list;
public class SingleListItem<T> { public class SingleListItem<T> implements ListItem<T> {
public T key; public T key;
public SingleListItem<T> next; public SingleListItem<T> next;
@Override
public T head() {
return key;
}
@Override
public ListItem<T> tail() {
return next;
}
} }