数组模拟队列的实现基础之上,对队列进行优化(数组的复用等),采用环形队列的方式。
上一篇数组模拟队列:https://blog.csdn.net/qq_42120059/article/details/107864815
思路分析:
思路整理:
1.初始值的设定:
front初始值为0,rear初始值也为0
2.变量含义的调整:
front变量的含义——指向队列的第一个元素,arr[front]指的是队列的第一个元素。
rear变量的含义——指向队列的最后一个元素的后一个位置(原因:留出一个空间)
3.判满:
(rear+1)%maxSiza == front
4.判空:
rear==front
5.有效数据个数判定:
(rear+maxSize-front)%maxSize //可自行举例分析
代码实现:
package com.jun._2_queue;
import java.util.Scanner;
/**
* 测试环形队列
*
* @author jun
*
*/
public class CircleArrayQueue {
public static void main(String[] args) {
// 创建一个环形队列
CircleArray arrQueue = new CircleArray(4);// 队列有效数据数3
char key = ' ';// 接受输入
Scanner scanner = new Scanner(System.in);
boolean loop = true;
// 输出菜单
while (loop) {
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据");
System.out.println("s(show):显示队列");
System.out.println("g(get):从队列获取数据");
System.out.println("h(head):查看头部数据");
key = scanner.next().charAt(0);// 接收用户传入的一个字符
switch (key) {
case 's':
arrQueue.show();
break;
case 'a':
System.out.println("请输入要添加的数");
int value = scanner.nextInt();
arrQueue.add(value);
break;
case 'g':// 取数据可能会发生无数据可取的异常
try {
int res = arrQueue.get();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = arrQueue.headQueue();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出!");
}
}
class CircleArray {
private int maxSize;// 数组最大容量
private int front;// 头
private int rear;// 尾
private int[] arr;// 模拟数组
// 构造器
public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = 0;// 默认就是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("队列为空");// 跳出
}
// 这里要分析front是指向队列的第一个元素
// 1、先把front的值保存到一个临时变量
// 2、将front后移
// 3、将临时保存的变量返回
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
// 显示队列数据
public void show() {
if (isEmpty()) {
System.out.println("队列为空,无法遍历");
}
// 思路:从front开始遍历,遍历多少个元素(加上有效数据个数)。
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 headQueue() {
if (isEmpty()) {
System.out.println("队列为空,无法遍历");
}
return arr[front];
}
}
后台输出:
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
a
请输入要添加的数
10
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
a
请输入要添加的数
20
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
a
请输入要添加的数
30
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
s
arr[0]=10
arr[1]=20
arr[2]=30
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
g
取出的数据是10
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
s
arr[1]=20
arr[2]=30
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
a
请输入要添加的数
40
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据
s
arr[1]=20
arr[2]=30
arr[3]=40
e(exit):退出程序
a(add):添加数据
s(show):显示队列
g(get):从队列获取数据
h(head):查看头部数据