public class Stack { ListItem contents; Stack() { contents = null; } public void push(Object x) { contents = new ListItem(x,contents); } public Object pop() { if (isEmpty()) return null; else { Object topItem = contents.item; contents = contents.next; return topItem; } } public Object peek() { if (isEmpty()) return null; else return contents.item; } public boolean isEmpty() { return (contents == null); } public String toString() { if (isEmpty()) return "empty stack"; else return "stack " + contents; } }