Ideas:
background
There are two ways to implement queues: 1. Array, 2. Linked list
When the array implements the queue, some textbooks only say that the condition for the queue to be full is (rear + 1) % manSize = front
This formula is really confusing
It turns out: this is the result of an array simulating a circular queue
Front of the queue: the initial value is 0, pointing to the first element of the queue
The end of the queue rear: the initial value is 0, pointing to the next bit of the last element of the queue
Compare the following ring diagram analysis: when an empty queue adds an element, rear++, rear becomes 1, the position of array 0 is used to store data, and rear does not store data.
At this point, if you add another element. rear++ , rear becomes 2, and the position of array 1 stores data.
The condition for the queue to be full is (rear + 1) % manSize = front Since rear is left empty, an array with maxSize of 8 can only store up to 7 bits. When rear is 7, (7+1)%8 = 0
The number of valid data in the queue is (rear-front+maxSize)%manSize
Because it is a circular queue, the rear may be smaller than the front, such as rear = 1, front = 6, plus maxSize in order not to take the absolute value, it is actually |rear-front|%manSize, because the absolute value needs to call the Math package.
It can be counted against the ring graph, rear does not save the value, and the result is 3 elements.
Apply the formula (1-6+8)%8 = 3 %8 = 3
For ease of understanding, I have drawn a concentric circle
Graphical demo:
Suppose maxsize=7
package suanfa;
import java.util.Scanner;
public class xishuarr {
public static void main(String[] args) {
ArrayQueue Queue=new ArrayQueue(4);
char key=' ';//接受用户输入
Scanner scanner =new Scanner(System.in);
boolean loop=true;
while(loop) {
System.out.println("s(shou):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列取数据");
System.out.println("h(head):查看队列头的数据");
key=scanner.next().charAt(0);
switch (key) {
case 's':
Queue.show();
break;
case 'a':
System.out.println("请输入一个数");
int value=scanner.nextInt();
Queue.add(value);
break;
case 'g':
try {
int res= Queue.get();
System.out.printf("取出的数据是%d\n",res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res= Queue.head();
System.out.printf("表头数据是%d\n",res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
break;
default:
break;
}
}
System.out.println("程序退出---");
}
}
class ArrayQueue{
private int maxSize;//数组最大容量
private int front;//队列头
private int rear;//队列尾
private int[] arr;//该数据用于存放数据,模拟队列
public ArrayQueue(int arrMaxSize) {
maxSize =arrMaxSize;
arr=new int[arrMaxSize];
front =0;//指向队列头部
rear=0;//指向队列尾
}
//判断队列是否为满
public boolean isfull() {
//因为是环形队列
return (rear+1)%maxSize==front;
}
//判断队列是否为空
public boolean isEmpty() {
return rear==front;
}
//添加数据到队列
public void add(int n){
if(isfull()) {
System.out.println("队列已满");
return ;
}
arr[rear]=n;
rear=(rear+1)%maxSize;
}
//获取队列的数据,出队列
public int get(){
if(isEmpty()) {
//抛出一个异常
throw new RuntimeException("队列空,不能取数据");
}
int value=arr[front];
front=(front+1)%maxSize;
return value;
}
//显示队列的所有数据
public void show() {
while(isEmpty()) {
System.out.println("队列空的,没有数据-----");
return;
}
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 head() {
if(isEmpty()) {
throw new RuntimeException("队列空的,没有数据");
}
return arr[front];
}
}
Part of the content of this article refers to the original article of the CSDN blogger "Green Night Walker"
Original link: https://blog.csdn.net/u013921288/article/details/123520874