From 08069c6b46493b48aa8eee200b137a578e8427c3 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Mon, 17 Mar 2014 12:11:39 +0100 Subject: [PATCH] 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! --- com/xgiovio/QueueTest.java | 4 +- com/xgiovio/StackCheckParenthesis.java | 8 +- com/xgiovio/StackInvertString.java | 5 +- ...xception.java => EmptyQueueException.java} | 4 +- ...xception.java => EmptyStackException.java} | 4 +- exceptions/FullQueueException.java | 14 +++ ...DynamicArrayQueue.java => ArrayQueue.java} | 22 ++-- queue/ArrayQueueNoBlank.java | 103 ++++++++++++++++++ queue/FixedArrayQueue.java | 103 ++++++++++++++++++ queue/Queue.java | 20 ++++ queue/QueueRules.java | 20 ---- ...DynamicArrayStack.java => ArrayStack.java} | 16 +-- stack/FixedArrayStack.java | 16 +-- ...SinglePointerStack.java => NodeStack.java} | 20 ++-- stack/Stack.java | 21 ++++ stack/StackRules.java | 21 ---- .../{SinglePointerNode.java => Node.java} | 12 +- 17 files changed, 318 insertions(+), 95 deletions(-) rename exceptions/{EmpyQueueException.java => EmptyQueueException.java} (60%) rename exceptions/{EmpyStackException.java => EmptyStackException.java} (60%) create mode 100644 exceptions/FullQueueException.java rename queue/{DynamicArrayQueue.java => ArrayQueue.java} (80%) create mode 100644 queue/ArrayQueueNoBlank.java create mode 100644 queue/FixedArrayQueue.java create mode 100644 queue/Queue.java delete mode 100644 queue/QueueRules.java rename stack/{DynamicArrayStack.java => ArrayStack.java} (77%) rename stack/{SinglePointerStack.java => NodeStack.java} (65%) create mode 100644 stack/Stack.java delete mode 100644 stack/StackRules.java rename stack/utility/{SinglePointerNode.java => Node.java} (61%) diff --git a/com/xgiovio/QueueTest.java b/com/xgiovio/QueueTest.java index fb25849..4d0257e 100644 --- a/com/xgiovio/QueueTest.java +++ b/com/xgiovio/QueueTest.java @@ -1,7 +1,7 @@ package com.xgiovio; import general_utility.test_object; -import queue.DynamicArrayQueue; +import queue.ArrayQueueNoBlank; /** * Created with xgiovio.macbookair. @@ -17,7 +17,7 @@ public class QueueTest { - DynamicArrayQueue queue = new DynamicArrayQueue(5); + ArrayQueueNoBlank queue = new ArrayQueueNoBlank(5); queue.enqueue(new test_object(1)); System.out.println(queue.front().get_data()); diff --git a/com/xgiovio/StackCheckParenthesis.java b/com/xgiovio/StackCheckParenthesis.java index 12ec6b4..b024d30 100644 --- a/com/xgiovio/StackCheckParenthesis.java +++ b/com/xgiovio/StackCheckParenthesis.java @@ -1,7 +1,7 @@ package com.xgiovio; -import exceptions.EmpyStackException; -import stack.SinglePointerStack; +import exceptions.EmptyStackException; +import stack.NodeStack; import java.util.Scanner; @@ -16,7 +16,7 @@ public class StackCheckParenthesis { String word = reader.nextLine(); boolean status = true; - SinglePointerStack stack = new SinglePointerStack(); + NodeStack stack = new NodeStack(); try{ for (int i = 0;i< word.length();i++){ @@ -72,7 +72,7 @@ public class StackCheckParenthesis { System.out.println("Stringa errata"); } } - catch (EmpyStackException e){ + catch (EmptyStackException e){ System.out.println("Stringa errata"); } } diff --git a/com/xgiovio/StackInvertString.java b/com/xgiovio/StackInvertString.java index 6ea2cb0..3542b84 100644 --- a/com/xgiovio/StackInvertString.java +++ b/com/xgiovio/StackInvertString.java @@ -1,7 +1,6 @@ package com.xgiovio; -import exceptions.EmpyStackException; -import stack.SinglePointerStack; +import stack.NodeStack; import java.util.Scanner; @@ -16,7 +15,7 @@ public class StackInvertString { String word = reader.nextLine(); String word_inverted = ""; - SinglePointerStack stack = new SinglePointerStack(); + NodeStack stack = new NodeStack(); for (int i = 0;i< word.length();i++){ stack.push(word.charAt(i)); } diff --git a/exceptions/EmpyQueueException.java b/exceptions/EmptyQueueException.java similarity index 60% rename from exceptions/EmpyQueueException.java rename to exceptions/EmptyQueueException.java index ce3c20f..0a7c7e1 100644 --- a/exceptions/EmpyQueueException.java +++ b/exceptions/EmptyQueueException.java @@ -6,9 +6,9 @@ package exceptions; * Date: 05/03/14 * Time: 0.09 */ -public class EmpyQueueException extends RuntimeException { +public class EmptyQueueException extends RuntimeException { - public EmpyQueueException(){ + public EmptyQueueException(){ super("Queue Empty"); } } diff --git a/exceptions/EmpyStackException.java b/exceptions/EmptyStackException.java similarity index 60% rename from exceptions/EmpyStackException.java rename to exceptions/EmptyStackException.java index 847d3ae..7813f7b 100644 --- a/exceptions/EmpyStackException.java +++ b/exceptions/EmptyStackException.java @@ -6,9 +6,9 @@ package exceptions; * Date: 05/03/14 * Time: 0.09 */ -public class EmpyStackException extends RuntimeException { +public class EmptyStackException extends RuntimeException { - public EmpyStackException (){ + public EmptyStackException(){ super("Stack Empty"); } } diff --git a/exceptions/FullQueueException.java b/exceptions/FullQueueException.java new file mode 100644 index 0000000..1f59073 --- /dev/null +++ b/exceptions/FullQueueException.java @@ -0,0 +1,14 @@ +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/queue/DynamicArrayQueue.java b/queue/ArrayQueue.java similarity index 80% rename from queue/DynamicArrayQueue.java rename to queue/ArrayQueue.java index 94b325f..1f8e2b0 100644 --- a/queue/DynamicArrayQueue.java +++ b/queue/ArrayQueue.java @@ -1,7 +1,6 @@ package queue; -import exceptions.EmpyQueueException; -import exceptions.EmpyStackException; +import exceptions.EmptyQueueException; /** * Created with xgiovio.macbookair. @@ -9,13 +8,13 @@ import exceptions.EmpyStackException; * Date: 10/03/14 * Time: 15:49 */ -public class DynamicArrayQueue extends QueueRules { +public class ArrayQueue implements Queue { - public DynamicArrayQueue(){ + public ArrayQueue(){ queue = (E[])new Object[def_size]; } - public DynamicArrayQueue(int in_size){ + public ArrayQueue(int in_size){ queue = (E[])new Object[in_size]; } @@ -43,10 +42,10 @@ public class DynamicArrayQueue extends QueueRules { } @Override - public E dequeue() throws EmpyQueueException { + public E dequeue() throws EmptyQueueException { if (front == -1){ - throw new EmpyQueueException(); + throw new EmptyQueueException(); }else{ E temp = queue[front]; front++; @@ -61,9 +60,9 @@ public class DynamicArrayQueue extends QueueRules { } @Override - public E front() throws EmpyQueueException { + public E front() throws EmptyQueueException { if (front == -1){ - throw new EmpyQueueException(); + throw new EmptyQueueException(); }else{ return queue[front]; } @@ -78,6 +77,11 @@ public class DynamicArrayQueue extends QueueRules { } } + @Override + public boolean isFull() { + return false; + } + @Override public int size() { if (front == -1){ diff --git a/queue/ArrayQueueNoBlank.java b/queue/ArrayQueueNoBlank.java new file mode 100644 index 0000000..e600070 --- /dev/null +++ b/queue/ArrayQueueNoBlank.java @@ -0,0 +1,103 @@ +package queue; + +import exceptions.EmptyQueueException; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 10/03/14 + * Time: 15:49 + */ +public class ArrayQueueNoBlank 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 { + + public void enqueue (E element); + public E dequeue () throws EmptyQueueException; + public E front() throws EmptyQueueException; + public boolean isEmpty(); + public boolean isFull(); + public int size(); + +} diff --git a/queue/QueueRules.java b/queue/QueueRules.java deleted file mode 100644 index 51ff4ea..0000000 --- a/queue/QueueRules.java +++ /dev/null @@ -1,20 +0,0 @@ -package queue; - -import exceptions.EmpyQueueException; -import exceptions.EmpyStackException; - -/** - * Created with xgiovio.macbookair. - * User: xgiovio - * Date: 10/03/14 - * Time: 15:45 - */ -abstract public class QueueRules { - - abstract public void enqueue (E element); - abstract public E dequeue () throws EmpyQueueException; - abstract public E front() throws EmpyQueueException; - abstract public boolean isEmpty(); - abstract public int size(); - -} diff --git a/stack/DynamicArrayStack.java b/stack/ArrayStack.java similarity index 77% rename from stack/DynamicArrayStack.java rename to stack/ArrayStack.java index fbfe7b0..d72d1ae 100644 --- a/stack/DynamicArrayStack.java +++ b/stack/ArrayStack.java @@ -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 extends StackRules { +public class ArrayStack implements Stack { - 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 extends StackRules { } @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--]; } diff --git a/stack/FixedArrayStack.java b/stack/FixedArrayStack.java index 7f1c364..a19da7d 100644 --- a/stack/FixedArrayStack.java +++ b/stack/FixedArrayStack.java @@ -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 extends StackRules { +public class FixedArrayStack implements Stack { - 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 extends StackRules { } @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--]; } diff --git a/stack/SinglePointerStack.java b/stack/NodeStack.java similarity index 65% rename from stack/SinglePointerStack.java rename to stack/NodeStack.java index 9613d5e..ef81720 100644 --- a/stack/SinglePointerStack.java +++ b/stack/NodeStack.java @@ -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 extends StackRules { +public class NodeStack implements Stack { @Override public void push(E element) throws FullStackException { - SinglePointerNode temp = new SinglePointerNode(element,stack); + Node temp = new Node(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 to_return; + public E pop() throws EmptyStackException { + Node to_return; if (isEmpty()) { - throw new EmpyStackException(); + throw new EmptyStackException(); }else{ to_return = stack; stack = stack.getNext(); @@ -61,7 +61,7 @@ public class SinglePointerStack extends StackRules { } - private SinglePointerNode stack = null; + private Node stack = null; private int number_of_elements = 0; } diff --git a/stack/Stack.java b/stack/Stack.java new file mode 100644 index 0000000..73d9543 --- /dev/null +++ b/stack/Stack.java @@ -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 { + + 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(); + +} diff --git a/stack/StackRules.java b/stack/StackRules.java deleted file mode 100644 index 63987db..0000000 --- a/stack/StackRules.java +++ /dev/null @@ -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 { - - 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(); - -} diff --git a/stack/utility/SinglePointerNode.java b/stack/utility/Node.java similarity index 61% rename from stack/utility/SinglePointerNode.java rename to stack/utility/Node.java index ebcd9ff..2a470f4 100644 --- a/stack/utility/SinglePointerNode.java +++ b/stack/utility/Node.java @@ -6,15 +6,15 @@ package stack.utility; * Date: 05/03/14 * Time: 0.49 */ -public class SinglePointerNode { +public class Node { - public SinglePointerNode (){ + public Node(){ element = null; next = null; } - public SinglePointerNode (E in_element, SinglePointerNode in_next){ + public Node(E in_element, Node in_next){ element = in_element; next = in_next; } @@ -27,15 +27,15 @@ public class SinglePointerNode { this.element = element; } - public SinglePointerNode getNext() { + public Node getNext() { return next; } - public void setNext(SinglePointerNode next) { + public void setNext(Node next) { this.next = next; } private E element; - private SinglePointerNode next; + private Node next; }