Files
unisa_strutture_dati_2013_2014/stack/FixedArrayStack.java

75 lines
1.3 KiB
Java

package stack;
import exceptions.EmptyStackException;
import exceptions.FullStackException;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 05/03/14
* Time: 0.12
*/
public class FixedArrayStack<E> implements Stack<E> {
public FixedArrayStack(){
stack = (E[])new Object[dcapacity];
}
public FixedArrayStack(int size){
stack = (E[])new Object[size];
}
@Override
public void push(E element) throws FullStackException {
if (isFull())
throw new FullStackException();
stack[++top] = element;
}
@Override
public E top() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException();
return stack[top];
}
@Override
public E pop() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException();
return stack[top--];
}
@Override
public boolean isEmpty() {
if (size() < 1)
return true;
return false;
}
@Override
public boolean isFull() {
if (stack.length == (top + 1))
return true;
return false;
}
@Override
public int size() {
return (++top);
}
private int top = -1;
private int dcapacity = 100;
private E[] stack;
}