队列
队列是一种先进先出的数据结构,First In First Out(FIFO)。
队列只能从队尾添加元素,同时也只能从队头取出元素。
1. 使用数组模拟队列
1.1 思路分析
- front初始值为-1,指向队列头的前一个位置
- rear初始值为-1,指向队列的最后一个元素
- 入队,往队尾添加元素,在添加前判断队列是否已满,尾指针自增
- 出队,从队头取出元素,在取出前判断队列是否为空,头指针自增
1.2 代码实现
public class ArrayQueue {
public static void main(String[] args) {
ArrQueue arrQueue = new ArrQueue(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':
arrQueue.showQueue();break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
arrQueue.addQueue(value);break;
case 'g':
try {
int res = arrQueue.getQueue();
System.out.println("取出的数据是"+res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = arrQueue.getHead();
System.out.println("队列头的数据是"+res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:break;
}
}
System.out.println("程序退出");
}
}
class ArrQueue{ //创建一个用数组实现的队列
private int maxSize; //表示数组的最大容量
private int front; //队列头,front指向队列头的前一个位置
private int rear; //队列尾
private int[] arr; //用于存放数据,模拟队列
public ArrQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
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.println("队列已满");
return;
}
rear++; //再尾指针后移
arr[rear] = n; //最后将数据插入到队尾
}
public int getQueue(){ //出队
if (isEmpty()){ //首先判断队列是否为空
throw new RuntimeException("队列为空");
}
front++; //再头指针后移
return arr[front]; //返回头指针指向的元素
}
public int getHead(){ //取队头元素
if (isEmpty()){
throw new RuntimeException("队列为空");
}
return arr[front + 1];
}
public void showQueue(){ //打印队列
if (isEmpty()){
System.out.println("队列为空");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.println("arr["+i+"] = "+arr[i]);
}
}
}
2. 使用数组模拟环形队列
2.1 思路分析
- front初始值为0,指向队列的第一个元素
- rear初始值为0,指向队列的最后一个元素的后一个位置,因为要空出一个元素作为约定。
- 判断队满:(rear + 1)%maxSize == front
- 判断队空:rear == front
- 队列有效数据个数:(rear - front + maxSize)%maxSize
2.2 代码实现
public class CircleArrayQueue {
public static void main(String[] args) {
CircleArray circleArray = new CircleArray(4); //创建一个环形队列,其队列的有效数据最大是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':
circleArray.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
circleArray.addQueue(value);
break;
case 'g':
try {
int res = circleArray.getQueue();
System.out.println("取出的数据是"+res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = circleArray.getHead();
System.out.println("队列头的数据是"+res);
}catch (Exception e){
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; //队列头,front指向队列的第一个元素
private int rear; //队列尾,rear指向队列的最后一个元素的后一个位置
private int[] arr; //用于存放数据,模拟队列
public CircleArray(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
}
public boolean isFull(){ //判断队列是否已满
return (rear + 1)%maxSize == front;
}
public boolean isEmpty(){ //判断队列是否为空
return rear == front;
}
public void addQueue(int n){ //入队
if (isFull()){ //判断队列是否已满
System.out.println("队列已满");
return;
}
arr[rear] = n;
rear = (rear + 1)%maxSize; //需要考虑越界的问题
}
public int getQueue(){ //出队
if (isEmpty()){ //判断队列是否为空
throw new RuntimeException("队列为空");
}
int value = arr[front];
front = (front + 1)%maxSize;
return value;
}
public void showQueue(){ //打印队列
if (isEmpty()){
System.out.println("队列为空");
return;
}
for (int i = front; i < front + getSize(); i++) {
System.out.println("arr["+(i % maxSize)+"] = "+arr[i % maxSize]);
}
}
public int getSize(){ //得到有效数据的个数
return (rear - front + maxSize)%maxSize;
}
public int getHead(){ //取队头元素
if (isEmpty()){
throw new RuntimeException("队列为空");
}
return arr[front];
}
}