package com.yajxit.queue;
public class CircleArrayQueueDemo {
private int maxSize;
private int front;
private int rear;
private int[]arr;
public CircleArrayQueueDemo(int maxSize){
this.maxSize = maxSize;
arr=new int[maxSize];
//默认为0可以删除
front=0;
rear=0;
}
public boolean isFull(){
return (rear+1)%maxSize==front;
}
public boolean isEmpty(){
return rear==front;
}
public void addElement(int n){
if (isFull()){
System.out.println("队列已满,无法添加元素");
}
//添加元素
arr[rear]=n;
//移动rear指针,取模
rear=(rear+1)%maxSize;
}
public int popElement(){
if (isEmpty()){
throw new RuntimeException("队列为空,不能输出");
}
//1,把front元素暂时存储到一个临时变量
int value=arr[front];
//2,移动front指针
front=(front+1)%maxSize;
//3,返回front
return value;
}
}