An instance of the stack data structure is a sequence of elements (objects) with one end designated as the top of the stack:

Main stack operations (LIFO):

  • push(e): insert element at the top of the stack
  • pop(): remove and return the element at the top of the stack Throws an error if stack is empty.

Additional stack operations:

  • top(): return the top element in the stack without removing it Throws an error if stack is empty.
  • size(): return the number of elements stored
  • isEmpty(): check if the stack is empty

Java Stack Interface

Below is a Java interface according to our stack ADT:

public interface Stack<E> {
	public void push(E element);
	public E pop() throws EmptyStackException;
	public E top() throws EmptyStackException;
	public int size();
	public boolean isEmpty();
}