概述
代码实现
编写一个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; // 指向队列尾,rear是指向队列尾的位置
}
// 判断队列是否满
public boolean isFull() {
return rear == maxSize-1;
}
// 判断队列是否为空
public boolean isEmpty() {
return front == rear;
}
// 添加数据到队列
public void addQueue(int n) {
// 判断队列是否满
if (isFull()) {
System.out.println("队列满,不能加入数据~");
return;
}
rear++; // 将 rear 后移
arr[rear] = n;
}
// 获取队列的数据, 出队列
public int getQueue() {
// 判断队列是否为空
if (isEmpty()) {
// 通过抛出异常
throw new RuntimeException("队列空,不能获取数据~");
}
front++; //front后移
return arr[front];
}
// 显示队列的所有数据
public void showQueue() {
// 遍历
if (isEmpty()) {
System.out.println("队列空,没有数据~");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%dn", i, arr[i]);
}
}
// 显示队列的头数据, 注意不是取出数据
public int headQueue() {
// 判断
if (isEmpty()) {
throw new RuntimeException("队列空,没有数据~");
}
return arr[front+1];
}
}
测试代码
package com.atguigu.queue;
import java.util.Scanner;
/**
* @author WHT
* @create 2021-06-01 11:01
*/
public class ArrayQueueDemo {
public static void main(String[] args) {
// 创建一个队列
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("a(add): 添加数据到队列");
System.out.println("g(get): 从队列取出数据");
System.out.println("h(head): 查看队列头的数据");
System.out.println("e(exit): 退出程序");
key = scanner.next().charAt(0); // 接收一个字符
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("请输入一个数:");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res = queue.getQueue();
System.out.printf("取出的数据是%dn", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.headQueue();
System.out.printf("队列头的数据是%dn", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~");
}
}
运行结果
D:javajdk-11.0.11binjava.exe "-javaagent:D:IntelliJ IDEA 2020.1.1libidea_rt.jar=4157:D:IntelliJ IDEA 2020.1.1bin" -Dfile.encoding=UTF-8 -classpath E:WHTProjectJavaDataStructuresoutproductionDataStructures com.atguigu.queue.ArrayQueueDemo
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
s
队列空,没有数据~
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
a
请输入一个数:
10
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
s
arr[0]=10
arr[1]=0
arr[2]=0
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
a
请输入一个数:
20
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
s
arr[0]=10
arr[1]=20
arr[2]=0
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
a
请输入一个数:
30
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
s
arr[0]=10
arr[1]=20
arr[2]=30
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
a
请输入一个数:
40
队列满,不能加入数据~
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
h
队列头的数据是10
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
h
队列头的数据是10
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
g
取出的数据是10
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
h
队列头的数据是20
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
s
arr[0]=10
arr[1]=20
arr[2]=30
s(show): 显示队列
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
e(exit): 退出程序
e
程序退出~~
Process finished with exit code 0
最后
以上就是暴躁马里奥为你收集整理的尚硅谷Java数据结构与算法——012数组模拟队列的全部内容,希望文章能够帮你解决尚硅谷Java数据结构与算法——012数组模拟队列所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复