package DataStructures; import java.util.NoSuchElementException; /** * A linked list implementation of a stack * @param the type of elements held in this stack * @author Peter Williams */ public class StackList implements Stack { private ListNode top; /** * Creates an empty stack suitable for holding items of type E */ public StackList() { top = null; } public boolean isEmpty() { return top == null; } public void push(E item) { top = new ListNode(item, top); } public E pop() { if (top == null) { throw new NoSuchElementException(); } else { E item = top.data; top = top.next; return item; } } }