public class DeleteList { public static void main(String[] args) { List list = new List(); list.add("D"); list.add("C"); list.add("B"); list.add("A"); System.out.println(list); list.delete("B"); System.out.println(list); } } class List { protected ListCell head; public void add(Object o) { if (head == null) head = new ListCell(o,null); else { ListCell current = head; while (current.getNext() != null) current = current.getNext(); current.setNext(new ListCell(o,null)); } } public void delete(Object o) { head = deleteRec(o,head); } public static ListCell deleteRec(Object o, ListCell list) { if (list == null) return null; if (list.getDatum().equals(o)) return list.getNext(); list.setNext(deleteRec(o,list.getNext())); return list; } public String toString() { String list = ""; for (ListCell cell = head; cell != null; cell = cell.getNext()) { list = list + cell; if ( cell.getNext() != null ) list += "->"; } return list; } } class ListCell { protected Object datum; protected ListCell next; public ListCell(Object o, ListCell n) { datum = o; next = n; } public Object getDatum() { return datum; } public ListCell getNext() { return next; } public void setDatum(Object o) { datum = o; } public void setNext(ListCell l) { next = l; } public String toString() { return ""+datum; } }