import java.sql.SQLOutput;
public class QueueDemo {
public static void main(String[] args) {
ArrayQueue arrayqueue=new ArrayQueue(1);
arrayqueue.addQueue(2);
arrayqueue.addQueue(2);
arrayqueue.showQueue();
// System.out.println(arrayqueue.getQuery());
System.out.println(arrayqueue.getHeadQuery());
}
}
class ArrayQueue{
int maxsize;
int rear;
int front;
int arr[];
ArrayQueue(int maxsize){
this.maxsize=maxsize;
arr=new int[maxsize];
front=-1;//从0开始,默认指向队头前一个元素
rear=-1;
}
//判断队列是否为空
boolean isEmpty(){
return rearfront;
}
//判断队列是否已满
boolean isFull(){
return rearmaxsize-1; //队列是从0号下标开始,直到maxsize-1即满,类似于数组下标
}
//添加队列元素,入队列
void addQueue(int n){
if(isFull()) {
System.out.println(“队列已满!”);
return;
}
rear++;
arr[rear]=n;
}
//获取队列元素,出队列
int getQuery(){
if (isEmpty()) {
throw new RuntimeException(“队列为空”);
}
front++;
return arr[front];
}
//显示队列所有数据
void showQueue(){
if(isEmpty())
throw new RuntimeException(“队列为空”);
for (int i = 0; i <arr.length; i++) {
System.out.print(“arr[”+i+"]="+arr[i]+" ");
}
}
//获取队列头元素
int getHeadQuery(){
if (isEmpty()) {
throw new RuntimeException(“队列为空”);
}
return arr[front+1];
}
}