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

View File

@@ -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<test_object> queue = new DynamicArrayQueue<test_object>(5);
ArrayQueueNoBlank<test_object> queue = new ArrayQueueNoBlank<test_object>(5);
queue.enqueue(new test_object(1));
System.out.println(queue.front().get_data());

View File

@@ -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<Character> stack = new SinglePointerStack<Character>();
NodeStack<Character> stack = new NodeStack<Character>();
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");
}
}

View File

@@ -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<Character> stack = new SinglePointerStack<Character>();
NodeStack<Character> stack = new NodeStack<Character>();
for (int i = 0;i< word.length();i++){
stack.push(word.charAt(i));
}

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -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<E> extends QueueRules<E> {
public class ArrayQueue<E> implements Queue<E> {
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<E> extends QueueRules<E> {
}
@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<E> extends QueueRules<E> {
}
@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<E> extends QueueRules<E> {
}
}
@Override
public boolean isFull() {
return false;
}
@Override
public int size() {
if (front == -1){

View File

@@ -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<E> implements Queue<E> {
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<queue.length; i++){
new_queue[i] = queue[front];
front = (front + 1) % queue.length;
}
rear = queue.length;
front = 0;
queue = new_queue;
}
}
@Override
public E dequeue() throws EmptyQueueException {
if (front == -1){
throw new EmptyQueueException();
}else{
E temp = queue[front];
front++;
if (front == rear){
front = -1;
rear = 0;
}
return temp;
}
}
@Override
public E front() throws EmptyQueueException {
if (front == -1){
throw new EmptyQueueException();
}else{
return queue[front];
}
}
@Override
public boolean isEmpty() {
if (size() == 0){
return true;
}else{
return false;
}
}
@Override
public boolean isFull() {
return false;
}
@Override
public int size() {
if (front == -1){
return 0;
}
if (front < rear){
return (rear - front);
}else{
return (queue.length - front + rear);
}
}
int def_size = 100;
E[] queue ;
int front = -1;
int rear = 0;
}

103
queue/FixedArrayQueue.java Normal file
View File

@@ -0,0 +1,103 @@
package queue;
import exceptions.EmptyQueueException;
/**
* Created with xgiovio.macbookair.
* User: xgiovio
* Date: 10/03/14
* Time: 15:49
*/
public class FixedArrayQueue<E> implements Queue<E> {
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<queue.length; i++){
new_queue[i] = queue[front];
front = (front + 1) % queue.length;
}
rear = queue.length;
front = 0;
queue = new_queue;
}
}
@Override
public E dequeue() throws EmptyQueueException {
if (front == -1){
throw new EmptyQueueException();
}else{
E temp = queue[front];
front++;
if (front == rear){
front = -1;
rear = 0;
}
return temp;
}
}
@Override
public E front() throws EmptyQueueException {
if (front == -1){
throw new EmptyQueueException();
}else{
return queue[front];
}
}
@Override
public boolean isEmpty() {
if (size() == 0){
return true;
}else{
return false;
}
}
@Override
public boolean isFull() {
return false;
}
@Override
public int size() {
if (front == -1){
return 0;
}
if (front < rear){
return (rear - front);
}else{
return (queue.length - front + rear);
}
}
int def_size = 100;
E[] queue ;
int front = -1;
int rear = 0;
}

20
queue/Queue.java Normal file
View File

@@ -0,0 +1,20 @@
package queue;
import exceptions.EmptyQueueException;
/**
* Created with xgiovio.macbookair.
* User: xgiovio
* Date: 10/03/14
* Time: 15:45
*/
public interface Queue<E> {
public void enqueue (E element);
public E dequeue () throws EmptyQueueException;
public E front() throws EmptyQueueException;
public boolean isEmpty();
public boolean isFull();
public int size();
}

View File

@@ -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 <E> {
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();
}

View File

@@ -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--];
}

View File

@@ -1,6 +1,6 @@
package stack;
import exceptions.EmpyStackException;
import exceptions.EmptyStackException;
import exceptions.FullStackException;
/**
@@ -11,7 +11,7 @@ import exceptions.FullStackException;
*/
public class FixedArrayStack<E> extends StackRules<E> {
public class FixedArrayStack<E> implements Stack<E> {
public FixedArrayStack(){
@@ -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--];
}

View File

@@ -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
View 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();
}

View File

@@ -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();
}

View File

@@ -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;
}