队列
- 队列(queue)
- 1.队列的特点
- 2.数组模拟队列JAVA代码
- 3.上述过程优化
博文主要是自己学习的笔记,供自己以后复习使用,
参考的主要教程是B站的
尚硅谷数据结构和算法
队列(queue)
1.队列的特点
1)队列是一个有序列表,可以用数组或者链表来实现
2)遵循先进先出的原则:先存入队列的数据,要先取出。
用数组模拟队列的示意图:
初始化:rear=front=-1,都指向队列的前一个元素
入队:rear++
出队:front++
判空:rear==front
判满:rear = maxSize - 1
2.数组模拟队列JAVA代码
class ArrayQueue {private int maxSize;//最大容量private int front;//队列头private int rear;//队列尾private int[] arr;//存放数组//创建队列的构造器public ArrayQueue(int maxSize) {this.maxSize = maxSize;arr = new int[maxSize];front = -1;rear = -1;}//判断队列是否已满public boolean isFull() {return rear == maxSize - 1;}//判断队列是否为空public boolean isEmpty() {return rear == front;}//添加数据到队列public void addQueue(int data) {if (isFull()) {System.out.println("队列已满");} else {rear++;arr[rear] = data;}}//获取队列的数据,出队列public int getQueue() {if (isEmpty()) {//通过抛出异常throw new RuntimeException("队列为空");} else {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];}
}
测试代码
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 'a':System.out.println("请输入数据");int data = scanner.nextInt();arrayQueue.addQueue(data);break;case 'g':try {int res = arrayQueue.getQueue();System.out.printf("取出的数据是%d\n", res);} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {int res = arrayQueue.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;}}}
}
3.上述过程优化
上述代码存在问题:
队列不管是存还是取都是++操作,因此上述队列只能用一次
**优化手段:**改为环形的队列,利用取模的操作实现。
初始化:rear=front=0,front指向当前元素,rear指向当前元素的后一个位置,空出一个空间作为约定
入队:rear = (rear+1)%maxSize
出队:front= (front+1)%maxSize
判空:rear==front
判满:(rear+1)%maxSize = front
有效数据个数(rear - front + maxSize) % maxSize
JAVA代码实现
class CircleArray {private int maxSize;private int front;private int rear;private int[] arr;public CircleArray(int maxSize) {this.maxSize = maxSize;rear = 0;front = 0;arr = new int[maxSize];}//判断队列是否已满public boolean isFull() {return (rear + 1) % maxSize == front;}//判断队列是否为空public boolean isEmpty() {return rear == front;}//添加数据到队列public void addQueue(int data) {if (isFull()) {System.out.println("队列已满");} else {arr[rear] = data;rear = (rear + 1) % maxSize;}}//获取队列的数据,出队列public int getQueue() {if (isEmpty()) {//通过抛出异常throw new RuntimeException("队列为空");} else {int temp = arr[front];front = (front + 1) % maxSize;return temp;}}//显示队列的所有数据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 headQueue() {if (isEmpty()) {throw new RuntimeException("队列为空~~~");}return arr[front];}//显示队列的头数据,注意不是取数据public int size() {return (rear - front + maxSize) % maxSize;}
}
测试代码
public class CircleArrayQueueDemo {public static void main(String[] args) {//测试CircleArray arrayQueue = new CircleArray(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 'a':System.out.println("请输入数据");int data = scanner.nextInt();arrayQueue.addQueue(data);break;case 'g':try {int res = arrayQueue.getQueue();System.out.printf("取出的数据是%d\n", res);} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {int res = arrayQueue.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;}}}
}