一般队列
队列是先进先出,后进后出的数据结构。可以想象成排队的状态。
public class Queue {
private int [] arr;
private int maxSize;
private int elems;
private int font;
private int end;
public Queue (int maxSize){
this .maxSize=maxSize;
arr=new int [maxSize];
end=-1 ;
}
public void insert (int value){
arr[++end]=value;
elems++;
}
public int remove (){
elems--;
return arr[font++];
}
public boolean isEmpty (){
return (elems == 0 );
}
public boolean isFull (){
return (end == maxSize-1 )
}
}
循环队列
public class MyQueue {
private int [] arr;
private int elems;
private int font;
private int end;
public MyQueue (int maxSize){
this .maxSize=maxSize;
arr=new int [maxSize];
end=-1 ;
}
public void insert (int value){
if (end == maxSize-1 ){
end = -1 ;
}
arr[++end] = value;
elems++;
}
public int remove (){
int temp=arr[font++];
if (font == maxSize){
font = 0 ;
}
elems--;
return temp;
}
}
优先级队列
public class PriorityQueue {
private int [] arr;
private int maxSize;
private int elems;
public PriorityQueue (int maxSize){
this .maxSize = maxSize;
arr = new int [maxSize];
}
public void insert (int value){
int i=0 ;
for (i=0 ;i<elems;i++){
if (arr[i] < value){
break ;
}
}
for (int j=elems;j>i;j--){
arr[j] = arr[j-1 ];
}
arr[i] = value;
elems++;
}
public int remove (){
elems--;
return arr[elems-1 ];
}
}