Creato Parzialmente la Queue
This commit is contained in:
14
exceptions/EmpyQueueException.java
Normal file
14
exceptions/EmpyQueueException.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with MONSTER.
|
||||||
|
* User: xgiovio
|
||||||
|
* Date: 05/03/14
|
||||||
|
* Time: 0.09
|
||||||
|
*/
|
||||||
|
public class EmpyQueueException extends RuntimeException {
|
||||||
|
|
||||||
|
public EmpyQueueException(){
|
||||||
|
super("Queue Empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
99
queue/DynamicArrayQueue.java
Normal file
99
queue/DynamicArrayQueue.java
Normal 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
20
queue/QueueRules.java
Normal 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();
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user