队列
提出一个问题:什么是队列?
- 队列是一个有序列表,可以是数组和链表来实现。(不知道链表是什么?没关系,后面会说到)
- 队列遵循的先入先出的原则。
这里MaxSize是数组的最大容量
front是队列头,rear是队列尾
MaxSize-1是队列以数组实现的下表是从0开始的
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;//指向队列尾指向队列尾的数据(最后一个数据)
}
加入一下方法
方法1:判断队列是否为满
public boolean isFull()
{
return rear == maxSize-1;
}
方法2:判断队列是否为空
public boolean isEmpty()
{
return rear == front;
}
方法3:添加数据到队列
public void addQueue(int n)
{
//判断是否满
if (isFull())
{
System.out.printf("队列满,无法加入数据");
return;
}
rear++;
arr[rear] = n;
}
方法4:获取数据,出队列
public int getQueue()
{
//判断队列是否为空
if (isEmpty())
{
//抛出异常
throw new RuntimeException("队列为空,不可读取数据");
}
front++;
return arr[front];
}
方法5:显示队列里的所有数据
public void showQueue()
{
//遍历
if (isEmpty())
{
System.out.println("队列为空");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d] = %d\n",i,arr[i]);
}
}
方法6:显示队列的头数据,注意不是取出数据
public int headQueue()
{
//判断
if (isEmpty())
{
throw new RuntimeException("队列为空,没有数据");
}
return arr[front+1];
}
完整的ArrayQueue代码如下
package queue.ArrayQueue;
public 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;//指向队列尾指向队列尾的数据(最后一个数据)
}
//判断队列是否满
public boolean isFull()
{
return rear == maxSize-1;
}
//判断是否为空
public boolean isEmpty()
{
return rear == front;
}
//添加数据到队列
public void addQueue(int n)
{
//判断是否满
if (isFull())
{
System.out.printf("队列满,无法加入数据");
return;
}
rear++;
arr[rear] = n;
}
//获取队列的数据,出队列
public int getQueue()
{
//判断队列是否为空
if (isEmpty())
{
//抛出异常
throw new RuntimeException("队列为空,不可读取数据");
}
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] = %d\n",i,arr[i]);
}
}
//显示队列的头数据,注意不是取出数据
public int headQueue()
{
//判断
if (isEmpty())
{
throw new RuntimeException("队列为空,没有数据");
}
return arr[front+1];
}
}
然后我们来实现
package queue.ArrayQueue;
import java.util.Scanner;
public class queueDemo01 {
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("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列中取出数据");
System.out.println("h(head):查看队列头数据");
key = scanner.next().charAt(0);
switch (key){
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.printf("输入一个数:");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是:%d",res);
}catch (Exception e){
e.getMessage();
}
break;
case 'h'://查看队列头的数据
try {
int res = arrayQueue.headQueue();
System.out.printf("队列头数据:%d",res);
}catch (Exception e){
e.getMessage();
}
case 'e':
scanner.close();
loop = false;
default:
break;
}
}
System.out.println("程序退出");
}
}
但目前以数组模拟队列,只能使用一次,不能复用。
环形数组可以解决这个问题,将在下一章中给出。