简介
- 队列是一个 有序列表,可以用 数组或是 链表来实现。
- 遵循 先入先出的原则。即: 先存入队列的数据,要先取出。后存入的要后取出。
数组模拟队列(非环形)
简单介绍
-
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量。
-
因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front 及 rear 分别记录队列前后端的下标,
front 会随着数据输出而改变,而 rear 则是随着数据输入而改变,如图所示:
-
当我们将数据存入队列时(addQueue),addQueue 的处理需要有两个步骤:
- 将尾指针往后移:
rear+1
, 当front == rear
队列为空 - 若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear 所指的数组元素中,否则无法存入数据。
rear == maxSize - 1
队列满
代码实现
ArrayQueue 队列类,使用数组实现
package datastructure.queue;
/**
* 使用数组模拟队列
*/
public class ArrayQueue {
/**
* 数组最大容量
*/
private int maxSize;
/**
* 队列头
*/
private int front;
/**
* 队列尾
*/
private int rear;
/**
* 存放数据,模拟队列
*/
private int[] array;
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
array = new int[maxSize];
// 指向队列头部,它是指向队列头的前一个位置
front = -1;
// 指向队列尾部,指向队列尾的数据,即就是队列最后一个数据
rear = -1;
}
/**
* 判断队列是否已满
*/
public boolean isFull() {
return rear == maxSize - 1;
}
/**
* 判断队列是否为空
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 添加数据到队列
*/
public void addQueue(int data) {
if (isFull()) {
System.out.println("队列已满,不能加入数据");
} else {
// ++rear rear 后移了一位
array[++rear] = data;
}
}
/**
* 获取队列数据,出队列
*/
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列空,不能取数据~~");
}
// ++front front 后移了一位
return array[++front];
}
/**
* 显示队列所有数据
*/
public void showQueue() {
if (isEmpty()) {
System.out.println("队列为空!");
} else {
for (int i = front; i < rear; i++) {
System.out.printf("array[%d]=%d\n", i + 1, array[i + 1]);
}
}
}
/**
* 显示队列的头数据,不是取数据,只是查看
*/
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空!");
}
return array[front + 1];
}
}
ArrayQueueDemo 数组队列测试类
package datastructure.queue;
import java.util.Scanner;
/**
* 队列测试类
*/
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("g(get): 从队列取出数据");
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 'g':
try {
int result = queue.getQueue();
System.out.printf("取出的数据是%d\n", result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int result = queue.headQueue();
System.out.printf("队列头的数据是%d\n", result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~~~");
}
}
测试结果:
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
队列为空!
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请数据一个数:
1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
array[0]=1
array[1]=0
array[2]=0
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请数据一个数:
2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请数据一个数:
3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请数据一个数:
4
队列已满,不能加入数据
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
array[0]=1
array[1]=2
array[2]=3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
h
队列头的数据是1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
h
队列头的数据是2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
h
队列头的数据是3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
h
队列为空!
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请数据一个数:
4
队列已满,不能加入数据
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e
程序退出~~~~
观察测试结果,当队列中数据全部取出时,不能再次往队列中添加数据,这是目前遇到的问题,后面用环形队列的方式来解决这个问题。
数组模拟环形队列
简单介绍
- 对前面的数组模拟队列优化,充分利用数组,因此将数组看做是一个环形的。(通过取模的方式来实现)
实现思路:
- front变量指向队列的第一个元素,初始值为0;
- rear变量指向队列的最后一个元素的后一个位置,空出一个空间作为约定,初始值为0
- 尾索引的下一个为头索引时表示队列满, 队列容量空出一个作为约定,这个在做判断队列满的时候需要注意,
(rear + 1) % maxSize == front
表示队列已满 rear == front
表示队列为空- 队列中的有效个数:
(rear + maxSize - front) % maxSize
代码实现
CircleArrayQueue 环形队列类
/**
* 使用数组模拟环形队列
*/
public class CircleArrayQueue {
/**
* 数组最大容量
*/
private int maxSize;
/**
* 队列头, 指向队列的第一个元素
* 初始值为 0
*/
private int front;
/**
* 队列尾,指向队列的最后一个元素的后一个位置,希望空出一个空间作为约定
* 初始值为 0
*/
private int rear;
/**
* 存放数据,模拟队列
*/
private int[] array;
public CircleArrayQueue(int maxSize) {
this.maxSize = maxSize;
array = new int[maxSize];
}
/**
* 判断队列是否已满
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* 判断队列是否为空
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 添加数据到队列
*/
public void addQueue(int data) {
if (isFull()) {
System.out.println("队列已满,不能加入数据");
} else {
array[rear] = data;
// 将rear后移,这里必须取模
rear = (rear + 1) % maxSize;
}
}
/**
* 获取队列数据,出队列
*/
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列空,不能取数据~~");
}
// 这里需要分析出front 是指向队列的第一个元素
// 1、先将 front 对应的值保留到一个临时变量中
// 2、将front后移,考虑取模
// 3、将临时变量的值返回
int value = array[front];
front = (front + 1) % maxSize;
return value;
}
/**
* 显示队列所有数据
*/
public void showQueue() {
if (isEmpty()) {
System.out.println("队列为空!");
} else {
// 从front 开始遍历,遍历多少个元素
for (int i = front; i < front + size(); i++) {
System.out.printf("array[%d]=%d\n", i % maxSize, array[i % maxSize]);
}
}
}
/**
* 当前队列有效数据的个数
*/
public int size() {
return (rear + maxSize - front) % maxSize;
}
/**
* 显示队列的头数据,不是取数据,只是查看
*/
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空!");
}
return array[front];
}
}
CircleArrayQueueDemo 环形队列测试类
/**
* 队列测试类
*/
public class CircleArrayQueueDemo {
public static void main(String[] args) {
System.out.println("------测试数组模拟环形队列-------");
// 容量设置为4,其队列的有效数据最大是3
CircleArrayQueue queue = new CircleArrayQueue(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("g(get): 从队列取出数据");
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 'g':
try {
int result = queue.getQueue();
System.out.printf("取出的数据是%d\n", result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int result = queue.headQueue();
System.out.printf("队列头的数据是%d\n", result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~~~");
}
}
结果:
------测试数组模拟环形队列-------
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
队列为空!
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入一个数:
10
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入一个数:
20
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入一个数:
30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入一个数:
40
队列已满,不能加入数据
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
array[0]=10
array[1]=20
array[2]=30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是10
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
array[1]=20
array[2]=30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入一个数:
40
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
array[1]=20
array[2]=30
array[3]=40
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据