欢迎光临散文网 会员登陆 & 注册

Java三十篇:初识队列

2023-03-07 00:10 作者:小刘Java之路  | 我要投稿

以下的都是我自己理解,不对的观点请及时联系我

1.什么是队列?

队列是数据结构中比较重要的一种类型(是一种数据结构),它支持 FIFO,尾部添加、头部删除(先进队列的元素先出队列),跟我们生活中的排队类似。

2.什么情况下使用队列?

一般情况下,如果是对一些及时消息的处理,并且处理时间很短的情况下是不需要队列的,直接阻塞式的方法调用就可以了。但是如果在消息处理的时候特别费时间,这个时候如果有新消息来了,就只能处于阻塞状态,造成用户等待。这个时候便需要引入队列了。当接收到消息后,先把消息房贷队列中,然后再用行的线程进行处理,这个时候就不会有消息阻塞了。

3.队列介绍:

队列有两种:① 单队列 :就是常见的队列,每次添加元素时,都是添加对队尾。② 循环队列

4.队列Queue

add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常 remove 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常 element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常 offer 添加一个元素并返回true 如果队列已满,则返回false poll 移除并返问队列头部的元素 如果队列为空,则返回null peek 返回队列头部的元素 如果队列为空,则返回null put 添加一个元素 如果队列满,则阻塞 take 移除并返回队列头部的元素 如果队列为空,则阻塞

java集合中Queue继承自Collection接口,Deque,Linkedlist,PriorityQueue,BlockingQueue等类都实现了它

Queue 用来存放 等待处理元素 的集合,这种场景一般用于缓冲、并发访问。

除了继承 Collection 接口的一些方法,Queue 还添加了额外的 添加、删除、查询操作。

img

队列的使用场景

银行排队的案例:

img

队列介绍

  • 队列是一个有序列表, 可以用数组或是链表来实现。

  • 遵循先入先出的原则, 即:先存入队列的数据, 要先取出,后存入的要后取出

  • 示意图:(使用数组模拟队列示意图)

img

数组模拟队列

思路分析

  • maxSize :队列容量(数组的长度)

  • arr :模拟队列的数组

  • front :指向队列头部元素的前一个元素,初始值为 -1

  • rear :指向队列尾部元素,初始值为 -1

img
  • 基本操作

队列判空:front == rear

队列判满:rear == (maxSize - 1) ,即 rear 是否已经指向了数组的最后一个位置

队列元素个数:rear - front

队列入队:队列不满才能入队,arr[++rear] = value

队列出队:队列不空才能出队,return arr[front++]


代码实现

//队列的定义
// 使用数组模拟队列-编写一个ArrayQueue类
class ArrayQueue {
privateint maxSize; // 表示数组的最大容量
privateint front; // 队列头
privateint rear; // 队列尾
privateint[] arr; // 该数据用于存放数据, 模拟队列

// 创建队列的构造器
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = newint[maxSize];
front = -1; // 指向队列头部,分析出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++; // 让 rear 后移
arr[rear] = n;
}

// 获取队列的数据, 出队列
public int getQueue() {
// 判断队列是否空
if (isEmpty()) {
// 通过抛出异常
thrownew RuntimeException("队列空,不能取数据");
}
front++; // front后移
return arr[front];

}

// 显示队列的所有数据
public void showQueue() {
// 遍历
if (isEmpty()) {
System.out.println("队列空的,没有数据~~");
return;
}
for (int i = front + 1; i <= rear; i++) {
// Java 中也能用占位符诶
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}

// 显示队列的头数据, 注意不是取出数据
public int headQueue() {
// 判断
if (isEmpty()) {
thrownew RuntimeException("队列空的,没有数据~~");
}
return arr[front + 1];
}
}

  • 测试代码

publicclass 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): 查看队列头的数据");
System.out.println();
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) {
System.out.println(e.getMessage());
}
break;
case'h': // 查看队列头的数据
try {
int res = queue.headQueue();
System.out.printf("队列头的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case'e': // 退出
scanner.close();
loop = false;
break;
default:
break;
}
}

System.out.println("程序退出~~");
}

}

  • 程序运行结果

s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

s
队列空的,没有数据~~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

s
arr[0]=1
arr[1]=2
arr[2]=3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
4
队列满,不能加入数据~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
队列空,不能取数据
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

  • 目前数组使用一次就不能用, 没有达到复用的效果,造成内存空间的浪费

  • 将这个数组使用算法, 改进成一个环形的队列(取模:%

解决问题如下:

数组模拟环形队列

思路分析

  • 对前面的队列进行优化,改造为环形队列(通过取模实现)

  • maxSize :队列容量(数组的长度)

  • arr :模拟队列的数组

  • front :指向队列头部元素,初始值为 0

  • rear :指向队列尾部元素的后一个元素,初始值为 0

img

代码实现

class CircleArray {
privateint maxSize; // 表示数组的最大容量
// front 变量的含义做一个调整:front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
// front 的初始值 = 0
privateint front;
// rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
// rear 的初始值 = 0
privateint rear; // 队列尾
privateint[] arr; // 该数据用于存放数据, 模拟队列

public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = newint[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 = (rear + 1) % maxSize;
}

// 获取队列的数据, 出队列
public int getQueue() {
// 判断队列是否空
if (isEmpty()) {
// 通过抛出异常
thrownew RuntimeException("队列空,不能取数据");
}
// 这里需要分析出 front是指向队列的第一个元素
// 1. 先把 front 对应的值保留到一个临时变量
// 2. 将 front 后移, 考虑取模
// 3. 将临时保存的变量返回
int value = arr[front];
front = (front + 1) % maxSize;
return value;

}

// 显示队列的所有数据
public void showQueue() {
// 遍历
if (isEmpty()) {
System.out.println("队列空的,没有数据~~");
return;
}
// 思路:从front开始遍历,遍历多少个元素
// 动脑筋
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}

// 求出当前队列有效数据的个数
public int size() {
// rear = 2
// front = 1
// maxSize = 3
return (rear + maxSize - front) % maxSize;
}

// 显示队列的头数据, 注意不是取出数据
public int headQueue() {
// 判断
if (isEmpty()) {
thrownew RuntimeException("队列空的,没有数据~~");
}
return arr[front];
}
}

测试代码

publicclass CircleArrayQueueDemo {

public static void main(String[] args) {

// 测试一把
System.out.println("测试数组模拟环形队列的案例~~~");

// 创建一个环形队列
CircleArray queue = new CircleArray(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): 查看队列头的数据");
System.out.println();
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("程序退出~~");
}

}

程序运行结果

测试数组模拟环形队列的案例~~~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

s
arr[0]=1
arr[1]=2
arr[2]=3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
4
队列满,不能加入数据~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

s
arr[2]=3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
队列空,不能取数据
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

总结:

1、队列有两种:① 单队列 :就是常见的队列,每次添加元素时,都是添加对队尾。② 循环队列(环形队列)

2、遵循先入先出的原则, 即:先存入队列的数据, 要先取出,后存入的要后取出


加强自身学习,提高自身素质。 积累工作经验,改进工作方法,向周围同志学习,注重别人优点,学习他们处理问题的方法,查找不足,提高自己。



Java三十篇:初识队列的评论 (共 条)

分享到微博请遵守国家法律