public class ListStack implements Stack { private Link top; public ListStack() { top = null; } public void push(Object elem) { top = new Link(elem, top); } public boolean empty() { return top == null; } public Object pop() { Object poppedvalue; if (top == null) return null; poppedvalue = top.element(); top = top.next(); return poppedvalue; } public String toString() { Link stackPtr = top; String result = "["; if (stackPtr != null) { result = result + top.element(); for (stackPtr = stackPtr.next; stackPtr != null; stackPtr = stackPtr.next) { result = result + "," + stackPtr.element(); } } result = result + "]"; return result; } private class Link { private Object element; private Link next; public Link(Object newelement) { element = newelement; next = null; } public Link(Object newelement, Link newnext) { element = newelement; next = newnext; } public Link next() { return next; } public Object element() { return element; } public void setNext(Link newnext) { next = newnext; } public void setElement(Object newelement) { element = newelement; } } }