miercuri, 6 iunie 2012

Stack

package lab3;



public interface Stack <T>{
   
    public boolean isEmpty (T[] a, int dim);
    public boolean isFull (T[] a, int dim);
    public void push(T k) throws StackFullException ;
    public boolean offer (T k);
    public T pop() throws StackEmptyException;
    public T pool ();
    public T peek() throws StackInvalidArgumentException;
    public T element ();
    }

package lab3;

public class ArrayStack<T> implements Stack<T> {

    private T[] a;
    private int dim;

    @SuppressWarnings("unchecked")
    public ArrayStack()
    {
    this.a=(T[])(new Object[5]);
    this.dim=-1;
    }

    public boolean isEmpty(T[] a, int dim)
    {
    if (-1==dim)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public boolean isFull(T[] a, int dim)
    {
    if (4==dim)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    public void push (T k) throws StackFullException
    {
    if (isFull(a,dim))
    throw new StackFullException();
    dim=dim+1;
    a[dim]=k;
    }

    public boolean offer(T k)
    {
    if (isFull(a,dim))
    {
    return false;
    }
    else
    {
    dim=dim+1;
    a[dim]=k;
    return true;
    }
    }

    public T pop() throws StackEmptyException
    {
    if (isEmpty(a,dim))
    {
    throw new StackEmptyException();
    }
    T k;
    k=a[dim];
    dim=dim-1;
    return k;
    }

    public T pool()
    {
    if (isEmpty(a,dim))
    {
    return null;
    }
    else
    {
    T k;
    k=a[dim];
    dim=dim-1;
    return k;
    }
    }

    public T element()
    {
    if (isEmpty(a,dim))
    {
    return null;
    }
    else
    {
    return a[dim];
    }
    }


    public T peek() throws StackInvalidArgumentException
    {
    if (isEmpty(a,dim))
    throw new StackInvalidArgumentException();
    return a[dim];
    }

    }




    package lab3;

public class StackEmptyException extends Throwable {

private static final long serialVersionUID = 1L;

public String getMessage()
{
return "Exceptie: Stiva goala!";
}
}






package lab3;

public class StackFullException extends Throwable {

    private static final long serialVersionUID = 1L;

    public String getMessage()
    {
    return "Exceptie: Stiva plina!";
    }

    }
   
package lab3;

public class StackInvalidArgumentException extends Throwable {

    private static final long serialVersionUID = 1L;

    public String getMessage()
    {
    return "Exceptie: Argumentul este invalid!";
    }


}
   
package lab3;

public class Test {

   
    public static void main(String args[])
    {
    Stack <Integer> mystack=new ArrayStack <Integer> ();
    Integer s=mystack.element();
    System.out.println(s);
    Integer s1=mystack.pool();
    System.out.println(s1);
    mystack.offer(10);
    System.out.println(mystack.element());
    System.out.println(mystack.pool());
    System.out.println(mystack.pool());

    try{
    Integer vf1;
    vf1=mystack.peek();
    System.out.println(vf1);
    }
    catch (StackInvalidArgumentException ex){
    System.out.println(ex.getMessage());
    }

    try{
    double vf2;
    vf2=mystack.pop();
    System.out.println(vf2);
    }
    catch (StackEmptyException ex){
    System.out.println(ex.getMessage());
    }

    try{
    mystack.offer(3);
    mystack.offer(5);
    mystack.offer(34);
    mystack.offer(86);
    mystack.offer(35);
    mystack.push(29);
    mystack.push(103);
    System.out.println(mystack.element());
    }
    catch (StackFullException ex){
    System.out.println(ex.getMessage());
    }
    }
}



   


   

   


   

Niciun comentariu:

Trimiteți un comentariu