67 lines
1.3 KiB
Java
67 lines
1.3 KiB
Java
package stack;
|
|
|
|
import exceptions.EmpyStackException;
|
|
import exceptions.FullStackException;
|
|
import stack.utility.SinglePointerNode;
|
|
|
|
/**
|
|
* Created with MONSTER.
|
|
* User: xgiovio
|
|
* Date: 05/03/14
|
|
* Time: 0.12
|
|
*/
|
|
|
|
|
|
public class SinglePointerStack<E> extends StackRules<E> {
|
|
|
|
@Override
|
|
public void push(E element) throws FullStackException {
|
|
SinglePointerNode<E> temp = new SinglePointerNode<E>(element,stack);
|
|
stack = temp;
|
|
++number_of_elements;
|
|
}
|
|
|
|
|
|
@Override
|
|
public E top() throws EmpyStackException {
|
|
if (isEmpty())
|
|
throw new EmpyStackException();
|
|
return stack.getElement();
|
|
}
|
|
|
|
@Override
|
|
public E pop() throws EmpyStackException {
|
|
SinglePointerNode<E> to_return;
|
|
if (isEmpty()) {
|
|
throw new EmpyStackException();
|
|
}else{
|
|
to_return = stack;
|
|
stack = stack.getNext();
|
|
return to_return.getElement();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
if (size() < 1)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
// not used
|
|
public boolean isFull() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return number_of_elements;
|
|
}
|
|
|
|
|
|
private SinglePointerNode<E> stack = null;
|
|
private int number_of_elements = 0;
|
|
|
|
}
|