68 lines
1.3 KiB
Java
68 lines
1.3 KiB
Java
package stack;
|
|
|
|
import exceptions.EmptyStackException;
|
|
import exceptions.FullStackException;
|
|
import stack.utility.Node;
|
|
|
|
/**
|
|
* Created with MONSTER.
|
|
* User: xgiovio
|
|
* Date: 05/03/14
|
|
* Time: 0.12
|
|
*/
|
|
|
|
|
|
public class NodeStack<E> implements Stack<E> {
|
|
|
|
@Override
|
|
public void push(E element) throws FullStackException {
|
|
Node<E> temp = new Node<E>(element,stack);
|
|
stack = temp;
|
|
++number_of_elements;
|
|
}
|
|
|
|
|
|
@Override
|
|
public E top() throws EmptyStackException {
|
|
if (isEmpty())
|
|
throw new EmptyStackException();
|
|
return stack.getElement();
|
|
}
|
|
|
|
@Override
|
|
public E pop() throws EmptyStackException {
|
|
Node<E> to_return;
|
|
if (isEmpty()) {
|
|
throw new EmptyStackException();
|
|
}else{
|
|
to_return = stack;
|
|
stack = stack.getNext();
|
|
--number_of_elements;
|
|
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 Node<E> stack = null;
|
|
private int number_of_elements = 0;
|
|
|
|
}
|