// Paweł Rzechonek (c) October 2005
// klasa reprezentująca stos dla liczb całkowitych
// (klonowanie)

package struktury;

public class StosLiczb implements Cloneable
{
    protected int[] stos;
    protected int szczyt = 0;

    public StosLiczb (int rozm)
    {
        if (rozm<1) throw new IllegalArgumentException();
        stos = new int[rozm];
    }

    public int ile ()
    {
        return szczyt;
    }
    public void wstaw (int x)
    {
        if (szczyt==stos.length) throw new IllegalStateException();
        stos[szczyt++] = x;
    }
    public int usun ()
    {
        if (szczyt==0) throw new IllegalStateException();
        return stos[--szczyt];
    }
    public int sprawdz ()
    {
        if (szczyt==0) throw new IllegalStateException();
        return stos[szczyt-1];
    }

    public Object clone ()
    {
        try
        {
            StosLiczb sl = (StosLiczb)super.clone();
            sl.stos = (int[])stos.clone();
            return sl;
        }
        catch (CloneNotSupportedException ex)
        {
            return null;
        }
    }
    
    public String toString ()
    {
        StringBuffer buf = new StringBuffer("[");
        for (int i=0; i<szczyt; i++) buf.append(i>0?", ":"").append(stos[i]);
        buf.append("]");
        return buf.toString();
    }
}
