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

80
stack/ArrayStack.java Normal file
View File

@@ -0,0 +1,80 @@
package stack;
import exceptions.EmptyStackException;
import exceptions.FullStackException;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 05/03/14
* Time: 0.12
*/
public class ArrayStack<E> implements Stack<E> {
public ArrayStack(){
stack = (E[])new Object[dcapacity];
}
public ArrayStack(int size){
stack = (E[])new Object[size];
}
@Override
public void push(E element) throws FullStackException {
if (isFull()){
E[] stack2 = (E[])new Object[(stack.length *2)];
for (int i = 0; i<= top; i++){
stack2[i] = stack[i];
}
stack = stack2;
stack[++top] = element;
}else{
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
// not used
public boolean isFull() {
return false;
}
@Override
public int size() {
return (++top);
}
private int top = -1;
private int dcapacity = 100;
private E[] stack;
}