/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jeccezioni; import java.util.*; /** * Classe Pila di interi * @author gabriele */ public class Pila { private ArrayList stack = new ArrayList(); /** * Costruisce una Pila vuota */ public Pila(){ }; /** * Conta gli elementi nella Pila * @return il numero di elementi nella Pila */ public int size(){ return stack.size();} /** * Inserisce un elemento nella pila * @param x elemento da inserire */ public void push(int x){ stack.add(x);} /** * Restituisce l'elemento in cima alla pila * @return l'elemento in cima alla pila * @throws EmptyStackException */ public int top() throws EmptyStackException { if (stack.size()==0) throw new EmptyStackException(); else return stack.get(stack.size()-1); } /** * Rimuove l'elemento in cima alla pila * @throws EmptyStackException */ public void pop() throws EmptyStackException{ if (stack.size()==0) throw new EmptyStackException(); else stack.remove(stack.size()-1); } }