一、使用数组实现队列的思路
![在这里插入图片描述](https://img-blog.csdnimg.cn/bc40d8ec9d7e43d9b170a4116e24d005.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2x1Y3lfaGFja2Vy,size_16,color_FFFFFF,t_70)
maxSize :队列的最大容量,初始值由程序员输入
front :随着数据输出(出队)而改变,初始值-1
rear :是随着数据输入(入队)而改变,初始值-1
队列为空:front == rear
队列为满:rear == maxSize-1
二、代码实现
package com.hao.demo;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @Author: Yzh
* @Date: 2021/07/28/9:24
* @Description:
*/
public class ArrayQueue {
public static void main(String[] args) {
Queue queue = new Queue(3);
Scanner scanner = new Scanner(System.in);
char key ;//接收用户数据
boolean loop = true;//是否循环
while (loop){
System.out.println("输入's',显示队列");
System.out.println("输入'e',退出程序");
System.out.println("输入'a',添加元素");
System.out.println("输入'g',获取元素");
System.out.println("输入'h',获取头元素");
key = scanner.next().charAt(0);
switch (key){
case 's':
queue.show();
break;
case 'e':
scanner.close();
loop = false;
break;
case 'a':
try{
System.out.println("请输入要添加的元素");
int data = scanner.nextInt();
queue.add(data);
}catch(RuntimeException exception){
System.out.println(exception.getMessage());
}
break;
case 'g':
try{
int res = queue.get();
System.out.println("取出的元素是:"+res);
}catch(RuntimeException exception){
System.out.println(exception.getMessage());
}
break;
case 'h':
try{
int head = queue.getHead();
System.out.println("取出的头元素是: "+head);
}catch(RuntimeException exception){
System.out.println(exception.getMessage());
}
break;
default:
break;
}
}
}
}
class Queue{
//成员变量
private int maxSize; //最大容量
private int front; //头指针
private int rear; //尾指针
private int[] array; //数组模拟队列
/**
* @Author: yzh
* @Description: 构造器
* @Param: [maxSize]
* @return:
* @Date: 2021/7/28
*/
public Queue(int maxSize) {
this.maxSize = maxSize;
array = new int[maxSize];
front = -1;//从-1开始,指向头元素的前一个位置
rear = -1;//直接指向最后的数据
}
/**
* @Author: yzh
* @Description: 判断队列是否为满
* @Param: []
* @return: boolean
* @Date: 2021/7/28
*/
public boolean isFull(){
//条件:rear == maxSize -1 为满
return rear == maxSize -1;
}
/**
* @Author: yzh
* @Description: 判断队列是否已空
* @Param: []
* @return: boolean
* @Date: 2021/7/28
*/
public boolean isEmpty(){
//条件:rear==front 为空
return rear == front;
}
/**
* @Author: yzh
* @Description: 添加元素到队列
* @Param: [data]
* @return: void
* @Date: 2021/7/28
*/
public void add(int data){
if(isFull()){
throw new RuntimeException("队列已满,无法加入");
}
array[++rear] = data;//注意是 ++rear
}
/**
* @Author: yzh
* @Description: 从队列中取出元素
* @Param: []
* @return: int
* @Date: 2021/7/28
*/
public int get(){
if(isEmpty()){
throw new RuntimeException("队列为空,无法取出元素");
}
return array[++front];
}
/**
* @Author: yzh
* @Description: 取出队列头元素
* @Param: []
* @return: int
* @Date: 2021/7/28
*/
public int getHead(){
if (isEmpty()){
throw new RuntimeException("队列为空,无法取出元素");
}
return array[front+1];
}
/**
* @Author: yzh
* @Description: 显示队列中的所有元素
* @Param: []
* @return: void
* @Date: 2021/7/28
*/
public void show(){
for (int i = 0; i < array.length; i++) {
System.out.printf("Queue[%d] :[%d]\n",i,array[i]);
}
}
}
总结
入队的时候是先 ++rear 数据再赋值
出队的时候也是 ++front 再得到数据
问题分析并优化
1) 目前数组使用一次就不能用, 没有达到复用的效果
2) 将这个数组使用算法,改进成一个环形的队列 取模:%