队列是一个有序列表,可以用数组或链表实现.
遵循先入先出的原则。即,先存入队列的数据,要先取出。后存入的要后取出。
队列本身是有序链表,若使用数组的结构来存入队列的数据,则队列数组的声明如下,其中maxSize是该队列的最大容量。
因为队列的输出,输入是分别从前后端来处理的。因此需要两个变量front及rear分别记录队列前后端的下标。front会随着数据输出而改变,而rear则随着数据输入而改变。
数组实现队列
package com.queue;
import java.util.Scanner;
public class ArrayQueueDemo {
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.println("请输入一个数:");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g': // 取出数据
try{
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是:%d\n",res);
}catch(Exception e){
System.out.println(e.getMessage());
}
break;
case 'h': // 查看队列的头部
try{
int head = arrayQueue.headQueue();
System.out.printf("队列的头部数据是:%d\n",head);
}catch(Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
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; // 指向队列头部,不包含队列头,也就是指向队列中第一个元素的前一个位置
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++; // 让rear后移
arr[rear] = n;
}
// 出队列
public int getQueue(){
// 判断队列是否空
if(isEmpty()){
// 抛出异常
throw new RuntimeException("队列为空,不能取数");
}
front++; // front后移
return arr[front];
}
// 显示队列的所有数据
public void showQueue(){
if(isEmpty()){
System.out.println("队列为空");
return;
}
for(int i = 0; i<arr.length; i++){
System.out.printf("%d\n", arr[i]);
}
}
public int headQueue(){
if(isEmpty()){
throw new RuntimeException("队列为空,,,");
}
return arr[front+1];
}
}
优化: 数组模拟环形队列
front变量的含义做一个调整:front数指向队列的第一个元素,也就是说arr[front]数是队列的第一个元素。 front = 0
rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置。这样可以空出一个空间做为约定。rear = 0
当队列满时, 条件是 (rear+1)%maxSize == front
当队列空时,条件是 rear == front
队列中的个数: (rear + maxSize - front)%maxSize
package com.queue;
import java.util.Scanner;
public class CircleQueueArrayDemo {
public static void main(String[] args){
System.out.println("测试数组模拟环形队列的案例~~");
CircleArray circleQueue = new CircleArray(4); // 设置maxSize=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':
circleQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数:");
int value = scanner.nextInt();
circleQueue.addQueue(value);
break;
case 'g': // 取出数据
try{
int res = circleQueue.getQueue();
System.out.printf("取出的数据是:%d\n",res);
}catch(Exception e){
System.out.println(e.getMessage());
}
break;
case 'h': // 查看队列的头部
try{
int head = circleQueue.headQueue();
System.out.printf("队列的头部数据是:%d\n",head);
}catch(Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出....");
}
}
//使用数组模拟队列, 编写一个ArrayQueue类
class CircleArray{
private int maxSize; // 表示数组的最大容量
// front变量的含义做一个调整:front数指向队列的第一个元素,也就是说arr[front]数是队列的第一个元素。 front = 0
private int front;
// rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置。这样可以空出一个空间做为约定。rear = 0
private int rear;
private int[] arr; // 该数组用于存取数据,模拟队列
public CircleArray(int maxSize){
this.maxSize = maxSize;
front = 0;
rear = 0;
arr = new int[maxSize];
}
// 队列是否已满
public boolean isFull(){
return (rear + 1)%maxSize == front;
}
// 队列是否为空
public boolean isEmpty(){
return front == rear;
}
// 添加数据到队列-- 入队列
public void addQueue(int n){
// 判断队列是否满
if(isFull()){
System.out.println("队列已满,不能再加入数据");
return;
}
arr[rear] = n; // 直接将数据加入队列
rear = (rear + 1)%maxSize; // 让rear后移,指向队列最后一个元素的下一个空间
}
// 出队列
public int getQueue(){
// 判断队列是否空
if(isEmpty()){
// 抛出异常
throw new RuntimeException("队列为空,不能取数");
}
// 1.先把front对应的值保留到一个临时变量
int res = arr[front];
// 2.front后移
front = (front + 1)%maxSize;
// 3.将临时保存的变量值返回
return res;
}
// 显示队列的所有数据
public void showQueue(){
if(isEmpty()){
System.out.println("队列为空");
return;
}
// (rear+maxSize-front)%maxSize 为队列元素的总个数
for(int i = 0; i<(rear+maxSize-front)%maxSize; i++){
System.out.printf("a[%d]=%d\n", (front+i)%maxSize,arr[(front+i)%maxSize]);
}
}
// 显示队列的第一个元素
public int headQueue(){
if(isEmpty()){
throw new RuntimeException("队列为空,,,");
}
return arr[front];
}
}