The Stack ADT

Solution

A stack is a collection where a client can work only with the most recently added item, called the top. push adds an item on top. pop removes the top item. top returns the top item without removing it. isEmpty reports whether the stack has no items.

Solution

Last in, first out. The items leave in the order 3, 2, 1.

Solution

pop and top. There is no top item to remove or return, so the implementation should throw an exception.

Solution
public static int pop(Stack<Integer> stack) {
  int value = stack.top();
  stack.pop();
  return value;
}
Solution

Each undo needs the most recently recorded action, so the actions are reversed last in, first out. A stack gives access only to the most recent item, which is all undo needs, and no part of the program can add, remove, or reorder actions anywhere else in the record.