Tuesday 12 January 2016

Java program for Stack implementation using vectors

Source Code:

import java.util.*;
class UnderFlowException extends Exception
{
public UnderFlowException(String s)
{
super(s);
}
}
class MyStack
{ private int i;
Vector v;
public MyStack()
{
i=-1;
v= new Vector();
}
public void push(Object o)
{

v.add(++i,o);
}
public Object pop() throws UnderFlowException
{
if(i>=0)
return v.get(i--);
else
{
UnderFlowException e=new UnderFlowException("** Stack Overflow **");
throw e;
}
}
public boolean empty()
{
return(i==-1);
}
}

No comments:

Post a Comment