Tuesday 18 March 2014

Java Program for Stack Demonstration

//stack demo
import java.util.*;

class StackDemo
{
    static void StackPush(Stack St,int a)
    {
        St.push(new Integer(a));
        System.out.println("push +a");
        System.out.println("stack:"+St);
    }
    static void Showpop(Stack St)
    {
       
        System.out.println("pop:-");
        Integer a=(Integer)St.pop();
        System.out.println(a);
    }
    public static void main(String args[])
    {
        Stack St=new Stack();
        System.out.println("Stack"+St);
        StackPush(St,42);
        StackPush(St,52);
        StackPush(St,62);
        StackPush(St,72);
        Showpop(St);
        Showpop(St);
        Showpop(St);
        Showpop(St);
   
        try
        {
            Showpop(St);
        }
        catch(EmptyStackException e)
        {
            System.out.println("stack underflow"+e);
        }
    }
}










       

No comments: