队列,这玩意用最简单的话来描述,就是先入先出的数据结构.
今天来用数组,模拟一个队列的有关操作(但是在复用性方面,还没有实现,但是简单的队列操作,都可以做出来)
先来说一下在队列中的几个基本概念
1.maxSize(队列的最大容量) 队列中的元素位置从0开始,所以最后一个位置应该是maxSize-1
2.front指针(刚开始为-1,指向队列头数据的前一个位置)
3.rear指针(刚开始为-1,指向队列的最后一个数据)
当加入一个元素进队列时,rear指针向后移一位
当从队列取出一个元素时,front指针向后移一位
所以可以得知,当front和rear指向同一个位置时,队列空 当rear指向maxSize-1时,队列满
下面就用列表来模仿以下队列的一些操作
先声明一些属性
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; //指向队列头,分析出front是指向队列头的前一个位置
rear = -1; //指向队列尾部,指向队列尾的数据(也就是队列的最后一个数据)
}
方法1:判断队列是否已满
//判断队列是否满
public boolean isFull(){
return rear == maxSize - 1;
}
方法2:判断队列是否为空
//判断队列是否为空
public boolean isEmpty(){
return rear == front;
}
方法3:添加数据到队列
//添加数据到队列
public void addQueue(int n){
if (!isFull()){
arr[++rear] = n;
}else{
throw new RuntimeException("sorry , the queue is full");
}
}
方法4:从队列中获取数据(出队)
//获取队列的数据,出队列
public int getQueue(){
if (!isEmpty()){
front++;
return arr[front];
}else{
throw new RuntimeException("sorry,there is no element can be gotten");
}
}
方法5:显示队列所有数据(不是出队)
//显示队列的所有数据
public void showQueue(){
if (isEmpty()){
System.out.println("the queue is empty");
throw new RuntimeException("sorry,there is no element can be shown");
}else{
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d",i,arr[i]);
System.out.println();
}
}
}
方法6:显示队列的头数据(不是出队)
//显示队列的头数据
public int headQueue(){
if (isEmpty()){
System.out.println("the queue is empty");
throw new RuntimeException("sorry,there is no element can be shown");
}else{
return arr[front+1];
}
}
整体测试:
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
//创建一个队列
ArrayQueue queue = new ArrayQueue(3);
char key = ' ';//用来接收用户传入的指令
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag){
System.out.println("=====1.显示队列=====");
System.out.println("=====2.退出程序=====");
System.out.println("=====3.添加数据到队列=====");
System.out.println("=====4.从队列中取出数据=====");
System.out.println("=====5.查看队列头的数据=====");
key = scanner.next().charAt(0);
switch (key){
case '1':
try {
queue.showQueue();
} catch (Exception e) {
System.out.println("队列为空");
}
break;
case '2':
flag = false;
break;
case '3':
System.out.println("=====请输入想要添加的数=====");
int i = scanner.nextInt();
try {
queue.addQueue(i);
} catch (Exception e) {
System.out.println("队列已满,无法执行");
}
break;
case '4':
try {
int result = queue.getQueue();
System.out.println("====已取出数字 " + result + " ====");
} catch (Exception e) {
System.out.println("队列为空,无法取出");
}
break;
case '5':
System.out.println("====目前队列头的数据为 " + queue.headQueue() + " ====");
break;
default:
System.out.println("请输入正确的指令");
}
}
}
}
//使用数组模拟队列--编写一个ArrayQueue队列
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; //指向队列头,分析出front是指向队列头的前一个位置
rear = -1; //指向队列尾部,指向队列尾的数据(也就是队列的最后一个数据)
}
//判断队列是否满
public boolean isFull(){
return rear == maxSize - 1;
}
//判断队列是否为空
public boolean isEmpty(){
return rear == front;
}
//添加数据到队列
public void addQueue(int n){
if (!isFull()){
arr[++rear] = n;
}else{
throw new RuntimeException("sorry , the queue is full");
}
}
//获取队列的数据,出队列
public int getQueue(){
if (!isEmpty()){
front++;
return arr[front];
}else{
throw new RuntimeException("sorry,there is no element can be gotten");
}
}
//显示队列的所有数据
public void showQueue(){
if (isEmpty()){
System.out.println("the queue is empty");
throw new RuntimeException("sorry,there is no element can be shown");
}else{
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d",i,arr[i]);
System.out.println();
}
}
}
//显示队列的头数据
public int headQueue(){
if (isEmpty()){
System.out.println("the queue is empty");
throw new RuntimeException("sorry,there is no element can be shown");
}else{
return arr[front+1];
}
}
}