class LispList { private Cell myList; private LispList(Cell list) { myList=list; } public boolean isEmpty() { return myList==null; } public E head() { return myList.first; } public LispList tail() { return new LispList(myList.rest); } public LispList cons(E item) { return new LispList(new Cell(item,myList)); } public static LispList empty() { return new LispList(null); } public boolean equals(Object other) { if(!(other instanceof LispList)) return false; Cell ptr1 = this.myList; Cell ptr2 = ((LispList) other).myList; for(;ptr1!=ptr2&&ptr1!=null&&ptr2!=null; ptr1=ptr1.rest,ptr2=ptr2.rest) if(!ptr1.first.equals(ptr2.first)) return false; return (ptr1==ptr2); } public String toString() { if(myList==null) return "[]"; else return "["+myList.first+restToString(myList.rest); } private static String restToString(Cell l) { if(l==null) return "]"; else return ","+l.first+restToString(l.rest); } private static class Cell { T first; Cell rest; Cell(T h,Cell t) { first=h; rest=t; } } }