public class PileSol {
  double haut ;
  PileSol suite ;

  PileSol (double elt, PileSol old) {
    haut = elt ;
    suite = old ;
  }

  private static void error (String msg) {
    System.err.println (msg) ;
    System.exit (2) ;
  }

  static PileSol push (PileSol pile, double elt) {
    // empile un réel
    return new PileSol (elt, pile) ;
  }

  static double top (PileSol pile) {
    // haut de pile
    if (pile == null)
      error ("Top sur pile vide") ;
    return pile.haut ;
  }

  static PileSol pop (PileSol pile) {
    // dépile
    if (pile == null)
      error ("Pop sur pile vide") ;
    return pile.suite ;
  }

  static String toString (PileSol pile, int rang) {
    // impression du contenu de la pile
    // version récursive
    if (pile == null) return "" ;
    else              return toString (pile.suite, rang + 1)
                             + rang + ": " + pile.haut + "\n" ;
  }

  static String toString (PileSol pile) {
    // impression du contenu de la pile
    // version itérative
    String out = "" ;
    int rang = 0;
    while (pile != null) {
      out = rang + ": " + pile.haut + "\n" + out ;
      pile = pile.suite ;
      rang++ ;
    }
    return out;
  }

  public static void main (String[] args) {
    PileSol p = null;
    p = push (p, 1.2) ;
    p = push (p, 1.5) ;
    p = pop (p) ;
    System.out.print (toString (p,0)) ;
    System.out.print (toString (p)) ;
    p = pop (p) ;
    p = pop (p) ;
  }
}
