队列介绍
目录
- 队列是一个有序列表,可以用数组或者链表实现
- 先进先出,先存入队列的数据, 要先取出。 后存入的要后取出
数组模拟队列思路
- 队列本身就是有序列表,若使用数组的结构来储存队列的数据,则maxSize是从该队列的最大容量
- 因为队列的输出,输入是分别从前后端来处理,因此需要两个变量front以及rear分别记录队列前后端的下标,front 会随着数据输出而改变, 而 rear 则是随着数据输入而改变,
存入数据的思路,addQueue
-
将尾指针后移:rear+1,当front == rear 为空
-
若尾指针 rear 小于队列的最大下标 maxSize-1, 则将数据存入 rear 所指的数组元素中, 否则无法存入数据。rear == maxSize - 1[队列满]
public void addQueue(int n) { //判断队列是否满 if (isFull()) { System.out.println("队列已满"); return; } rear++; arr[rear] = n; }
取出数据getQueue
-
将头指针后移 front++;当front == rear 为空
public int getQueue() { //判断是否为空 if (isEmpty()) { //抛出异常 throw new RuntimeException("为空"); } front++; return arr[front]; }
创建队列
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 showQueue() {
//遍历
if (isEmpty()) {
System.out.println("队列为空");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.println("第" + i + "元素未:\t" + arr[i]);
}
}
查看头元素
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("为空");
}
return arr[front + 1];
}
完整代码
package com.nie.queue;
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
//测试
//4个数据
ArrayQueue arrayQueue = new ArrayQueue(4);
Scanner scanner = new Scanner(System.in);
boolean tag = true;//标签
while (tag) {
System.out.println("输入[s]\t 显示队列");
System.out.println("输入[e]\t 退出");
System.out.println("输入[a]\t 增添元素到队列");
System.out.println("输入[g]\t 由队列中取出数据");
System.out.println("输入[h]\t 查看头部的一个数据");
char key = scanner.next().charAt(0);//接收一个字符
switch (key){
case 's':
arrayQueue.showQueue();
break;
case 'e':
//退出
scanner.close();
tag = false;
break;
case 'a':
System.out.println("输入一个数据");
int val=scanner.nextInt();
arrayQueue.addQueue(val);
break;
case 'g':
try {
int res=arrayQueue.getQueue();
System.out.println("取数的数据为\t:"+res);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 'h':
int res = arrayQueue.headQueue();
System.out.printf("队列头的数据是%d\n", res);
break;
default:
break;
}
}
}
}
class ArrayQueue {
private int maxSize;//表示最大的总量
private int front;//头
private int rear;//尾部
private int[] arr;//利用数组存放数据,模拟队列
/**
* 创建队列
*
* @param arrmaxSize
*/
public ArrayQueue(int arrmaxSize) {
maxSize = arrmaxSize;
arr = new int[maxSize];
front = -1;//指向队列头部,分析出front是指向队列头的一个位置
rear = -1;//指向尾部,
}
/**
* 判断队列是否满
*/
public boolean isFull() {
return rear == maxSize - 1;
}
/**
* 判断是否为空
*
* @return
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 添加元素
*/
public void addQueue(int n) {
//判断队列是否满
if (isFull()) {
System.out.println("队列已满");
return;
}
rear++;
arr[rear] = n;
}
/**
* 取出数据
* @return
*/
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.println("第" + i + "元素未:\t" + arr[i]);
}
}
/**
* 查看头元素
*
* @return
*/
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("为空");
}
return arr[front + 1];
}
}
数组的循环队列
概述
利用数组,将数组看成为一个环形,(通过取模来实现)
说明
- 尾索引的下一个为头索引时表示队列满, (rear + 1) % maxSize == front 满
- rear == front 队空
- ront 指向队列元素的一个元素,初始值为 0
- rear 指向队列的最后一个元素的后一个位置. 空出一个空间,以便控制是否满. 初始值为 0
- 判断队列满 (rear + 1) % maxSize == front
- rear == front 队空
- 有效的元素为:(rear+maxSize-front)%maxSize
代码实现
显示所有数据
通过取模 i%maxSize 获取数据
System.out.println(“第” + i%maxSize + “元素未:\t” + arr[i%maxSize]);
public void showQueue() {
//遍历
if (isEmpty()) {
System.out.println("队列为空");
return;
}
for (int i = front; i < front+size(); i++) {
System.out.println("第" + i%maxSize + "元素未:\t" + arr[i%maxSize]);
}
}
取出数据
- 这里需要分析出 front 是指向队列的第一个元素
先把 front 对应的值保留到一个临时变量 int val=arr[front]; - 将 front 后移, 考虑取模 front=(front+1)%maxSize;
- 将临时保存的变量返回 return val;
public int getQueue() {
//判断是否为空
if (isEmpty()) {
//抛出异常
throw new RuntimeException("为空");
}
//将头部元素赋值
int val=arr[front];
front=(front+1)%maxSize;
return val;
}
添加元素
- 直接将数据加入 arr[rear] = n;
- 将 rear 后移, 这里必须考虑取模 rear = (rear + 1) % maxSize;
public void addQueue(int n) {
//判断队列是否满
if (isFull()) {
System.out.println("队列已满");
return;
}
//直接将数据加入
arr[rear] = n;
//将 rear 后移, 这里必须考虑取模
rear = (rear + 1) % maxSize;
}
完整代码
package com.nie.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//测试
//4个数据
CircleArray circleArray = new CircleArray(3);
Scanner scanner =new Scanner(System.in);
boolean tag = true;//标签
while (tag) {
System.out.println("当前的数据个数为:\t"+circleArray.size());
System.out.println("输入[s]\t 显示队列");
System.out.println("输入[e]\t 退出");
System.out.println("输入[a]\t 增添元素到队列");
System.out.println("输入[g]\t 由队列中取出数据");
System.out.println("输入[h]\t 查看头部的一个数据");
char key = scanner.next().charAt(0);//接收一个字符
switch (key){
case 's':
circleArray.showQueue();
break;
case 'e':
//退出
scanner.close();
tag = false;
break;
case 'a':
System.out.println("输入一个数据");
int val=scanner.nextInt();
circleArray.addQueue(val);
break;
case 'g':
try {
int res=circleArray.getQueue();
System.out.println("取数的数据为\t:"+res);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 'h':
int res = circleArray.headQueue();
System.out.printf("队列头的数据是%d\n", res);
break;
default:
break;
}
}
}
}
class CircleArray {
private int maxSize;//表示最大的总量
//front 指向队列元素的一个元素,初始值为 0
private int front;//头
//rear 指向队列的最后一个元素的后一个位置. 空出一个空间,以便控制是否满. 初始值围殴 0
private int rear;//尾部
private int[] arr;//利用数组存放数据,模拟队列
/**
* 创建队列
*
* @param arrmaxSize
*/
public CircleArray(int arrmaxSize) {
maxSize = arrmaxSize;
arr = new int[maxSize];
}
/**
* 判断队列是否满
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* 判断是否为空
*
* @return
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 添加元素
*/
public void addQueue(int n) {
//判断队列是否满
if (isFull()) {
System.out.println("队列已满");
return;
}
//直接将数据加入
arr[rear] = n;
//将 rear 后移, 这里必须考虑取模
rear = (rear + 1) % maxSize;
}
/**
* 取出数据
* @return
*/
public int getQueue() {
//判断是否为空
if (isEmpty()) {
//抛出异常
throw new RuntimeException("为空");
}
//将头部元素赋值
int val=arr[front];
front=(front+1)%maxSize;
return val;
}
/**
* 显示所有数据
*/
public void showQueue() {
//遍历
if (isEmpty()) {
System.out.println("队列为空");
return;
}
for (int i = front; i < front+size(); i++) {
System.out.println("第" + i%maxSize + "元素未:\t" + arr[i%maxSize]);
}
}
/**
* 取出元素
*
* @return
*/
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("为空");
}
return arr[front + 1];
}
/**
* 当前有效数据的个数
* @return
*/
public int size(){
return (rear+maxSize-front)%maxSize;
}
}