A linear queue.
- Karla G
- 28 mar 2015
- 2 Min. de lectura
This is also a queue, but its principal array works in a linear way. Each time an element is dequeued, the elements will move one to the front, so there won't be any empty spaces at the front of the array, and the front will always be zero. (first element of the array)
import remixlab.bias.event.*;
import remixlab.bias.event.shortcut.*;
import remixlab.bias.agent.profile.*;
import remixlab.bias.agent.*;
import remixlab.dandelion.agent.*;
import remixlab.proscene.*;
import remixlab.dandelion.core.*;
import remixlab.dandelion.constraint.*;
import remixlab.fpstiming.*;
import remixlab.util.*;
import remixlab.dandelion.geom.*;
import remixlab.bias.core.*;
import javax.swing.JOptionPane;
Scene scene;
int a[];
int queue_Size, front, back, num, out, z,y;
void setup(){
scene=new Scene(this);
scene.setGridVisualHint(false);
scene.setAxesVisualHint(false);
size(900, 700, P3D);
queue_Size=5;
a= new int[queue_Size];
front=-1;
back=-1;
num=0;
a[4]=0;
}
void draw(){
background(0);
pushMatrix();
textSize(10);
translate(0,0,30);
fill(#20FF00);
text("Welcome to the queue simulator, ", -100, 45);
text("click the numbers between 1-4 ", -100, 60);
text(" to push the figure and to choose it, ", -100, 75);
text("click 5 to pop the lastone ", -100, 90);
fill(#22FF00);
popMatrix();
pushMatrix();
translate(5,5,5);
translate (20,0,0);
fill(255,0,0);
rect(10,10,10,10);
fill(255,0,255);
translate (20,0,0);
rect(10,10,10,10);
fill(0,255,0);
translate (20,0,0);
rect(10,10,10,10);
fill(0,0,255);
translate (20,0,0);
rect(10,10,10,10);
popMatrix();
pushMatrix();
translate(40,-40,0);
dibujar();
popMatrix();
pushMatrix();
dibuja();
popMatrix();
}
void keyReleased(){
switch(key){
case '1':
queue(1);
break;
case '2':
queue(2);
break;
case '3':
queue(3);
break;
case '4':
queue(4);
break;
case '5':
dequeue();
break;
}
}
void queue(int num){
if(back+1 < queue_Size-1){
back++;
a[back]=num;
}
else{
println("There is no more space");
}
for(int i=0; i<queue_Size-1; i++){
println("Array after queue: "+a[i]);
}
println("");
}
void dequeue(){
z=0;
for(int k=0; k< queue_Size-1; k++){
if(a[k]!=0){
z++;
}
}
if(z!=0){
front=0;
}
else{
front=-1;
}
if(front==0){
out=a[front];
for(int j=0; j<queue_Size-1; j++){
a[j]=a[j+1];
}
back--;
}
else{
println("ERROR in dequeue");
}
for(int i=0; i<queue_Size-1; i++){
println("Array after dequeue: "+a[i]);
}
println("");
}
void dibujar(){
switch(out){
case 1:
fill(255,0,0);
rect(10,10,10,10);
case 2:
fill(255,0,255);
rect(10,10,10,10);
break;
case 3:
fill(0,255,0);
rect(10,10,10,10);
break;
case 4:
fill(0,0,255);
rect(10,10,10,10);
break;
}
}
void dibuja(){
for(int i=0; i<=4; i++){
switch(a[i]){
case 1:
translate (0,-20,0);
fill(255,0,0);
rect(10,10,10,10);
break;
case 2:
translate (0,-20,0);
fill(255,0,255);
rect(10,10,10,10);
break;
case 3:
translate (0,-20,0);
fill(0,255,0);
rect(10,10,10,10);
break;
case 4:
translate (0,-20,0);
fill(0,0,255);
rect(10,10,10,10);
break;
}
}
}
Comments