Cola Circular
 
Cola Circular
Mis aficiones
Album de fotos
Curriculum vitae
Mis enlaces favoritos
Cola Circular
 
Imagen
 
Codigo de una cola circular
import java.util.Queue;


public class ArrayCircularQueue implements Queue {
private int front=0, rear=0;
private Object[]queue;

public ArrayCircularQueue(int maxElements){
queue=new Object[maxElements];
}
public void insert(Object o){
int temp =rear;
rear=(rear+1)%queue.length;
if (front==rear){
rear=temp;
//throw new FullQueueException();
}
queue [rear]=o;
}
public boolean isEmpty(){
return front==rear;
}
public boolean isFull(){
return ((rear+1)%queue.length)==front;
}
public Object remove(){
if (front==rear);
//throw new EmptyQueueException();
front=(front+1)%queue.length;
return queue [front];
}
}


import java.awt.peer.SystemTrayPeer;


public class ArrayLinearQueue implements Queue {
private int front = -1, rear= -1;
private Object[]queue;

public ArrayLinearQueue(int maxElements){
queue=new Object[maxElements];
}
public void insert (Object o){
if(rear==queue.length-1);
//throw new FullQueueException ();
queue[++rear]=o;
}
public boolean isEmpty(){
return front==rear;
}
public boolean isFull(){
return rear==queue.length -1;
}
public Object remove(){
if(front==rear);
//throw new EmptyQueueException ();
return queue[++front];
}



}



import com.javajeff.cds.*;

public class Queuedemo {
public static void main (String []args){
System.out.println("ArrayLinearQueue demo");
System.out.println("----------------------");
queueDemo(new ArrayLinearQueue (5));
System.out.println("ArrayCircularQueue demo");
System.out.println("----------------------");
queueDemo(new ArrayCircularQueue (6));

}
static void queueDemo (Queue q){
System.out.println("Is empty =" +q.isEmpty());
System.out.println("Is full = " +q.isFull());

System.out.println("Insertign/This/");
q.insert("This");

System.out.println("Inserting/is");
q.insert("is");

System.out.println("Inserting/a");
q.insert("a");

System.out.println("inserting/sentence");
q.insert("snetence");

System.out.println("inserting/sentence");
q.insert(".");

try {
System.out.println("inserting/One last time");
q.insert("One last Time");

}
catch (FullQueueException e){
System.out.println("One insert too may");
System.out.println("Is empty="+q.isEmpty());
System.out.println("Is full="+q.isFull());

}
System.out.println("");

while (!q.isEmpty());
System.out.println("q.remove()"+"[Is empty=]"+q.isEmpty()+",Is full="+q.isFull()+"]");

try {
q.remove();
}
catch (EmptyQueueException e){
System.out.println("One insert too may");
}
System.out.println("");

}

}



public Interface Queue{
void insert (Object o);
boolean isEmpty();
boolean isFull();
Object remove();

}



public class FullQueueException extends RuntimeException {

}



public class EmptyQueueException extends RuntimeException {

}
 
.
Escríbeme
Me interesa tu opinión