eliminato classi superflue del progetto e risolto qualche bug. c'è ancora del lavoro da fare sulla queue e deque
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
@@ -27,16 +27,18 @@ public class ArrayStack<E> implements Stack<E> {
|
||||
|
||||
|
||||
@Override
|
||||
public void push(E element) throws FullStackException {
|
||||
if (isFull()){
|
||||
public void push(E element) {
|
||||
if (stack.length == (top + 1)){
|
||||
E[] stack2 = (E[])new Object[(stack.length *2)];
|
||||
for (int i = 0; i<= top; i++){
|
||||
stack2[i] = stack[i];
|
||||
}
|
||||
stack = stack2;
|
||||
stack[++top] = element;
|
||||
top++;
|
||||
stack[top] = element;
|
||||
}else{
|
||||
stack[++top] = element;
|
||||
top++;
|
||||
stack[top] = element;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +53,8 @@ public class ArrayStack<E> implements Stack<E> {
|
||||
public E pop() throws EmptyStackException {
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
return stack[top--];
|
||||
top--;
|
||||
return stack[top + 1];
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,9 +72,24 @@ public class ArrayStack<E> implements Stack<E> {
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return (++top);
|
||||
return (top + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String to_return = "";
|
||||
to_return = to_return + "[";
|
||||
for (int i = 0;i<=top;i++){
|
||||
if ( i == top){
|
||||
to_return+=(stack[i].toString());
|
||||
}else{
|
||||
to_return+=(stack[i].toString() + ",");
|
||||
}
|
||||
}
|
||||
to_return = to_return + "]";
|
||||
|
||||
return to_return;
|
||||
}
|
||||
|
||||
private int top = -1;
|
||||
private int dcapacity = 100;
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 05/03/14
|
||||
* Time: 0.12
|
||||
*/
|
||||
|
||||
|
||||
public class FixedArrayStack<E> implements Stack<E> {
|
||||
|
||||
|
||||
public FixedArrayStack(){
|
||||
|
||||
stack = (E[])new Object[dcapacity];
|
||||
|
||||
}
|
||||
public FixedArrayStack(int size){
|
||||
|
||||
stack = (E[])new Object[size];
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void push(E element) throws FullStackException {
|
||||
if (isFull())
|
||||
throw new FullStackException();
|
||||
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
|
||||
public boolean isFull() {
|
||||
if (stack.length == (top + 1))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return (++top);
|
||||
}
|
||||
|
||||
|
||||
private int top = -1;
|
||||
private int dcapacity = 100;
|
||||
private E[] stack;
|
||||
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
import stack.utility.Node;
|
||||
|
||||
/**
|
||||
@@ -15,7 +14,7 @@ import stack.utility.Node;
|
||||
public class NodeStack<E> implements Stack<E> {
|
||||
|
||||
@Override
|
||||
public void push(E element) throws FullStackException {
|
||||
public void push(E element) {
|
||||
Node<E> temp = new Node<E>(element,stack);
|
||||
stack = temp;
|
||||
++number_of_elements;
|
||||
@@ -60,6 +59,24 @@ public class NodeStack<E> implements Stack<E> {
|
||||
return number_of_elements;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String to_return = "";
|
||||
to_return = to_return + "[";
|
||||
|
||||
for ( Node<E> temp = stack; temp != null ; temp = temp.getNext()){
|
||||
|
||||
if ( temp.getNext() == null){
|
||||
to_return+=(temp.getElement().toString());
|
||||
}else{
|
||||
to_return+=( temp.getElement().toString() + ",");
|
||||
}
|
||||
}
|
||||
to_return = to_return + "]";
|
||||
|
||||
return to_return;
|
||||
}
|
||||
|
||||
|
||||
private Node<E> stack = null;
|
||||
private int number_of_elements = 0;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package stack;
|
||||
|
||||
import exceptions.EmptyStackException;
|
||||
import exceptions.FullStackException;
|
||||
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
@@ -11,7 +11,7 @@ import exceptions.FullStackException;
|
||||
*/
|
||||
public interface Stack<E> {
|
||||
|
||||
public void push (E element) throws FullStackException;
|
||||
public void push (E element) ;
|
||||
public E top () throws EmptyStackException;
|
||||
public E pop () throws EmptyStackException;
|
||||
public boolean isEmpty();
|
||||
|
||||
Reference in New Issue
Block a user