队列介绍
- 队列是一个有序列表,可以用数组或者链表来实现。
- 遵循先入先出原则。即:先存入队列的数据要先取出,后存入的要后取出。
数组模拟队列
- 当我们将数据存入队列时称为add,add的处理需要有两个步骤:
- 将尾指针往后移:rear + 1,当 front == rear 时队列为空
- 若尾指针rear小于队列的最大小标maxSize - 1,则将数据存入rear所指的数组元素中,否则无法存入数据。rear == maxSize - 1 时队列满
代码实现
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(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("q(quit): 退出程序");
key = scanner.next().charAt(0);
switch (key){
case 's':
arrayQueue.show();
break;
case 'a':
System.out.println("输入一个数");
arrayQueue.add(scanner.nextInt());
break;
case 'g':
try {
int value = arrayQueue.get();
System.out.printf("取出的数据是%d\n",value);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int value = arrayQueue.showHead();
System.out.printf("队列头数据是%d\n",value);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'q':
loop = false;
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
}
}
}
class ArrayQueue{
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int maxSize){
this.maxSize = maxSize;
arr = new int[maxSize];
front = -1;
rear = -1;
}
public boolean isFull(){
return rear == maxSize - 1;
}
public boolean isEmpty(){
return rear == front;
}
public void add(int n){
if(isFull()){
System.out.println("队列已满");
return;
}
rear++;
arr[rear] = n;
}
public int get(){
if(isEmpty()){
throw new RuntimeException("队列为空,不能取数据");
}
front++;
return arr[front];
}
public void show(){
if(isEmpty()){
System.out.println("队列为空");
}
for (int i = front + 1; i <= rear; i++) {
System.out.printf("arr[%d]=%d\n",i,arr[i]);
}
}
public int showHead(){
if(isEmpty()){
System.out.println("队列为空");
}
return arr[front+1];
}
}
- 问题分析并优化
- 目前数组使用一次就不能再使用,没有达到复用效果
- 将这个数组使用算法,改进成一个环形队列
数组模拟环形队列
思路分析
- front变量的含义调整:front就指向队列的第一个元素,即arr[front]就是队列的第一个元素,front初始值 = 0
- rear变量的含义调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定,rear初始值 = 0
- 当队列满时,条件是
(rear + 1) % maxSize = front - 当队列空时,条件是front == rear
- 队列中有效的数据的个数 (rear + maxSize - front) % maxSize
代码实现
public class CircleArrayQueueDemo {
public static void main(String[] args) {
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("a(add): 添加数据到队列");
System.out.println("g(get): 取出队列数据");
System.out.println("h(head): 显示队列头数据");
System.out.println("q(quit): 退出程序");
key = scanner.next().charAt(0);
switch (key){
case 's':
queue.show();
break;
case 'a':
System.out.println("输入一个数");
queue.add(scanner.nextInt());
break;
case 'g':
try {
int value = queue.get();
System.out.printf("取出的数据是%d\n",value);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int value = queue.showHead();
System.out.printf("队列头数据是%d\n",value);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'q':
loop = false;
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
}
}
}
class CircleArrayQueue{
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleArrayQueue(int maxSize){
this.maxSize = maxSize;
arr = new int[maxSize];
front = 0;
rear = 0;
}
public boolean isFull(){
return (rear + 1) % maxSize == front;
}
public boolean isEmpty(){
return rear == front;
}
public void add(int n){
if(isFull()){
System.out.println("队列已满");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
public int get(){
if(isEmpty()){
throw new RuntimeException("队列已空");
}
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
public void show(){
if(isEmpty()){
System.out.println("队列已空");
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
public int size(){
return (rear + maxSize - front) % maxSize;
}
public int showHead(){
if(isEmpty()){
throw new RuntimeException("队列已空");
}
return arr[front];
}
}
- 创建 CircleArrayQueue(4) 实际只能存储 3 个元素
- 这是因为 isFull() 方法使用 (rear + 1) % maxSize == front 判断队满
- 需要牺牲一个数组位置来区分队空和队满状态