// making everything public for clarity (but bad style!) // see C&S 23.35-23.38 public class TestDLL { public static void main(String[] args) { DLL list = new DLL(); list.append("D"); list.append("C"); list.append("B"); list.append("A"); System.out.println(list); list.removeFront(); System.out.println(list); } } class DLL { public DLLCell head; public DLLCell tail; public void append(Object o) { if (head == null) { head = new DLLCell(o,null,null); tail = head; } else { tail.next = new DLLCell(o,tail,null); tail = tail.next; } } public Object removeFront() { Object front = null; if (head != null) { front = head.data; head = head.next; if ( head == null ) tail = null; else head.prev = null; } return front; } public String toString() { String list = ""; for (DLLCell cell = head; cell != null; cell = cell.next) { list = list + cell; if ( cell.next != null ) list += "<-->"; } return list; } } class DLLCell { public DLLCell next; public DLLCell prev; public Object data; public DLLCell(Object data, DLLCell prev, DLLCell next) { this.data = data; this.prev = prev; this.next = next; } public String toString() { return ""+data; } }