sclass AppendableChain extends Chain { A element; Chain next, last; int size; *() {} *(A *element) { size = 1; last = this; } *(A *element, Chain *next) { if (next != null) { size = next.size; last = next.last; } else last = this; size++; } toString { ret str(toList()); } bool add(A a) { AppendableChain newLast = new Chain(a, null); last.next = newLast; last = newLast; ++size; true; } ArrayList toList() { ArrayList l = emptyList(size); Chain c = this; while (c != null) { l.add(c.element); c = c.next; } ret l; } // TODO: optimize public Iterator iterator() { ret toList().iterator(); } }