介绍
- 队列是一个有序列表,可以用数组或是链表来实现;
- 遵循先入先出的原则。即:先存入队列的数据,要先取出,后存入的要后取出;
- 示意图:(使用数组模拟队列示意图)
数组实现队列
思路分析
- 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如上图,其中maxSize是该队列的最大容量。
- 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量front和rear分别记录队列前后端的下标,front会随着数据的输出而改变,rear则是随着数据的输入而改变(如上图所示)
- 当我们将数据存入队列时称为“addQueueu”,addQueue的处理需要两个步骤:
- 将尾指针往后移动:rear = rear + 1,当front == rear时,则表示当前队列为空
- 若尾指针rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据。当rear == maxSize - 1时,则表示当前队列已满
代码实现
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(3);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("p(pop): 从队列取出数据");
System.out.println("h(head): 查看队列头的数据");
key = scanner.next().charAt(0);
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'p':
try {
int res = queue.popQueue();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.headQueue();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
System.out.println();
System.out.println();
System.out.println();
}
System.out.println("程序退出~~");
}
}
class ArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[maxSize];
this.front = -1;
this.rear = -1;
}
public boolean isFull() {
return this.rear == this.maxSize - 1;
}
public boolean isEmpty() {
return this.front == this.rear;
}
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列满,不能添加数据~~~~~~~~~~~~~~~~~~~~");
return;
}
rear++;
this.arr[rear] = n;
}
public int popQueue() {
if (isEmpty()) {
throw new RuntimeException("队列空,不能取出数据~~~~~~~~~~~~~~~~~~~~");
}
front++;
return arr[front];
}
public void showQueue() {
if (isEmpty()) {
System.out.println("队列空的,没有数据~~~~~~~~~~~~~~~~~~~~~~");
return;
}
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 this.arr[front + 1];
}
}
问题分析并优化
- 目前数组使用一次就不能使用了,没有达到复用的效果
- 将这个数组使用算法,改进成一个环形队列,取模:%
数组实现环形队列
分析说明
- 尾索引的下一个位头索引时表示队列已满,即将队列容量空出一个作为约定,这个在做判断队列满的时候需要注意:(rear + 1) % maxSize == front,即表示为队列已满
- 判断队列为空的方式仍然是 rear == front
思路分析
- front变量的含义做一个调整:front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值为0
- rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定,rear的初始值为0
- 队列已满的条件是:(rear + 1) % maxSize == front
- 队列为空的条件是:rear == front
- 队列中有效数据的个数是:(rear + maxSize - front) % maxSize
- 我们可以在原来队列的基础上进行修改,得到一个环形队列
代码实现
public class CircleArrayQueueDemo {
public static void main(String[] args) {
CircleArray queue = new CircleArray(4);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("p(pop): 从队列取出数据");
System.out.println("h(head): 查看队列头的数据");
key = scanner.next().charAt(0);
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'p':
try {
int res = queue.popQueue();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.headQueue();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
System.out.println();
System.out.println();
System.out.println();
}
System.out.println("程序退出~~");
}
}
class CircleArray {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleArray(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[maxSize];
this.front = 0;
this.rear = 0;
}
public boolean isFull() {
return ((this.rear + 1) % this.maxSize) == this.front;
}
public boolean isEmpty() {
return this.front == this.rear;
}
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列满,不能添加数据~~~~~~~~~~~~~~~~~~~~");
return;
}
this.arr[rear] = n;
rear = (rear + 1) % maxSize;
}
public int popQueue() {
if (isEmpty()) {
throw new RuntimeException("队列空,不能取出数据~~~~~~~~~~~~~~~~~~~~");
}
int temp = arr[front];
front = (front + 1) % maxSize;
return temp;
}
public int size() {
return (rear + maxSize - front) % maxSize;
}
public void showQueue() {
if (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 headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列空的,没有数据~~");
}
return arr[front];
}
}