普通队列
- 使用场景: 银行排队的案列
- 队列介绍
- 队列是一个有序列表,可以使用数组或是链表来实现
- 先入后出
- 示意图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fc11bc0442343986107d921b0b5ce589.png)
- 现在的队列数组使用一次就不能用,没有达到复用,后面将使用环形数组
- 实现代码
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = 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':
arrayQueue.showQueue();
break;
case 'e':
scanner.close();
loop = false;
break;
case 'a':
System.out.println("输入一个数据");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res = arrayQueue.getQueue();
System.out.println("取出的数据是:" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = arrayQueue.headQueue();
System.out.println("队列头的数据是:" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
}
}
System.out.println("程序退出");
}
}
class ArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
front = -1;
rear = -1;
arr = new int[maxSize];
}
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++;
arr[rear] = n;
}
public int getQueue() {
if(isEmpty()) {
throw new RuntimeException("队列空,不能取数据");
}
front++;
return arr[front];
}
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]);
}
}
public int headQueue() {
if(isEmpty()) {
throw new RuntimeException("队列空,不能取数据");
}
return arr[front + 1];
}
}
环形队列
- 思路
- front变量指向队列的第一个元素,也就是说,arr[front]就是队列的第一个元素,front初始值为0
- rear指向队列的最后一个元素,rear初始值为0
- 当队列满时,条件是: (rear + 1) % maxSize = front
- 当队列为空时: rear == front
- 队列的有效数据的个数: (rear + maxSize - front) % maxSize
- 环形队列生成了
- 代码
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
CircleArrayQueue arrayQueue = new CircleArrayQueue(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':
arrayQueue.showQueue();
break;
case 'e':
scanner.close();
loop = false;
break;
case 'a':
System.out.println("输入一个数据");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res = arrayQueue.getQueue();
System.out.println("取出的数据是:" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = arrayQueue.headQueue();
System.out.println("队列头的数据是:" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
}
}
System.out.println("程序退出");
}
}
class CircleArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[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++;
}
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,arr[i]);
}
}
public int size() {
return (rear + maxSize - front) % maxSize;
}
public int headQueue() {
if(isEmpty()) {
throw new RuntimeException("队列空的,没有数据");
}
return arr[front];
}
}