3.数组模拟队列
队列特点: 先进先出
3.1 思路分析

- maxSize表示最大容量,front 表示队列头,rear表示队列后端
- 添加元素

- 将尾指针往后移动:real +1, 队列为空的条件: front = = real
- 当real == maxSize-1 表示队列满了,不能添加元素
3.2 代码实现
package com.tomdd;
/**
* 数组模拟队类
*
* @author zx
* @date 2022年12月21日 9:22
*/
public class ArrayQueue {
/**
* real表示数组下标,默认为-1表示没有任何数据
*/
private int real;
/**
* 队列头部
*/
private int front;
/**
* 存放队列元素的数组
*/
private int[] arr;
/**
* 队列最大容量
*/
private int maxSize;
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
//real表示数组下标
real = -1;
//默认指向队列的第一个位置的前面的位置
front = -1;
arr = new int[maxSize];
}
public void isFull() {
if (real == maxSize - 1) {
throw new RuntimeException("队列已满,不能添加元素");
}
}
public void isEmpty() {
if (front == real) {
throw new RuntimeException("队列为空");
}
}
public void push(int num) {
isFull();
arr[++real] = num;
}
public int poll() {
isEmpty();
return arr[++front];
}
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(4);
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
}
}
3.3 问题
- 从队列中取出一个数据,然后再添加一个数据是添加不进去,
- 我们在取数据的时候并没有改变指针。使用环形队列进行改进
4.数组模拟环形队列
通过取模方式修改real/front 指针实现数组环形队列
4.1 思路分析
- front表示队列第一个元素,默认值为0
- real表示队列最后一个元素的后一个位置(约定希望空出一个空间),初始值也是为0
- 队列满的判断条件:
(real+1)%maxSize==front - 队列为空:
real == front - 队列中有效个数:
(real+maxSize-front)%maxSize - 添加元素移动real指针:
real = (real + 1) % maxSize; - 取出元素front指针:
front = (front + 1) % maxSize;
4.2 代码实现
package com.tomdd.arrayqueue;
/**
* 环形数组队列
*
* @author zx
* @date 2022年12月21日 9:45
*/
public class AnnulusArrayQueue {
/**
* real表示队列最后一个元素的后一个位置(约定希望空出一个空间),初始值也是为0
*/
private int real;
/**
* front表示队列第一个元素,默认值为0
*/
private int front;
/**
* 存放队列元素的数组
*/
private int[] arr;
/**
* 队列最大容量
*/
private int maxSize;
public AnnulusArrayQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
}
public void isFull() {
if ((real + 1) % maxSize == front) {
throw new RuntimeException("队列已满");
}
}
public void isEmpty() {
if (real == front) {
throw new RuntimeException("队列为空");
}
}
public void push(int num) {
isFull();
arr[real] = num;
real = (real + 1) % maxSize;
}
public int poll() {
isEmpty();
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
public int size() {
return (real + maxSize - front) % maxSize;
}
public void printQueue() {
isEmpty();
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
public static void main(String[] args) {
AnnulusArrayQueue queue = new AnnulusArrayQueue(4);
queue.push(1);
queue.push(2);
queue.push(3);
queue.printQueue();
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println("-------------------");
queue.push(4);
queue.push(5);
queue.push(6);
queue.printQueue();
}
}
打印结果:
arr[0]=1
arr[1]=2
arr[2]=3
1
23
arr[3]=4
arr[0]=5
arr[1]=6
版权声明:本文为zhongxu_yuan原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。