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:
@@ -1,6 +1,6 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmpyStackException;
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
|
||||
/**
|
||||
@@ -11,15 +11,15 @@ import exceptions.FullStackException;
|
||||
*/
|
||||
|
||||
|
||||
public class DynamicArrayStack<E> extends StackRules<E> {
|
||||
public class ArrayStack<E> implements Stack<E> {
|
||||
|
||||
|
||||
public DynamicArrayStack(){
|
||||
public ArrayStack(){
|
||||
|
||||
stack = (E[])new Object[dcapacity];
|
||||
|
||||
}
|
||||
public DynamicArrayStack(int size){
|
||||
public ArrayStack(int size){
|
||||
|
||||
stack = (E[])new Object[size];
|
||||
|
||||
@@ -41,16 +41,16 @@ public class DynamicArrayStack<E> extends StackRules<E> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public E top() throws EmpyStackException {
|
||||
public E top() throws EmptyStackException {
|
||||
if (isEmpty())
|
||||
throw new EmpyStackException();
|
||||
throw new EmptyStackException();
|
||||
return stack[top];
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() throws EmpyStackException {
|
||||
public E pop() throws EmptyStackException {
|
||||
if (isEmpty())
|
||||
throw new EmpyStackException();
|
||||
throw new EmptyStackException();
|
||||
return stack[top--];
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmpyStackException;
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
|
||||
/**
|
||||
@@ -11,15 +11,15 @@ import exceptions.FullStackException;
|
||||
*/
|
||||
|
||||
|
||||
public class FixedArrayStack<E> extends StackRules<E> {
|
||||
public class FixedArrayStack<E> implements Stack<E> {
|
||||
|
||||
|
||||
public FixedArrayStack(){
|
||||
public FixedArrayStack(){
|
||||
|
||||
stack = (E[])new Object[dcapacity];
|
||||
|
||||
}
|
||||
public FixedArrayStack( int size){
|
||||
public FixedArrayStack(int size){
|
||||
|
||||
stack = (E[])new Object[size];
|
||||
|
||||
@@ -34,16 +34,16 @@ public class FixedArrayStack<E> extends StackRules<E> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public E top() throws EmpyStackException {
|
||||
public E top() throws EmptyStackException {
|
||||
if (isEmpty())
|
||||
throw new EmpyStackException();
|
||||
throw new EmptyStackException();
|
||||
return stack[top];
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() throws EmpyStackException {
|
||||
public E pop() throws EmptyStackException {
|
||||
if (isEmpty())
|
||||
throw new EmpyStackException();
|
||||
throw new EmptyStackException();
|
||||
return stack[top--];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmpyStackException;
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
import stack.utility.SinglePointerNode;
|
||||
import stack.utility.Node;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
@@ -12,28 +12,28 @@ import stack.utility.SinglePointerNode;
|
||||
*/
|
||||
|
||||
|
||||
public class SinglePointerStack<E> extends StackRules<E> {
|
||||
public class NodeStack<E> implements Stack<E> {
|
||||
|
||||
@Override
|
||||
public void push(E element) throws FullStackException {
|
||||
SinglePointerNode<E> temp = new SinglePointerNode<E>(element,stack);
|
||||
Node<E> temp = new Node<E>(element,stack);
|
||||
stack = temp;
|
||||
++number_of_elements;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public E top() throws EmpyStackException {
|
||||
public E top() throws EmptyStackException {
|
||||
if (isEmpty())
|
||||
throw new EmpyStackException();
|
||||
throw new EmptyStackException();
|
||||
return stack.getElement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() throws EmpyStackException {
|
||||
SinglePointerNode<E> to_return;
|
||||
public E pop() throws EmptyStackException {
|
||||
Node<E> to_return;
|
||||
if (isEmpty()) {
|
||||
throw new EmpyStackException();
|
||||
throw new EmptyStackException();
|
||||
}else{
|
||||
to_return = stack;
|
||||
stack = stack.getNext();
|
||||
@@ -61,7 +61,7 @@ public class SinglePointerStack<E> extends StackRules<E> {
|
||||
}
|
||||
|
||||
|
||||
private SinglePointerNode<E> stack = null;
|
||||
private Node<E> stack = null;
|
||||
private int number_of_elements = 0;
|
||||
|
||||
}
|
||||
21
stack/Stack.java
Normal file
21
stack/Stack.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 05/03/14
|
||||
* Time: 0.06
|
||||
*/
|
||||
public interface Stack<E> {
|
||||
|
||||
public void push (E element) throws FullStackException;
|
||||
public E top () throws EmptyStackException;
|
||||
public E pop () throws EmptyStackException;
|
||||
public boolean isEmpty();
|
||||
public boolean isFull();
|
||||
public int size();
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmpyStackException;
|
||||
import exceptions.FullStackException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 05/03/14
|
||||
* Time: 0.06
|
||||
*/
|
||||
public abstract class StackRules <E> {
|
||||
|
||||
public abstract void push (E element) throws FullStackException;
|
||||
public abstract E top () throws EmpyStackException;
|
||||
public abstract E pop () throws EmpyStackException;
|
||||
public abstract boolean isEmpty();
|
||||
public abstract boolean isFull();
|
||||
public abstract int size();
|
||||
|
||||
}
|
||||
@@ -6,15 +6,15 @@ package stack.utility;
|
||||
* Date: 05/03/14
|
||||
* Time: 0.49
|
||||
*/
|
||||
public class SinglePointerNode<E> {
|
||||
public class Node<E> {
|
||||
|
||||
public SinglePointerNode (){
|
||||
public Node(){
|
||||
element = null;
|
||||
next = null;
|
||||
|
||||
}
|
||||
|
||||
public SinglePointerNode (E in_element, SinglePointerNode<E> in_next){
|
||||
public Node(E in_element, Node<E> in_next){
|
||||
element = in_element;
|
||||
next = in_next;
|
||||
}
|
||||
@@ -27,15 +27,15 @@ public class SinglePointerNode<E> {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public SinglePointerNode<E> getNext() {
|
||||
public Node<E> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(SinglePointerNode<E> next) {
|
||||
public void setNext(Node<E> next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
private E element;
|
||||
private SinglePointerNode<E> next;
|
||||
private Node<E> next;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user