/**
* O(1) time.
*/
public int size() {
return (top + 1);
}
/**
* O(1) time.
*/
public boolean isEmpty() {
return (top < 0);
}
/**
* O(1) time.
* @exception StackFullException if the array is full.
*/
public void push(Object obj) throws StackFullException {
if (size() == capacity)
throw new StackFullException("Stack overflow.");
S[++top] = obj;
}
/**
* O(1) time.
*/
public Object top() throws StackEmptyException {
if (isEmpty())
throw new StackEmptyException("Stack is empty.");
return S[top];
}
/**
* O(1) time.
*/
public Object pop() throws StackEmptyException {
Object elem;
if (isEmpty())
throw new StackEmptyException("Stack is Empty.");
elem = S[top];
S[top--] = null; // dereference S[top] for garbage collection.
return elem;
}
}