基本介绍
队列是一个有序列表,可以使用数组或链表来实现。
队列遵循先进先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出。
数组模拟队列
因为队列的输入、输出分别是从前后两端来处理的,所以定义两个变量front、rear来分别表示队列的前后端的下标。
下面就用代码来实现简单的队列。
代码实现
数组模拟队列
因为这里涉及到了两种不同结构的队列,普通的队列与环形队列。所以这里编写一个抽象类(AbstractQueue),记录队列的公共方法。
队列都有添加数据(入队列)、获取数据(出队列)、打印数据(显示队列所有数据)、显示头数据等方法。
public abstract class AbstractQueue {
/**
* TODO 添加数据(入队列)
*
* @param n 数据
* @return void
*/
public abstract void enterQueue(int n);
/**
* TODO 获取数据(出队列)
*
* @return int
*/
public abstract int outQueue();
/**
* TODO 显示队列所有数据
*
* @return void
*/
public abstract void listQueue();
/**
* TODO 显示头数据
*
* @return int
*/
public abstract int headQueue();
}
下面就通过实现这个类,来实现数组模拟队列。
public class ArrayQueue extends AbstractQueue {
/**
* 数组的最大容量
*/
private final int maxSize;
/**
* 队列头
*/
private int front;
/**
* 队列尾
*/
private int rear;
/**
* 用于存储数据
*/
private final int[] arr;
/**
* TODO 初始化
*/
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[maxSize];
// 指向队列头部的前一个位置
this.front = -1;
// 指向队列尾部的数据
this.rear = -1;
}
/**
* TODO 判断队列是否 充满
*/
public boolean isFull() {
return this.rear == this.maxSize - 1;
}
/**
* TODO 判断队列是否为空
*/
public boolean isEmpty() {
return this.rear == this.front;
}
/**
* TODO 添加数据(入队列)
*/
@Override
public void enterQueue(int n) {
// 判断队列是否满
if (isFull()) {
System.err.println("队列已满,不能添加数据");
return;
}
// 后移
this.rear++;
this.arr[this.rear] = n;
}
/**
* TODO 从队列中获取数据(出队列)
*/
@Override
public int outQueue() {
// 判断队列是否为空
if (isEmpty()) {
throw new RuntimeException("队列为空,不能获取数据");
}
// 后移
this.front++;
return this.arr[this.front];
}
/**
* TODO 显示队列的所有数据
*/
@Override
public void listQueue() {
if (isEmpty()) {
System.err.println("队列为空");
return;
}
for (int i = 0; i < this.arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, this.arr[i]);
}
}
/**
* TODO 显示 队列 的 头数据
*/
@Override
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能获取数据");
}
return this.arr[this.front + 1];
}
}
这里我们将队列的前端(front)与队列的后端(rear)的初始值都设为 -1 。
判断队列是否已满:this.rear == this.maxSize - 1。
判断队列是否为空:this.rear == this.front。
上面的代码都很简单,基本都有注释,下面编写测试类。
public class Test {
public static void main(String[] args) {
AbstractQueue arrayQueue = new ArrayQueue(3);
testQueue(arrayQueue);
}
private static void testQueue(AbstractQueue queue) {
// 接收用户输入
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("s: 显示队列");
System.out.println("h: 显示队列的头部");
System.out.println("e: 退出程序");
System.out.println("a: 添加数据到队列");
System.out.println("g: 从队列中获取数据");
// 接收一个字符
key = scanner.next().charAt(0);
switch (key) {
case 's':
queue.listQueue();
break;
case 'h':
try {
int head = queue.headQueue();
System.out.printf("队列头部的数据是%d\n", head);
} catch (Exception e) {
System.err.println(e.getMessage());
}
break;
case 'e':
flag = Boolean.FALSE;
scanner.close();
break;
case 'a':
System.out.println("请输入一个数:");
int value = scanner.nextInt();
queue.enterQueue(value);
break;
case 'g':
try {
int res = queue.outQueue();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
System.err.println(e.getMessage());
}
break;
default:
break;
}
}
System.out.println("程序已退出");
}
}
至于测试结果就不给出了,感兴趣的读者,可直接复制代码,运行即可。
从上面的数组模拟队列可以看出。当队列充满的时候,就算从队列中取出数据,新的数据也不能添加到队列中。这样就造成了大量的空间浪费。所以为了充分利用数组。就要用将数组转换为环形队列。
数组模拟环形队列
public class RingArrayQueue extends AbstractQueue {
/**
* 表示数组的最大容量
*/
private final int maxSize;
/**
* 队列头,指向队列的第一个元素(默认为 0)
*/
private int front;
/**
* 队列尾,指向队列最后一个元素的后一个位置(默认为 0),希望空出一个空间
*/
private int rear;
/**
* 存放数据
*/
private final int[] arr;
/**
* TODO 构造器,初始化数组
*/
public RingArrayQueue(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[maxSize];
}
/**
* TODO 判断队列是否满(例如数组长度(maxSize)为3,则rear就为3[最后一个元素的角标为 2 + 1],)
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* TODO 判断队列是否为空
*/
public boolean isEmpty() {
return this.rear == this.front;
}
/**
* TODO 添加数据(入队列)
*/
@Override
public void enterQueue(int n) {
if (isFull()) {
System.err.println("队列已满,不能添加数据");
return;
}
arr[rear] = n;
// 取模, rear后移
rear = (rear + 1) % maxSize;
}
/**
* TODO 获取数据(出队列)
*/
@Override
public int outQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能获取数据");
}
// 1. 先把 front 对应的值保留到一个临时变量
int temp = arr[front];
// 2. 取模,将 front 后移
front = (front + 1) % maxSize;
// 3. 将临时保存的变量返回
return temp;
}
/**
* TODO 显示队列所有的数据
*/
@Override
public void listQueue() {
if (isEmpty()) {
System.err.println("队列为空....");
return;
}
// 从front开始遍历,遍历多少个元素
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
/**
* TODO 显示头部元素
*/
@Override
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能获取头元素");
}
return arr[front];
}
/**
* TODO 有效数据的个数
*/
private int size() {
return (rear + maxSize - front) % maxSize;
}
}
判断队列是否已满:(rear + 1) % maxSize == front。
判断队列是否为空:this.rear == this.front。
上面的代码基本上也有注释,环形队列理解起来还是有点绕,尽可能的画图跟代码慢慢理解吧。测试方法就和测试普通的队列一样。因为环形队列也是继承自 AbstractQueue 他们有公用的方法,所以测试方法就能重用了。
public static void main(String[] args) {
AbstractQueue ringArrayQueue = new RingArrayQueue(4);
testQueue(ringArrayQueue);
}
博主对于环形队列这里也不是特别理解,如果有读者有更多的见解,可以给博主留言。
参考资料:
作者:韩顺平
课程:《Java数据结构与算法》
天气因你逆转,世界因你天晴。