public class IntegerStackLL { IntegerLinkedList list; private int size; public IntegerStackLL() { list = new IntegerLinkedList(); size = 0; } /** Pushes item onto the stack */ public void push(int item) { Node node = new Node(item, null); list.insert(node); size++; } /** Pops off an item from the stack */ public int pop() { if (isEmpty()) throw new RuntimeException("Trying to pop an empty stack."); size--; return list.delete(); } /** Returns the item at the top of the stack */ public int top() { if (isEmpty()) throw new RuntimeException("Trying to access an empty stack."); return 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; IntegerStackLL is = new IntegerStackLL(); // 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()); } }