Friday 25 October 2013

Stack using Array

class Stack
{
    int a[],top,l;
    Stack(int c)
    {
        a=new int[c];l=c;
        top=0;
    }
    void push(int n)
    {
        if(top==l)
        {
            System.out.println("Overflow");
            top=l-1;
        }
        else
        {
            a[top]=n;
            top=top+1;
            System.out.println("Element "+n+" pushed");
        }
    }
    void pop()
    {
        if(top==-1)
        {
            top=0;
            System.out.println("Underflow");
        }
        else
        {
            System.out.println("Element "+a[top]+" popped");
            top=top-1;
        }
    }
    void display()
    {
        System.out.println("Current status of stack ");
        for(int i=0;i<=top;i++)
        {
            System.out.print(a[i]+" ");
        }
    }
}

No comments:

Post a Comment