文章目录
1.基本概念
队列是一种先进先出的数据结构,类似于管道;当红球、蓝球、黑球依次进入管道后,管道中的排序也是红、蓝、黑;若要取出球,也只能依次取出:红、蓝、黑。

Tom想打到Jerry,必须依次把蓝、绿、黄......打掉,最后才能取到Jerry;
在使用代码写队列时,需要使用两个指针表示队列的首位数据front和尾部数据rear;如果要用数组,需要明确数组大小,避免队列数据超出数组范围。
在进行取数据时,通过front指针获取最前面的数据,然后front指针重新指向最前面的数据;在进行增加数据时,需要考虑当前队列是否超出数组大小,再将数据增加到最后,并且rear指针后移一位;
2.代码实现-一次性数组
java
package queue;
import java.util.Scanner;
/**
* 队列 用数组实现
* 数据直接从0位置存放到数组中,新数据向后面存储
* 队列新增数据时,rea指针移位;rear指向最后一位数据
* 队列取出数据时,front指针移位;front指向首部数据的前一位
* <p>
* 目前队列问题:只能使用一次数组,即使数组为空,当rear指针移动到末尾,也不能增加数据
*/
public class ArrayQueue {
public static void main(String[] args) {
QueueArr queue = new QueueArr(3);
char key = ' '; // 接收用户输入
Scanner scanner = new Scanner(System.in); // 创建键盘输入管道
boolean loop = true; // 循环标识符
// 队列测试菜单
while (loop) {
System.out.println("s(show):显示队列");
System.out.println("a(add):添加数据");
System.out.println("g(get):取出数据");
System.out.println("h(head):查看头部数据");
System.out.println("e(exit):退出队列");
System.out.println("请输入操作项:");
key = scanner.next().charAt(0);
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("请输入一个整数:");
queue.addQueue(scanner.nextInt());
break;
case 'g':
try {
System.out.println(queue.getQueue());
break;
} catch (Exception e) {
System.out.println(e.getMessage());
}
case 'h':
try {
System.out.println(queue.headQueue());
break;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
case 'e':
loop = false;
break;
default:
System.out.println("输入的操作项不对!");
}
}
}
}
/**
* 队列类
*/
class QueueArr {
private int[] arr; // 队列数组
private int maxSize; // 队列数组最大容量
private int front; // 队列头部 记录头部数据的index值
private int rear; // 队列尾部 记录尾部数据的index值
/**
* 队列构造器
*
* @param size 指定队列长度
*/
public QueueArr(int size) {
this.maxSize = size; // 为队列最大容量赋值
this.arr = new int[size]; // 构建队列数组
this.front = -1; // 默认值
this.rear = -1; // 默认值
}
/**
* 判断队列是否已满
*
* @return 布尔值
*/
public boolean isFull() {
// 如果rear指针指向了数组最后的位置,表示已满
return this.rear == this.maxSize - 1;
}
/**
* 判断队列是否为空
*
* @return 布尔值
*/
public boolean isEmpty() {
// 如果front与rear数值相同,表示没有数据进入队列,此时队列为空
return this.front == this.rear;
}
/**
* 队列添加数据
*
* @param data 添加的数据
*/
public void addQueue(int data) {
// 判断队列是否已满,未满则添加数据;已满则提示并退出
if (isFull()) { // 队列已满
System.out.println("队列已满,无法添加数据!");
} else { // 队列未满,可以添加数据
this.rear++; // 确定新数据存放的index位置
this.arr[this.rear] = data; // 将新数据增加到数组中
System.out.println("已添加新数据!");
}
}
/**
* 取出一位队列值
*
* @return 返回队列首部数据
*/
public int getQueue() {
// 先判断队列是否为空
if (isEmpty()) { // 空队列,抛出异常
throw new RuntimeException("队列为空!");
} else { // 队列中有数据
this.front++; // 首部指针移位
return this.arr[this.front]; // 获取首部数据
}
}
/**
* 显示队列头部数据,注意不是取出数据
*
* @return 返回队列头部数据
*/
public int headQueue() {
// 先判断队列是否为空
if (isEmpty()) { // 空队列,抛出异常
throw new RuntimeException("队列为空!");
} else { // 队列中有数据
return this.arr[this.front + 1]; // 返回首部数据
}
}
/**
* 打印队列数据
*/
public void showQueue() {
// 先判断队列是否为空,不为空时才打印数据
if (isEmpty()) { // 空队列
System.out.println("队列为空!");
} else { // 队列中有数据
for (int i = 0; i <= this.rear; i++) { // 遍历到尾部指针为止
System.out.printf("%d\t", this.arr[i]);
}
System.out.println();
}
}
}