import java.util.LinkedList;
import java.util.ListIterator;
public class Main {
public static void main
(String[] args
) { final LinkedList<String> list = new LinkedList<String> ();
list.add ("1"); list.add ("2"); list.add ("3"); list.add ("4");
final MyIterator<String> it = new MyIterator (list.listIterator());
System.
out.
println(it.
next()); System.
out.
println(it.
next ()); System.
out.
println(it.
next ()); System.
out.
println(it.
previous ()); System.
out.
println(it.
previous ()); System.
out.
println(it.
next ()); }
public static class MyIterator<T> {
private final ListIterator<T> listIterator;
private boolean nextWasCalled = false;
private boolean previousWasCalled = false;
public MyIterator(ListIterator<T> listIterator) {
this.listIterator = listIterator;
}
public T next() {
nextWasCalled = true;
if (previousWasCalled) {
previousWasCalled = false;
listIterator.next ();
}
return listIterator.next ();
}
public T previous() {
if (nextWasCalled) {
listIterator.previous();
nextWasCalled = false;
}
previousWasCalled = true;
return listIterator.previous();
}
}
}
aW1wb3J0IGphdmEudXRpbC5MaW5rZWRMaXN0OwppbXBvcnQgamF2YS51dGlsLkxpc3RJdGVyYXRvcjsKCnB1YmxpYyBjbGFzcyBNYWluIHsKCQlwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CgkJCWZpbmFsIExpbmtlZExpc3Q8U3RyaW5nPiBsaXN0ID0gbmV3IExpbmtlZExpc3Q8U3RyaW5nPiAoKTsKCQkJCgkJCWxpc3QuYWRkICgiMSIpOyBsaXN0LmFkZCAoIjIiKTsgbGlzdC5hZGQgKCIzIik7IGxpc3QuYWRkICgiNCIpOwoJCQkKCQkJZmluYWwgTXlJdGVyYXRvcjxTdHJpbmc+IGl0ID0gbmV3IE15SXRlcmF0b3IgKGxpc3QubGlzdEl0ZXJhdG9yKCkpOwoJCQkKCQkJU3lzdGVtLm91dC5wcmludGxuKGl0Lm5leHQoKSk7CgkJCVN5c3RlbS5vdXQucHJpbnRsbihpdC5uZXh0ICgpKTsKCQkJU3lzdGVtLm91dC5wcmludGxuKGl0Lm5leHQgKCkpOwoJCQlTeXN0ZW0ub3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