Files
unisa_strutture_dati_2013_2014/stack/FixedArrayStack.java
2014-03-05 01:13:42 +01:00

75 lines
1.3 KiB
Java

package stack;
import exceptions.EmpyStackException;
import exceptions.FullStackException;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 05/03/14
* Time: 0.12
*/
public class FixedArrayStack<E> extends StackRules<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 EmpyStackException {
if (isEmpty())
throw new EmpyStackException();
return stack[top];
}
@Override
public E pop() throws EmpyStackException {
if (isEmpty())
throw new EmpyStackException();
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;
}