关键点在于:
普通队列的front指向第一个元素的前一个,rear指向最后一个元素(包含数据)
循环队列的front指向第一个元素,rear指向最后一个元素的后一个元素(不包含数据),maxSize-1为实际的容量
队列满的条件:
普通rear = maxSize-1;
循环(rear+1)%maxSize == front
空的条件一样:
rear == front;
循环队列的有效数据个数:
(rear + maxSize - front)% maxSize
class ArrayQueue{
private int maxSize;
private int front;
private int rear;
private int[] arr;
//构造器
public ArrayQueue(int arrMaxSize){
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;//队列头部,队列头的前一个位置。
rear = -1;//队列尾部的位置(包含数据)。
}
//队列是不是满了
public boolean isfull(){
return rear == maxSize - 1;
}
//队列是否为空
public boolean isEmpty(){
return front == rear;
}
//添加数据进队列
public void addQueue(int n){
if (isfull()){
System.out.println("队列满,无法添加。");
return;
}
rear++;
arr[rear] = n;
}
//出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列空,无法读取数据。");
}
front++;
return arr[front];//返回出队列的元素值
}
//显示所有元素
public void showQueue(){
if (isEmpty()){
throw new RuntimeException("队列是空的,无法展示数据");
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d] = %d\n",i,arr[i]);
}
}
//展示头数据(第一个数据)
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列是空的,无法展示数据");
}
return arr[front + 1];
}
}
环形队列
class CircleQueue{
private int maxSize;
private int front = 0;
private int rear = 0;
private int[] arr;
//构造器
public CircleQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
}
//队列是不是满了
public boolean isfull(){
return front == (rear + 1) % maxSize;
}
//队列是否为空
public boolean isEmpty(){
return front == rear;
}
//添加数据进队列
public void addQueue(int n){
if (isfull()){
System.out.println("队列满,无法添加。");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
//出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列空,无法读取数据。");
}
int value = arr[front];
front = (front + 1) % maxSize;
return value;//返回出队列的元素值
}
//显示所有元素
public void showQueue(){
if (isEmpty()){
throw new RuntimeException("队列是空的,无法展示数据");
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d] = %d\n",i % maxSize,arr[i % maxSize]);
}
}
public int size(){
return (rear + maxSize - front) % maxSize;
}
//展示头数据(第一个数据)
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列是空的,无法展示数据");
}
return arr[front];
}
}
项目测试代码
public static void main(String[] args) {
CircleQueue queue = new CircleQueue(3);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop){
System.out.println("a(add)添加一个元素");
System.out.println("g(get)出去一个元素");
System.out.println("s(show)展示所有元素");
System.out.println("h(h)展示第一个元素");
System.out.println("e(exit)退出程序");
key = scanner.next().charAt(0);
switch (key){
case 'a':
System.out.println("请输入一个数据");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
System.out.println(queue.getQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 's':
queue.showQueue();
break;
case 'h':
try {
System.out.println(queue.headQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出");
}