
H - 1!
A Stack as an Abstract Data Type!
• A stack
– Last-in, first-out (LIFO) property
• The last item placed on the stack
will be the first item removed
– Analogy
• A stack of dishes in a cafeteria
• vs: A queue
– First in, first out (FIFO) property
• The first item added is the first
item to be removed!
Stack of cafeteria dishes
(vertical slice view)
H - 2!
15.1 – Stacks!
1-2!
Top of stack
Adding an
element
Removing
an element
H - 3!
Developing the Stack ADT
• ADT stack operations
– Create an empty stack
– Determine whether a stack is empty
– Add a new item to the stack
– Remove from the stack the item that was
added most recently
– Retrieve (but not remove) from the stack
the item that was added most recently!
H - 4!
public interface StackInterface<E>!
/** Determines whether stack is empty.!
*/!
public boolean isEmpty();!
/** Adds an item to the top of a stack.!
*/!
public void push(E newItem);!
/** Removes the top of a stack.!
* Exception: Throws StackException if the stack is empty. */!
public E pop() throws StackException;!
/** Retrieves the top of a stack.!
* Exception: Throws StackException if the stack is empty. */ !
public E peek() throws StackException;!
}!
{!