带你走进Java数据结构与算法(三)
🤗前言:上文学习了稀疏数组,本文带领大家走进队列~
队列
- ==有序列表==,用数组或链表来实现
- ==先进先出== :存入队列的数据,要先取出
- front 随着数据出队而改变
- rear 随着数据入队而改变
队列操作
- 队空条件:front == rear == null,rear + 1
- 队满条件:rear = maxSize - 1,则继续入队,否则队满:rear = maxSize - 1
import java.util.Scanner;
/**
* @author Kcs 2022/8/7
*/
public class ArrayQueueDemo1 {
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.println("取出的数据是:" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
//查看队列头数据
case 'h':
try {
int res = queue.headQueue();
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("程序退出");
}
}
//使用数据模拟队列编写一个ArrayQueue类
class ArrayQueue {
/**
* 表示数组的最大容量
*/
private int maxSize;
/**
* 队列头
*/
private int front;
/**
* 队列尾
*/
private int rear;
/**
* 存放数据数组
*/
private int[] array;
/**
* 构造器
*/
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
array = 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++;
array[rear] = n;
}
/**
* 出队操作
*/
public int getQueue() {
//判断是否为空
if (isEmpty()) {
throw new RuntimeException("队列为空,没有数据可取");
}
// front后移
front++;
return array[front];
}
/**
* 显示队列数据
*/
public void showQueue() {
// 遍历队列
if (isEmpty()) {
System.out.println("队列为空,没有数据!!");
return;
}
for (int i = 0; i < array.length; i++) {
System.out.printf("array[%d] = %d\n", i, array[i]);
}
}
/**
* 显示队列头部数据,不是取数据
*/
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,没有数据~~");
}
return array[front + 1];
}
}
此时把内容取出完,但也不能添加入数据,还在占用空间
环形队列
分析
- front 变量的含义做一个调整:front 指向队列的第一个元素,array[front] ,front 的初始值 = 0
- rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置,空出一个空间作为约定,rear 初始值 = 0
- 队列==满==条件:(rear + 1) % maxSize = front
- 队列为==空==的条件:rear = front
- 队列中有效数据的个数:( rear +maxSize - front ) % maxSize // rear =1 , front = 0
import java.util.Scanner;
/**
* @author Kcs 2022/8/7
*/
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//创建一个环形队列对象
// 队列有效数据最多只有3个
CircleQueue queue = new CircleQueue(4);
//接收用户输入
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.println("取出的数据是:" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
//查看队列头数据
case 'h':
try {
int res = queue.headQueue();
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("程序退出");
}
}
//使用数据模拟队列编写一个ArrayQueue类
class CircleQueue {
/**
* 表示数组的最大容量
*/
private int maxSize;
/**
* front 指向队列的第一个元素,array[front] ,front 的初始值 = 0
*/
private int front;
/**
* 1. rear 指向队列的最后一个元素的后一个位置,空出一个空间作为约定,rear 初始值 = 0
*/
private int rear;
/**
* 存放数据数组
*/
private int[] array;
/**
* 构造器
*/
public CircleQueue(int arrMaxSize) {
maxSize = arrMaxSize;
array = new int[maxSize];
}
/**
* 判断队列是否满 (rear + 1) % maxSize == front
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* 判断队列是否为空 rear = front
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 入队操作,添加数据
*/
public void addQueue(int n) {
//判断是否队满
if (isFull()) {
System.out.println("队满,不可以添加数据");
return;
}
// 直接将数据加入
array[rear] = n;
//将rear后移,取模,判断队满,防止地址溢出
rear = (rear + 1) % maxSize;
}
/**
* 出队操作,取数据
*/
public int getQueue() {
//判断是否为空
if (isEmpty()) {
throw new RuntimeException("队列为空,没有数据可取");
}
// front直接指向队列的第一个元素
//1. front 对应的值保存到临时变量
int value = array[front];
//2. front 后移,取模,防止地址溢出
front = (front + 1) % maxSize;
//3.将临时保存的变量返回
return value;
}
/**
* 显示队列数据
*/
public void showQueue() {
// 遍历队列
if (isEmpty()) {
System.out.println("队列为空,没有数据!!");
return;
}
//从front开始遍历,有效个数:( rear +maxSize - front ) % maxSize
for (int i = front; i < front + effectiveValue(); i++) {
System.out.printf("array[%d] = %d\n", i % maxSize, array[i % maxSize]);
}
}
/**
* 求出当前队列有效数据的个数
*/
public int effectiveValue() {
return (rear + maxSize - front) % maxSize;
}
/**
* 显示队列头部数据,不是取数据
*/
public int headQueue() {
//判断为空
if (isEmpty()) {
throw new RuntimeException("队列为空,没有数据~~");
}
return array[front];
}
}
转载自:https://juejin.cn/post/7176892323861626939