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,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){
|
||||
103
queue/ArrayQueueNoBlank.java
Normal file
103
queue/ArrayQueueNoBlank.java
Normal 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
103
queue/FixedArrayQueue.java
Normal 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
20
queue/Queue.java
Normal 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();
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user