import java.util.*; class List { private Cell llist; public static List empty() { return new List(null); } private List(Cell l) { llist=l; } public int head() { return llist.first; } public List tail() { return new List(llist.next); } public List cons(int n) { return new List(new Cell(n, llist)); } public boolean isEmpty() { return llist==null; } public String toString() { if(llist==null) return "[]"; else { String str="["+llist.first; for(Cell ptr=llist.next; ptr!=null; ptr=ptr.next) str+=","+ptr.first; return str+"]"; } } public static List fromString(String str) { str=str.trim(); if(str.charAt(0)!='['||str.charAt(str.length()-1)!=']') return empty(); str = str.substring(1,str.length()-1); StringTokenizer toks = new StringTokenizer(str,","); try { return new List(make(toks)); } catch(NumberFormatException e) { return empty(); } } private static Cell make(StringTokenizer toks) { if(toks.hasMoreTokens()) { String num = toks.nextToken().trim(); return new Cell(Integer.parseInt(num),make(toks)); } else return null; } private static class Cell { int first; Cell next; Cell(int f,Cell n) { first=f; next=n; } } }