public class Stack<E> {
  private class Node<E> {
    E data;
    Node<E> link;
    public Node(E d, Node<E> l) {
      data = d;
      link = l;
    }
  }
  private Node<E> top;
  public void add(E e) {
    Node<E> n = new Node<E>(e, this.top);
    this.top = n;
  }
  public boolean hasMore() {
    return this.top != null;
  }
  public E remove() {
    Node<E> aux = this.top;
    this.top = aux.link;
    return aux.data;
  }
  public static void main(String args[]) {
    Stack<String> s = new Stack<String>();
    s.add("AA");
    s.add("BB");
    s.add("CC");
    while (s.hasMore()) {
      System.out.println(s.remove());
    }
  }
}
