// to be tested sclass AppendableChain extends Chain { A element; Chain next, last; int size; *() {} // only used internally *(A *element) { size = 1; last = this; } *(A *element, AppendableChain *next) { if (next == null) ret; new Chain b; b.element = next.element; b.next = next.next; this.next = b; last = next.last; size = next.size+1; } toString { ret str(toList()); } bool add(A a) { Chain newLast = new Chain(a, null); last.next = newLast; last = newLast; ++size; true; } AppendableChain dropFirst() { if (next == null) null; new AppendableChain a; a.element = next.element; a.next = next.next; a.last = last == next ? a : last; a.size = size-1; ret a; } 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(); } }