hello-algo/codes/cpp/chapter_stack_and_queue/array_queue.cpp

129 lines
3 KiB
C++
Raw Normal View History

/**
* File: array_queue.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
2022-11-30 02:27:26 +08:00
/* 基于环形数组实现的队列 */
class ArrayQueue {
private:
2023-02-01 03:23:29 +08:00
int *nums; // 用于存储队列元素的数组
int front; // 队首指针,指向队首元素
int queSize; // 队列长度
int queCapacity; // 队列容量
2022-11-30 02:27:26 +08:00
public:
2022-11-30 02:27:26 +08:00
ArrayQueue(int capacity) {
// 初始化数组
2022-11-30 03:46:53 +08:00
nums = new int[capacity];
2023-02-01 03:23:29 +08:00
queCapacity = capacity;
front = queSize = 0;
2022-11-30 02:27:26 +08:00
}
2023-01-14 19:52:11 +08:00
~ArrayQueue() {
delete[] nums;
}
2022-11-30 02:27:26 +08:00
/* 获取队列的容量 */
int capacity() {
2023-02-01 03:23:29 +08:00
return queCapacity;
2022-11-30 02:27:26 +08:00
}
/* 获取队列的长度 */
int size() {
2023-02-01 03:23:29 +08:00
return queSize;
2022-11-30 02:27:26 +08:00
}
/* 判断队列是否为空 */
2023-09-14 03:36:31 +08:00
bool isEmpty() {
2023-02-01 03:23:29 +08:00
return size() == 0;
2022-11-30 02:27:26 +08:00
}
/* 入队 */
void push(int num) {
2023-02-01 03:23:29 +08:00
if (queSize == queCapacity) {
2022-11-30 02:27:26 +08:00
cout << "队列已满" << endl;
return;
}
2023-02-01 03:23:29 +08:00
// 计算队尾指针,指向队尾索引 + 1
// 通过取余操作,实现 rear 越过数组尾部后回到头部
int rear = (front + queSize) % queCapacity;
// 将 num 添加至队尾
2022-11-30 02:27:26 +08:00
nums[rear] = num;
2023-02-01 03:23:29 +08:00
queSize++;
2022-11-30 02:27:26 +08:00
}
/* 出队 */
void pop() {
int num = peek();
2023-02-01 03:23:29 +08:00
// 队首指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % queCapacity;
queSize--;
2022-11-30 02:27:26 +08:00
}
/* 访问队首元素 */
int peek() {
2023-09-14 03:36:31 +08:00
if (isEmpty())
2022-11-30 02:27:26 +08:00
throw out_of_range("队列为空");
return nums[front];
}
2022-11-30 03:46:53 +08:00
/* 将数组转化为 Vector 并返回 */
2022-11-30 02:27:26 +08:00
vector<int> toVector() {
// 仅转换有效长度范围内的列表元素
2023-02-01 03:23:29 +08:00
vector<int> arr(queSize);
for (int i = 0, j = front; i < queSize; i++, j++) {
arr[i] = nums[j % queCapacity];
2022-11-30 02:27:26 +08:00
}
return arr;
}
};
/* Driver Code */
int main() {
/* 初始化队列 */
int capacity = 10;
ArrayQueue *queue = new ArrayQueue(capacity);
2022-11-30 02:27:26 +08:00
/* 元素入队 */
queue->push(1);
queue->push(3);
queue->push(2);
queue->push(5);
queue->push(4);
2022-11-30 02:27:26 +08:00
cout << "队列 queue = ";
printVector(queue->toVector());
2022-11-30 02:27:26 +08:00
/* 访问队首元素 */
int peek = queue->peek();
cout << "队首元素 peek = " << peek << endl;
2022-11-30 02:27:26 +08:00
/* 元素出队 */
queue->pop();
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
printVector(queue->toVector());
2022-11-30 02:27:26 +08:00
/* 获取队列的长度 */
int size = queue->size();
cout << "队列长度 size = " << size << endl;
/* 判断队列是否为空 */
2023-09-14 03:36:31 +08:00
bool empty = queue->isEmpty();
2022-11-30 02:27:26 +08:00
cout << "队列是否为空 = " << empty << endl;
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
queue->push(i);
queue->pop();
2022-11-30 02:27:26 +08:00
cout << "" << i << " 轮入队 + 出队后 queue = ";
printVector(queue->toVector());
2022-11-30 02:27:26 +08:00
}
2023-01-14 19:52:11 +08:00
// 释放内存
delete queue;
2022-11-30 02:27:26 +08:00
return 0;
}