From e3059ed799830a9df939083b14eefb1f174b5cec Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Sun, 23 Mar 2014 22:00:57 +0100 Subject: [PATCH] =?UTF-8?q?eliminato=20classi=20superflue=20del=20progetto?= =?UTF-8?q?=20e=20risolto=20qualche=20bug.=20c'e=CC=80=20ancora=20del=20la?= =?UTF-8?q?voro=20da=20fare=20sulla=20queue=20e=20deque?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- com/xgiovio/ArrayTest.java | 45 +++++++++++++ exceptions/FullQueueException.java | 14 ---- exceptions/FullStackException.java | 14 ---- queue/ArrayQueue.java | 6 +- queue/ArrayQueueNoBlank.java | 103 ----------------------------- queue/FixedArrayQueue.java | 103 ----------------------------- stack/ArrayStack.java | 32 +++++++-- stack/FixedArrayStack.java | 74 --------------------- stack/NodeStack.java | 21 +++++- stack/Stack.java | 4 +- 10 files changed, 94 insertions(+), 322 deletions(-) create mode 100644 com/xgiovio/ArrayTest.java delete mode 100644 exceptions/FullQueueException.java delete mode 100644 exceptions/FullStackException.java delete mode 100644 queue/ArrayQueueNoBlank.java delete mode 100644 queue/FixedArrayQueue.java delete mode 100644 stack/FixedArrayStack.java diff --git a/com/xgiovio/ArrayTest.java b/com/xgiovio/ArrayTest.java new file mode 100644 index 0000000..5ce25a5 --- /dev/null +++ b/com/xgiovio/ArrayTest.java @@ -0,0 +1,45 @@ +package com.xgiovio; + +import general_utility.test_object; +import stack.ArrayStack; +import stack.FixedArrayStack; +import stack.NodeStack; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 23/03/14 + * Time: 20:37 + */ +public class ArrayTest { + + public static void main(String[] args) { + NodeStack a = new NodeStack(); + a.push(new test_object(10)); + a.push(new test_object(20)); + a.push(new test_object(30)); + a.push(new test_object(40)); + + + + a.pop(); + a.pop(); + + + System.out.print(a.toString()); + + a.push(new test_object(50)); + a.push(new test_object(60)); + + System.out.print(a.toString()); + a.pop(); + System.out.print(a.toString()); + System.out.print(a.top().toString()); + + + + + + } + +} diff --git a/exceptions/FullQueueException.java b/exceptions/FullQueueException.java deleted file mode 100644 index 1f59073..0000000 --- a/exceptions/FullQueueException.java +++ /dev/null @@ -1,14 +0,0 @@ -package exceptions; - -/** - * Created with MONSTER. - * User: xgiovio - * Date: 05/03/14 - * Time: 0.09 - */ -public class FullQueueException extends RuntimeException { - - public FullQueueException(){ - super("Queue Full"); - } -} diff --git a/exceptions/FullStackException.java b/exceptions/FullStackException.java deleted file mode 100644 index 2df4f7a..0000000 --- a/exceptions/FullStackException.java +++ /dev/null @@ -1,14 +0,0 @@ -package exceptions; - -/** - * Created with MONSTER. - * User: xgiovio - * Date: 05/03/14 - * Time: 0.09 - */ -public class FullStackException extends RuntimeException { - - public FullStackException(){ - super("Stack Full"); - } -} diff --git a/queue/ArrayQueue.java b/queue/ArrayQueue.java index 1f8e2b0..c84e67b 100644 --- a/queue/ArrayQueue.java +++ b/queue/ArrayQueue.java @@ -26,14 +26,14 @@ public class ArrayQueue implements Queue { } queue[rear]= element; rear = (rear + 1) % queue.length; - if ( rear == front ){ + if ( rear ==( front - 1) ){ //reallocate; E[] new_queue = (E[]) new Object[queue.length * 2]; - for (int i= 0 ;i implements Queue { - - public ArrayQueueNoBlank(){ - queue = (E[])new Object[def_size]; - } - - public ArrayQueueNoBlank(int in_size){ - queue = (E[])new Object[in_size]; - } - - - @Override - public void enqueue(E element) { - if (front == -1){ - front++; - } - queue[rear]= element; - rear = (rear + 1) % queue.length; - if ( rear == front ){ - //reallocate; - E[] new_queue = (E[]) new Object[queue.length * 2]; - for (int i= 0 ;i implements Queue { - - public FixedArrayQueue(){ - queue = (E[])new Object[def_size]; - } - - public FixedArrayQueue(int in_size){ - queue = (E[])new Object[in_size]; - } - - - @Override - public void enqueue(E element) { - if (front == -1){ - front++; - } - queue[rear]= element; - rear = (rear + 1) % queue.length; - if ( rear == front ){ - //reallocate; - E[] new_queue = (E[]) new Object[queue.length * 2]; - for (int i= 0 ;i implements Stack { @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 implements Stack { 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 implements Stack { @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; diff --git a/stack/FixedArrayStack.java b/stack/FixedArrayStack.java deleted file mode 100644 index a19da7d..0000000 --- a/stack/FixedArrayStack.java +++ /dev/null @@ -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 implements Stack { - - - 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; - -} diff --git a/stack/NodeStack.java b/stack/NodeStack.java index ef81720..81efa08 100644 --- a/stack/NodeStack.java +++ b/stack/NodeStack.java @@ -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 implements Stack { @Override - public void push(E element) throws FullStackException { + public void push(E element) { Node temp = new Node(element,stack); stack = temp; ++number_of_elements; @@ -60,6 +59,24 @@ public class NodeStack implements Stack { return number_of_elements; } + @Override + public String toString() { + String to_return = ""; + to_return = to_return + "["; + + for ( Node 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 stack = null; private int number_of_elements = 0; diff --git a/stack/Stack.java b/stack/Stack.java index 73d9543..a181cb8 100644 --- a/stack/Stack.java +++ b/stack/Stack.java @@ -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 { - 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();