/**
  *
  * Der ADT Keller. 
  * Implementierung mit einer generischen Reihung.
  * 
  * @version 20.4.2019
  * @author Carsten Wieneke
  */
  
  import java.util.*;

public class Stack<Inhaltstyp> {
  
  private ArrayList<Inhaltstyp> k;    // Der Anfang des Kellers
  private int pos;
  
  
  /**
  * Konstruktor
  */
  public Stack(){
    pos = -1;
    k = new ArrayList<Inhaltstyp>();
  }
  
  /**
  * Keller leer?
  */
  public boolean isEmpty(){
    return k.size() == 0;
  }
  
  /**
  * Einkellern
  */
  public void push(Inhaltstyp x){
   k.add(x);
    pos++;
  }
  
  /**
  * Oberstes Element holen.
  */
  public Inhaltstyp top(){
    if (isEmpty()) {
      // Kotrollierter Abbruch
      throw new RuntimeException();
    } // end of if   
    return k.get(pos);
  }
  
  /**
  * Oberstes Element entfernen und zurückgeben.
  */
  public Inhaltstyp pop(){
    Inhaltstyp obj;
    if (isEmpty()) {
      throw new RuntimeException();
    }
    obj = k.get(pos);
    k.remove(pos);
    pos--;
    return obj;
  }
}
 
