2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-11-25 02:04:38 +08:00
|
|
|
* File: array_queue.java
|
|
|
|
* Created Time: 2022-11-25
|
|
|
|
* Author: Krahets (krahets@163.com)
|
|
|
|
*/
|
|
|
|
|
2022-11-10 03:40:57 +08:00
|
|
|
package chapter_stack_and_queue;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
2023-04-14 00:12:10 +08:00
|
|
|
private int[] nums; // 用于存储队列元素的数组
|
|
|
|
private int front; // 队首指针,指向队首元素
|
2023-02-01 03:23:29 +08:00
|
|
|
private int queSize; // 队列长度
|
2022-11-10 03:40:57 +08:00
|
|
|
|
|
|
|
public ArrayQueue(int capacity) {
|
|
|
|
nums = new int[capacity];
|
2023-02-01 03:23:29 +08:00
|
|
|
front = queSize = 0;
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
public int capacity() {
|
|
|
|
return nums.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
public int size() {
|
2023-02-01 03:23:29 +08:00
|
|
|
return queSize;
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
public boolean isEmpty() {
|
2023-02-01 03:23:29 +08:00
|
|
|
return queSize == 0;
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 入队 */
|
2023-02-02 01:43:01 +08:00
|
|
|
public void push(int num) {
|
2023-02-01 03:23:29 +08:00
|
|
|
if (queSize == capacity()) {
|
2022-11-10 03:40:57 +08:00
|
|
|
System.out.println("队列已满");
|
|
|
|
return;
|
|
|
|
}
|
2023-02-01 03:23:29 +08:00
|
|
|
// 计算尾指针,指向队尾索引 + 1
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
int rear = (front + queSize) % capacity();
|
2023-02-16 02:17:15 +08:00
|
|
|
// 将 num 添加至队尾
|
2022-11-10 03:40:57 +08:00
|
|
|
nums[rear] = num;
|
2023-02-01 03:23:29 +08:00
|
|
|
queSize++;
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
public int pop() {
|
2022-12-02 00:09:34 +08:00
|
|
|
int num = peek();
|
2023-02-01 03:23:29 +08:00
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
2022-11-10 03:40:57 +08:00
|
|
|
front = (front + 1) % capacity();
|
2023-02-01 03:23:29 +08:00
|
|
|
queSize--;
|
2022-11-10 03:40:57 +08:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
public int peek() {
|
|
|
|
if (isEmpty())
|
2023-04-23 03:41:39 +08:00
|
|
|
throw new IndexOutOfBoundsException();
|
2022-11-10 03:40:57 +08:00
|
|
|
return nums[front];
|
|
|
|
}
|
|
|
|
|
2022-11-30 03:46:53 +08:00
|
|
|
/* 返回数组 */
|
2022-11-10 03:40:57 +08:00
|
|
|
public int[] toArray() {
|
|
|
|
// 仅转换有效长度范围内的列表元素
|
2023-02-01 03:23:29 +08:00
|
|
|
int[] res = new int[queSize];
|
|
|
|
for (int i = 0, j = front; i < queSize; i++, j++) {
|
|
|
|
res[i] = nums[j % capacity()];
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
2022-11-30 03:46:53 +08:00
|
|
|
return res;
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class array_queue {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
/* 初始化队列 */
|
|
|
|
int capacity = 10;
|
|
|
|
ArrayQueue queue = new ArrayQueue(capacity);
|
|
|
|
|
|
|
|
/* 元素入队 */
|
2023-02-02 01:43:01 +08:00
|
|
|
queue.push(1);
|
|
|
|
queue.push(3);
|
|
|
|
queue.push(2);
|
|
|
|
queue.push(5);
|
|
|
|
queue.push(4);
|
2022-11-10 03:40:57 +08:00
|
|
|
System.out.println("队列 queue = " + Arrays.toString(queue.toArray()));
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
int peek = queue.peek();
|
|
|
|
System.out.println("队首元素 peek = " + peek);
|
|
|
|
|
|
|
|
/* 元素出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
int pop = queue.pop();
|
|
|
|
System.out.println("出队元素 pop = " + pop + ",出队后 queue = " + Arrays.toString(queue.toArray()));
|
2022-11-10 03:40:57 +08:00
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
int size = queue.size();
|
|
|
|
System.out.println("队列长度 size = " + size);
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
boolean isEmpty = queue.isEmpty();
|
2022-11-30 02:27:26 +08:00
|
|
|
System.out.println("队列是否为空 = " + isEmpty);
|
2022-11-10 03:40:57 +08:00
|
|
|
|
|
|
|
/* 测试环形数组 */
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2023-02-02 01:43:01 +08:00
|
|
|
queue.push(i);
|
2023-03-13 21:58:21 +08:00
|
|
|
queue.pop();
|
2022-11-30 02:27:26 +08:00
|
|
|
System.out.println("第 " + i + " 轮入队 + 出队后 queue = " + Arrays.toString(queue.toArray()));
|
2022-11-10 03:40:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|