Saturday 23 June 2012

java program to maintain stack using java.util.Stack class.

import java.io.*;
import java.util.*;
class StackArray
{
    Stack s=new Stack();
    public void push(int i) throws Exception
    {
        s.push(new Integer(i));
       
    }

   public void pop() throws Exception
    {
        if(s.empty())
            System.out.println("array Underflow");
        else
            s.pop();   
    }
    public void display() throws Exception
    {
        for(Iterator i=s.iterator();i.hasNext();)
            System.out.println(i.next());
   
    }
}
class MainStack
{
    public static void main(String args[])
    {    try
        {
        StackArra

y s=new StackArray();
        int c;
        s.push(1);
        s.push(2);
        s.push(3);
        s.display();
        s.pop();
        s.display();
        }
        catch(Exception e)
        {
        System.out.println(e);   
        }   
    }
}

No comments:

Post a Comment