Java数据结构|队列
队列介绍
- 队列是一个有序列表,可以用数组或是链表来实现;
- 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的的数据要后取出;
- 示意图:(使用数组模拟队列示意图)
数组模拟队列思路
- 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如上图,其中maxSize是该队列的最大容量;
- 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量front和rear来分别记录前后端的下标,front会随着数据的输出而改变,而rear则是随着数据输入而改变。
- 当我们将数据存入队列是称为“addQueue”,addQueue的处理有两个步骤:
- 将尾指针往后移,rear+1,当front == rear时,判断列表为空;
- 若尾指针rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear == maxSize是判断为队列满。
代码实现(非环形队列)
数组模拟队列类函数ArrayQueue
// 使用数组模拟队列编写一个ArrayQueue类
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; // 指向队列头部,分析出front是指向队列头的前一个位置
rear = -1; // 指向队列尾,指向队列尾的数据也就是数组的最后一个数据
}
// 判断队列为空
public boolean isEmpt(){
return front == rear;
}
// 判断队列是否满列
public boolean isFull(){
return rear == maxSize - 1;
}
// 入队列(添加数据到队列)
public void addQueue(int data){
// 先判断是否为满队列
if(isFull()){
System.out.println("队列满,无法加入数据!");
return;
}
rear++;
arr[rear] = data;
}
// 出队列(获取队列的数据)
public int getQueue(){
// 先判断是否为空队列
if(isEmpt()){
throw new RuntimeException("队列空,无法获取数据");
}
front++;
return arr[front];
}
// 显示队列的所有数据(遍历实现)
public void showQueue(){
if(isEmpt()){
System.out.println("空队列,无数据");
return;
}
for(int i = 0; i<arr.length; i++ ){
// i = front +1;
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
// 显示队列的头数据(注意不是取出数据)
public int headQueue(){
if(isEmpt()){
throw new RuntimeException("空队列,无数据");
}
return arr[front + 1];
}
}
测试代码
package priv.wdragon.queue;
/*
* 队列介绍
* 数组模拟队列思路
*/
import java.util.Scanner;
public class ArrayQueueTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 测试arrayQueue
// 首先创建一个队列
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 'e':
scanner.close();
loop = false;
break;
case 'a':
System.out.println("请输入要加入队列的数(int):");
int data = scanner.nextInt();
queue.addQueue(data);
break;
case 'g':
try {
int result = queue.getQueue();
System.out.printf("取出的数据是:%d\n", result);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int result = queue.headQueue();
System.out.printf("队列头的数据是:%d\n", result);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
System.out.println("程序已退出");
}
}
测试结果
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
14
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
arr[0]=14 arr[1]=0 arr[2]=0 s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
g
取出的数据是:14
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
空队列,无数据
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
12
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
arr[0]=14 arr[1]=12 arr[2]=0 s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
4
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
arr[0]=14 arr[1]=12 arr[2]=4 s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
4
队列满,无法加入数据!
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
g
取出的数据是:12
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
arr[0]=14 arr[1]=12 arr[2]=4 s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
g
取出的数据是:4
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
空队列,无数据
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
测试结果分析
我们发现此时用数组模拟的队列,当数据出队列之后,之前的位置不能再利用了,当判断队列为空的时候,实际上数组还是有数据的。当所有数据全部出队列以后,此时rear == front 同时也满足rear == maxSize - 1,也就是及判断为空队列,又是满队列。
因此,接下来利用这个数组改进成一个循环队列,取模:%。
数组模拟环形队列
对前面的数组模拟队列的优化,充分利用数组,因此将数组看成是一个环形的。通过取模的方式来实现。
分析思路
- front变量的含义调整为:头队列数据所处的位置索引;初始化front = 0;
- rear变量的含义调整为:尾队列数据的后一个位置的索引。因为希望空出一个空间作为约定。初始化rear = 0;
- 满队列的判断条件:(rear + 1) % maxSize == front;
- 空队列的判断条件:rear == front;
- 此时,队列中的有效数据的个数:(rear - front + macSize) % macSize;
代码实现(环形队列)
数组模拟环形队列类函数CircleArrayQueue
class CircleArrayQueue {
private int maxSize; // 表示数组的最大容量
private int front; // 指向队列头数据的位置索引
private int rear; // 队列尾数据的后一个位置的索引,默认空余出来一个空间
private int[] arr; // 该数组用于存放数据,模拟循环队列
// 创建队列的构造器
public CircleArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = 0; // 指向队列头数据的位置索引
rear = 0; // 队列尾数据的后一个位置的索引,默认空余出来一个空间
// 已经初始化,front = 0 和 rear = 0 可以不写
}
// 判断队列为空
public boolean isEmpt() {
return front == rear;
}
// 判断队列是否满列
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
// 入队列(添加数据到队列)
public void addQueue(int data) {
// 先判断是否为满队列
if (isFull()) {
System.out.println("队列满,无法加入数据!");
return;
}
arr[rear] = data;
// 先直接加数据至rear处,然后rear再后移,此时因为是循环队列,所以考虑取模
rear = (rear + 1) % maxSize;
}
// 出队列(获取队列的数据)
public int getQueue() {
// 先判断是否为空队列
if (isEmpt()) {
throw new RuntimeException("队列空,无法获取数据");
}
// 要先将头部的数据暂存位一个变量,因为循环队列front的含义导致先取数据然后再后移,
// 如果先取出来(return)的话,front就没有机会后移了,front后移也要注意取模
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
// 获取队列中有效数据的个数
public int validSize() {
return (rear - front + maxSize) % maxSize;
}
// 显示队列的所有数据(遍历实现,循环队列是注意取模)
public void showQueue() {
if (isEmpt()) {
System.out.println("空队列,无数据");
return;
}
for (int i = front; i < front + validSize(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
// 显示队列的头数据(注意不是取出数据)
public int headQueue() {
if (isEmpt()) {
throw new RuntimeException("空队列,无数据");
}
return arr[front];
}
}
测试代码
import java.util.Scanner;
public class CircleArrayQueueTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 测试CircleArrayQueue
// 首先创建一个循环队列
CircleArrayQueue queue = new CircleArrayQueue(4);
// 因为我们定义循环队列的时候默认了一个空余的空间,所以实际有效数据最多为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 'e':
scanner.close();
loop = false;
break;
case 'a':
System.out.println("请输入要加入队列的数(int):");
int data = scanner.nextInt();
queue.addQueue(data);
break;
case 'g':
try {
int result = queue.getQueue();
System.out.printf("取出的数据是:%d\n", result);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int result = queue.headQueue();
System.out.printf("队列头的数据是:%d\n", result);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
System.out.println("程序已退出");
}
}
测试结果
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
4
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
6
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
8
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
9
队列满,无法加入数据!
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
arr[0]=4
arr[1]=6
arr[2]=8
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
g
取出的数据是:4
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
arr[1]=6
arr[2]=8
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
g
取出的数据是:6
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
g
取出的数据是:8
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
g
队列空,无法获取数据
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
空队列,无数据
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
a
请输入要加入队列的数(int):
8
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
s
arr[3]=8
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列中取出数据
h(head):显示队列头的数据
测试结果分析
如上图测试结果展示,我们完成了一个循环队列的创建,充分利用了数组的位置,队列对应于数组的索引达到了动态改变的效果。
收获
实现了数组模拟队列和循环的创建和增删改等一系列操作,这里注意一些算法的技巧,比如取模(也就是正数取余)可以实现环形数组,以后类似的题目可以凭经验很快给出解题方案。