import java.util.LinkedList; import java.util.List; /** * A stack of objects of type T. * @author Peter Cappello */ public class Stack { private final List stack = new LinkedList<>(); /** * Answers the question is the stack empty. * @return true if and only if it is empty. */ public boolean isEmpty() { return stack.isEmpty(); } /** * Removes and retrieves the stack's top element. * @return the top of the stack. * @throws IndexOutOfBoundsException when the stack is empty. */ public T pop() { return stack.remove( 0 ); } /** * Inserts an element into the stack. * @param element */ public void push( T element ) { stack.add( 0, element ); } /** * Retrieves but does not remove the top element of the stack. * @return the top element of the stack. * @throws IndexOutOfBoundsException when the stack is empty. */ public T peek() { return stack.get( 0 ); } }