天津网站建设制作价格/网络销售
介绍
- 队列是一个有序列表,可以用数组或是链表来实现。
- 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
- 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队 列的最大容量。
- 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front 及 rear 分别记录队列前后端的下标,
front 会随着数据输出而改变,而 rear 则是随着数据输入而改变
判断队列是否为空: rear = front ;
判断队列是否为满 rear = MaxSize - 1;
package Com.JW.Demo;import java.util.Scanner;import javax.management.RuntimeErrorException;public class ArrayQueueDemo {@SuppressWarnings("resource")public static void main(String[] args) {boolean flag = true;Scanner scanner = new Scanner(System.in);//初始化ArrayQueue arrayQueue = new ArrayQueue(5);while(flag) {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): 查看队列头的数据");switch (scanner.next()) {case "s":arrayQueue.showQueue();break;case "a":System.out.println("请输入");int val = scanner.nextInt();arrayQueue.addQueue(val);break;case "g":int queue = arrayQueue.getQueue();System.out.println("数据"+queue);break;case "h":arrayQueue.getHead();break;case "e":flag = false;break;default:break;}}}}class ArrayQueue {private int MaxSize;private int font;private int rear;private int[] arr;public ArrayQueue(int size) {MaxSize = size;arr = new int[size];font = -1;rear = -1;}public boolean isEmpty() {return rear == font;}public boolean isFull() {return rear == MaxSize - 1;}public void addQueue(int val) {if (isFull()) {System.out.println("队列已经满了");return;}rear++;arr[rear] = val;}public int getQueue() {if (isEmpty()) {System.out.println("队列为空");}font++;int count = arr[font] ;arr[font] = 0;return count;}public void showQueue() {if (isEmpty()) {System.out.println("队列为空");return ;}for (int i = 0; i < arr.length; i++) {if(arr[i]!=0) {System.out.printf("arr[%d]=%d\n", i, arr[i]);}}}public void getHead() {if(isEmpty()) {throw new RuntimeException("队列空");}System.out.println(arr[font+1]);}}
结果
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
队列为空
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入
12
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入
15
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入
16
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入
17
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入
18
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
请输入
19
队列已经满了
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据s
arr[0]=12
arr[1]=15
arr[2]=16
arr[3]=17
arr[4]=18
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
数据12
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[1]=15
arr[2]=16
arr[3]=17
arr[4]=18
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
数据15
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[2]=16
arr[3]=17
arr[4]=18
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
数据16
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[3]=17
arr[4]=18
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
数据17
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[4]=18
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
数据18
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
h
头部数据:16
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据s
队列为空
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据