Creato Parzialmente la Queue

This commit is contained in:
2014-03-10 16:36:55 +01:00
parent 84aa1374c7
commit 0759ac55e6
4 changed files with 133 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
package queue;
import exceptions.EmpyQueueException;
import exceptions.EmpyStackException;
/**
* Created with xgiovio.macbookair.
* User: xgiovio
* Date: 10/03/14
* Time: 15:49
*/
public class DynamicArrayQueue<E> extends QueueRules<E> {
public DynamicArrayQueue(){
queue = (E[])new Object[def_size];
}
public DynamicArrayQueue(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 EmpyQueueException {
if (front == -1){
throw new EmpyQueueException();
}else{
E temp = queue[front];
front++;
if (front == rear){
front = -1;
rear = 0;
}
return temp;
}
}
@Override
public E front() throws EmpyQueueException {
if (front == -1){
throw new EmpyQueueException();
}else{
return queue[front];
}
}
@Override
public boolean isEmpty() {
if (size() == 0){
return true;
}else{
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/QueueRules.java Normal file
View File

@@ -0,0 +1,20 @@
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();
}