队列
队列的介绍
1.队列是一个有序的列表,可以去用数组或者是链表来实现的
2.遵循先入先出的原则,即:先存入的队列的数据要先去取出。后存入的数据要在后面取出
示意图
![alt text](image.png)
队列可以去用数组和链表去实现
数组就是顺序存储结构
链式就是链式存储结构
加数据实在队列的尾部加
去数据是在队列的队首去
队列本身就是一个有序表
maxSize就是队列的最大的容量
因为队列的输入所以分别是在前后两个位置对队列进行相应的处理
front和rear分别记录这个队列的前后端下标
package Java.DataStructuresAndAlgorithms.DataStructures.Queue;
import java.util.Scanner;
public class ArrayQueueDemo {
//使用数组模拟队列
public static void main(String[] args) {
//test测试一下
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.printf("取出一个数据: %d", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.headQueue();
System.out.printf("取出一个数据: %d", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
System.out.println("输入的字母存在问题");
break;
}
}
System.out.println("程序退出啦~~~");
}
}
//编写一个ArrayQueue
class ArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1; //指向队列的头部的前一个位置
rear = -1; //指向队列的尾部的具体的那个位置,是指向队列尾部的数据,包含队列尾部的最后一个数据
}
public boolean isFull() {
return rear == maxSize - 1;//后面的那个指针指向了这个maxSize的时候,这时候就意味着这个添加到了最后一个了
}
public boolean isEmpty() {
return rear == front;//前后都没动啊
}
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列已经满了不可以再次添加数据啦~~~");
return;
}
rear++;
arr[rear] = n;
//arr[rear++]=n
}
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列是空的啦不可以再取了");
//System.out.println("队列是空的啦不可以再取了");
}
front++;//move
return arr[front];
}
public void showQueue() {
if (isEmpty()) {
System.out.println("队列是空的,没有数据啊~~~");
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列是空的,没有数据啊~~~");
}
return arr[front + 1]; //加1的原因就是这个front原本就是本来指向的队列的前一个
}
}
➜ code git:(main) ✗ /usr/bin/env /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -cp /Users/spring/Library/Application\ Support/C
ode/User/workspaceStorage/886eeaf04c801c2f2580b125c2e39834/redhat.java/jdt_ws/code_ecfd4cd0/bin Java.DataStructuresAndAlgorithms.DataStructures.Queue.ArrayQue
ueDemo
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
s
队列是空的,没有数据啊~~~
arr[0]=0
arr[1]=0
arr[2]=0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
a
输出一个数
10
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
s
arr[0]=10
arr[1]=0
arr[2]=0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
a
输出一个数
20
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
s
arr[0]=10
arr[1]=20
arr[2]=0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
a
输出一个数
30
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
s
arr[0]=10
arr[1]=20
arr[2]=30
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
a
输出一个数
10
队列已经满了不可以再次添加数据啦~~~
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列获取数据
h(head):查看队列头的数据
e
程序退出啦~~~
➜ code git:(main) ✗
之后还会对这个队列进行相关的优化