The code that I wrote is as follows:
public class Astack implements StackInterface {
private int sz;
private Object[] a;
private int topPosition = -1;
public Astack(int sz){
a = new Object[sz];
this.sz = sz;
}
public int size(){
return topPosition+1;
}
public boolean isEmpty(){
if(topPosition == -1){
return true;
}
return false;
}
public void push(Object element) throws StackFullException{
if(topPosition+1 == sz){
throw new StackFullException("Stack Full.");
}
a[++topPosition] = element;
}
public Object top() throws StackEmptyException{
if(topPosition == -1){
throw new StackEmptyException("Stack already Empty");
}
return a[topPosition];
}
public Object pop() throws StackEmptyException{
if(topPosition == -1){
throw new StackEmptyException("Stack already Empty");
}
return a[topPosition--];
}
}
The StackInterface
that I wrote:
public interface StackInterface{
public int size();
public boolean isEmpty();
public Object top() throws StackEmptyException;
public void push(Object element) throws StackFullException;
public Object pop() throws StackEmptyException;
}
StackInterface
given to you, or did you write it too? Could you also include it in the question? – 200_success♦ yesterdayStackInterface
. I just wanted to use stack the traditional way than using Stack java class. I have added the interface that i wrote in the question above. – Abhay_maniyar yesterday