public class StackLL { private LinkedList list; private int size; public StackLL() { list = new LinkedList(); size = 0; } /** Pushes item onto the stack */ public void push(Item item) { Node node = new Node(item, null); list.insert(node); size++; } /** Pops off an item from the stack */ public Item pop() { if (isEmpty()) throw new RuntimeException("Trying to pop an empty stack."); size--; return (Item) list.delete(); } /** Returns the item at the top of the stack */ public Item top() { if (isEmpty()) throw new RuntimeException("Trying to access an empty stack."); return (Item) list.peek(); } /** Returns true iff the stack is empty. */ public boolean isEmpty() { return size == 0; } /** Returns a string representation of the stack. */ public String toString() { return list.toString(); } public static void main(String[] args) { int capacity = 15; StackLL is = new StackLL(); // int n = is.pop(); this should cause an exception // int n = is.top(); this should cause an exception if (is.isEmpty()) System.out.println("stack is empty"); else System.out.println("stack is not empty"); for (int i = 0; i < capacity; i++) is.push(i); is.push(capacity); System.out.println("stack before popping: " + is); System.out.println(is.pop()); System.out.println("stack after popping: " + is); System.out.println("top of stack is: " + is.top()); Stack ss = new Stack(capacity); ss.push("Now"); ss.push("is"); ss.push("the"); ss.push("time"); System.out.println(ss); } }