Refactor di tutto lo stack. Manca l'esercizio sulle operazioni matematiche via stack. Sulla coda bisogna implementare da zero FixedArrayQueue e poi la relativa versione senza fullexception in ArrayQueue. Esercizi!

This commit is contained in:
2014-03-17 12:11:39 +01:00
parent d60e74a09c
commit 08069c6b46
17 changed files with 318 additions and 95 deletions

67
stack/NodeStack.java Normal file
View File

@@ -0,0 +1,67 @@
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;
}