一、队列简介
-
队列是一个有序列表,可以用数组或是链表来实现。
-
遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
-
示意图:(使用数组模拟队列示意图)
二、数组实现队列
-
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量
-
因为队列的输入、输出分别是从前后端来处理的,因此需要两个变量front和rear分别记录队列的头和尾
-
代码实现
package org.westos.queue; 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 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': 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("取出的数据是%d\n", res); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } break; case 'h': //查看队列头的数据 try { int res = queue.headQueue(); System.out.printf("队列头的数据是%d\n", res); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } break; case 'e': //退出 scanner.close(); loop = false; break; default: break; } } System.out.println("程序退出~~"); } } 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; rear = -1; } //判断队列是否满 public boolean isFull(){ return rear == maxSize-1; } //判断队列是否为空 public boolean isEmpty(){ return rear == front; } //添加数据到队列 public void addQueue(int n){ //添加前先判断队列是否满了 if (isFull()){ throw new RuntimeException("队列已满,不能添加元素"); } rear++; arr[rear] = n; } //从队列中获取数据 public int getQueue(){ //判断队列是否为空 if (isEmpty()){ throw new RuntimeException("队列为空,不能取出元素"); } front++; return arr[front]; } //显示队列所有数据 public void showQueue(){ if (isEmpty()){ System.out.println("队列为空,没有数据"); } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+"\t"); } System.out.println(); } //返回队列的头数据 public int headQueue(){ if (isEmpty()){ throw new RuntimeException("队列为空,没有数据"); } return arr[front+1]; } }
三、数组模拟环形队列
对前面的数组模拟队列的优化,充分利用数组. 因此将数组看做是一个环形的。(通过取模的方式来实现即可)
代码实现:
package org.westos.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//测试一把
System.out.println("测试数组模拟环形队列的案例~~~");
// 创建一个环形队列
CircleArrayQueue queue = new CircleArrayQueue(4); //说明设置4, 其队列的有效数据最大是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':
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("取出的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h': // 查看队列头的数据
try {
int res = queue.headQueue();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e': // 退出
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~");
}
}
class CircleArrayQueue{
private int maxSize;//队列最大容量
private int front;//队列头,此处指向度列的第一个元素,初始值为0
private int rear;//队列尾,指向队列最后一个元素的后一个位置,初始值为0
private int[] arr;//数组用于存放数据,模拟队列
//创建队列构造器
public CircleArrayQueue(int maxSize){
this.maxSize = maxSize;
arr = new int[maxSize];
front = 0;
rear = 0;
}
//判断队列是否满
public boolean isFull(){
return (rear+1) % maxSize == front;
}
//判断队列是否为空
public boolean isEmpty(){
return rear == front;
}
//添加数据到队列
public void addQueue(int n){
//添加前先判断队列是否满了
if (isFull()){
throw new RuntimeException("队列已满,不能添加元素");
}
arr[rear] = n;
rear = (rear+1)%maxSize;
}
//从队列中获取数据
public int getQueue(){
//判断队列是否为空
if (isEmpty()){
throw new RuntimeException("队列为空,不能取出元素");
}
//先将front的值保存到一个临时变量
int value = arr[front];
front = (front+1) % maxSize;
return value;
}
//显示队列所有数据
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空,没有数据");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i%maxSize]+"\t");
}
System.out.println();
}
//返回队列的头数据
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,没有数据");
}
return arr[front];
}
//求出当前队列有效数字个数
public int size(){
return (rear+maxSize-front) % maxSize;
}
}
//返回队列的头数据
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,没有数据");
}
return arr[front];
}
//求出当前队列有效数字个数
public int size(){
return (rear+maxSize-front) % maxSize;
}
}