队列的模拟及环形队列思路
定义
- 队列是一个有序列表,可以用数组或是链表来实现。
- 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
模拟思路
-
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量
-
因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front及 rear分别记录队列前后端的下标,front 会随着数据输出而改变,而 rear则是随着数据输入而改变,如图所示
入队出队操作模拟
当我们将数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:
-
将尾指针往后移:rear+1 , 当 front == rear 时,队列为空
-
若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear所指的数组元素中,否则无法存入数据。rear == maxSize - 1时,队列满
注意:front指向的是队列首元素的前一个位置
实现代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import java.util.Scanner; public class ArrayQueueDemo { public static void main(String[] args) { ArrayQueue queue = new ArrayQueue( 3 ); Scanner scanner = new Scanner(System.in); boolean flag = true ; while (flag){ System.out.println( "输入a(add)添加数据" ); System.out.println( "输入g(get)取出数据" ); System.out.println( "输入s(show)显示所有数据" ); System.out.println( "输入h(head)显示头部数据" ); System.out.println( "输入e(exit)退出程序" ); char c = scanner.next().charAt( 0 ); switch (c){ case 'a' : System.out.println( "请输入数据" ); int num = scanner.nextInt(); queue.addNum(num); break ; case 'g' : try { queue.getQueue(); System.out.println( "取出成功" ); } catch (Exception e){ System.out.println(e.getMessage()); } break ; case 's' : queue.showQueue(); break ; case 'h' : try { queue.headQueue(); } catch (Exception e){ System.out.println(e.getMessage()); } break ; case 'e' : flag = false ; System.out.println( "程序已退出" ); break ; default : break ; } } scanner.close(); } } class ArrayQueue{ private int maxSize; //队列的大小 private int front; //指向队列首元素的前一个位置 private int rear; //指向队列的尾元素 private int [] arr; //用数组来实现队列 //构造方法初始化 public ArrayQueue( int maxSize) { this .maxSize = maxSize; front = - 1 ; //指向的是队列第一个元素的前一个位置 rear = - 1 ; arr = new int [maxSize]; } /** * 判断队列是否装满 * @return */ public boolean isFull(){ return rear == maxSize- 1 ; } /** * 判断队列是否为空 * @return */ public boolean isEmpty(){ return front == rear; } /** * 为队列添加数据 * @param num */ public void addNum( int num){ if (isFull()){ System.out.println( "队列已经满了,无法再加入数据" ); return ; } rear++; arr[rear] = num; } /** * 取出队列中的数据 * @return */ public int getQueue(){ if (isEmpty()){ throw new RuntimeException( "队列为空,不能取出数据" ); } front++; return arr[front]= 0 ; } /** * 显示队列的所有数据 */ public void showQueue(){ if (isEmpty()) { System.out.println( "队列为空,没有数据" ); return ; } for ( int i = 0 ; i<arr.length; i++){ System.out.printf( "arr[%d]=%d\n" ,i,arr[i]); } } /** * 显示队列的头数据,注意不是取出数据 * @return */ public int headQueue(){ if (isEmpty()){ throw new RuntimeException( "队列为空,没有数据" ); } return arr[front+ 1 ]; } } |
运行结果
注意:因先入先出的原则,front和rear最终都会在队列顶部,所以上述队列只能一次性使用,没有达到复用的效果,因此我们要用到环形队列
环形队列
思路:
- front变量含义调整:front变量指向队首元素,初值为0
- rear变量含义调整:rear变量指向队尾元素的下一个元素,初值为0。规定空出一个位置
- 队列为空的判定条件:front == rear
- 队列为满的判定条件:(rear + 1) % maxSize == front
- 队列中有效元素的个数:(rear - front + maxSize) % maxSize
- 入队和出队时,都需要让标记对maxSize取模
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
import
java.util.Scanner;
public
class
CyclicArrayQueueDemo {
public
static
void
main(String[] args) {
CyclicArrayQueue queue =
new
CyclicArrayQueue(
3
);
Scanner scanner =
new
Scanner(System.in);
boolean
flag =
true
;
while
(flag){
System.out.println(
"输入a(add)添加数据"
);
System.out.println(
"输入g(get)取出数据"
);
System.out.println(
"输入s(show)显示所有数据"
);
System.out.println(
"输入h(head)显示头部数据"
);
System.out.println(
"输入e(exit)退出程序"
);
char
c = scanner.next().charAt(
0
);
switch
(c){
case
'a'
:
System.out.println(
"请输入数据"
);
int
num = scanner.nextInt();
queue.addNum(num);
break
;
case
'g'
:
try
{
int
n = queue.getQueue();
System.out.println(
"取出的数是:"
+n);
}
catch
(Exception e){
System.out.println(e.getMessage());
}
break
;
case
's'
:
queue.showQueue();
break
;
case
'h'
:
try
{
queue.headQueue();
}
catch
(Exception e){
System.out.println(e.getMessage());
}
break
;
case
'e'
:
flag =
false
;
System.out.println(
"程序已退出"
);
break
;
default
:
break
;
}
}
scanner.close();
}
}
class
CyclicArrayQueue {
private
int
maxSize;
//队列的大小
private
int
front;
//front变量指向队首元素,初值为0
private
int
rear;
//rear变量指向队尾元素的下一个元素,初值为0。规定空出一个位置
private
int
[] arr;
//用数组来实现队列
public
CyclicArrayQueue(
int
maxSize) {
this
.maxSize = maxSize;
arr =
new
int
[maxSize];
}
/**
* 判断队列是否装满
*
* @return
*/
public
boolean
isFull() {
return
(rear +
1
) % maxSize == front;
}
/**
* 判断队列是否为空
*
* @return
*/
public
boolean
isEmpty() {
return
front == rear;
}
/**
* 为队列添加数据
*
* @param num
*/
public
void
addNum(
int
num) {
if
(isFull()) {
System.out.println(
"队列已经满了,无法再加入数据"
);
return
;
}
arr[rear] = num;
rear = (rear +
1
) % maxSize;
}
/**
* 取出队列中的数据
*
* @return
*/
public
int
getQueue() {
if
(isEmpty()) {
throw
new
RuntimeException(
"队列为空,不能取出数据"
);
}
int
value = arr[front];
front = (front +
1
) % maxSize;
return
value;
}
/**
* 显示队列的所有数据
*/
public
void
showQueue() {
if
(isEmpty()) {
System.out.println(
"队列为空,没有数据"
);
return
;
}
for
(
int
i = front; i < front + size(); i++) {
System.out.printf(
"arr[%d]=%d\n"
, i % maxSize, arr[i % maxSize]);
}
}
public
int
size() {
return
(rear + maxSize - front) % maxSize;
}
/**
* 显示队列的头数据,注意不是取出数据
* @return
*/
public
int
headQueue(){
if
(isEmpty()){
throw
new
RuntimeException(
"队列为空,没有数据"
);
}
return
arr[front];
}
}
本文来自博客园,作者:腹白,转载请注明原文链接:https://www.cnblogs.com/wyh518/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?