class IntSet14a { // Destructive Set class, sets stored as unordered linked lists // Uses recursive methods private Cell list; public void delete(int n) { if(list!=null) if(list.first==n) list=list.next; else delete(n,list); } private static void delete(int n,Cell l) { if(l.next!=null) if(l.next.first==n) l.next=l.next.next; else delete(n,l.next); } public void add(int n) { if(list==null) list = new Cell(n,null); else add(n,list); } private static void add(int n,Cell l) { if(l.first!=n) if(l.next==null) l.next = new Cell(n,null); else add(n,l.next); } public boolean member(int n) { return member(n,list); } private static boolean member(int n,Cell l) { if(l==null) return false; else if(n==l.first) return true; else return member(n,l.next); } public String toString() { if(list==null) return "{}"; else return "{"+toString(list)+"}"; } private static String toString(Cell l) { if(l.next==null) return ""+l.first; else return l.first+","+toString(l.next); } private static class Cell { int first; Cell next; Cell(int f,Cell n) { first=f; next=n; } } }