???????????????????????queue????list(??)???????????????????????????????????????????????????????????????????????????????????(FIFO??First In First Out)?????п??????????????磬????????????????????????????????????????д???????????????????????????????????????????“????????”???????????????????
???????е?????У???????β???????????????????????????????
??????????Java?????е?????????????棩??
package cn.zhf.list;
public class MyQueue {
private int maxSize;//?????????
private int[] qarray;//???????????
private int front;//???????????
private int rear;//????????????
private int nItems;//?????????????
//????????????
public MyQueue(int s){
maxSize = s;
qarray = new int[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//?????β????????
public void enqueue(int i){
if(rear == maxSize - 1){
rear = -1;
}
qarray[++rear] = i;
nItems++;
}
//??????????
public int dequeue(){
int temp = qarray[front++];
if(front == maxSize){
front = 0;
}
nItems--;
return temp;
}
public int peekFront(){//?????????
return qarray[front];
}
public boolean isEmpty(){
return (nItems == 0);
}
public static void main(String[] args) {
MyQueue queue = new MyQueue(5);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
while(!queue.isEmpty()){
int i = queue.dequeue();
System.out.println(i);
}
}
}