sclass ReverseChain implements Iterable {
A element;
ReverseChain prev;
int size;
*() {}
*(ReverseChain *prev, A *element) {
if (prev == null) size = 1;
else {
prev.check();
size = prev.size+1;
}
}
void check {
if (size < 1) fail("You called the ReverseChain default constructor. Don't do that");
}
toString {
ret str(toList());
}
ArrayList toList() {
check();
ArrayList l = emptyList(size);
for i to size: l.add(null);
int i = size;
ReverseChain c = this;
while (c != null) {
l.set(--i, c.element);
c = c.prev;
}
ret l;
}
public Iterator iterator() { ret toList().iterator(); }
}