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

138 lines
3.4 KiB
C++
Raw Normal View History

/*
* File: array_queue.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
*/
#include "../include/include.hpp"
2022-11-29 18:27:26 +00:00
/* 基于环形数组实现的队列 */
class ArrayQueue {
private:
2022-11-29 19:46:53 +00:00
int *nums; // 用于存储队列元素的数组
int cap; // 队列容量
int front = 0; // 头指针,指向队首
int rear = 0; // 尾指针,指向队尾 + 1
2022-11-29 18:27:26 +00:00
public:
ArrayQueue(int capacity) {
// 初始化数组
2022-11-29 19:46:53 +00:00
cap = capacity;
nums = new int[capacity];
2022-11-29 18:27:26 +00:00
}
/* 获取队列的容量 */
int capacity() {
2022-11-29 19:46:53 +00:00
return cap;
2022-11-29 18:27:26 +00:00
}
/* 获取队列的长度 */
int size() {
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
2022-11-29 19:46:53 +00:00
return (capacity() + rear - front) % capacity();
2022-11-29 18:27:26 +00:00
}
/* 判断队列是否为空 */
bool empty() {
return rear - front == 0;
}
/* 入队 */
void offer(int num) {
if (size() == capacity()) {
cout << "队列已满" << endl;
return;
}
// 尾结点后添加 num
nums[rear] = num;
// 尾指针向后移动一位,越过尾部后返回到数组头部
rear = (rear + 1) % capacity();
}
/* 出队 */
int poll() {
// 删除头结点
if (empty())
throw out_of_range("队列为空");
int num = nums[front];
// 队头指针向后移动,越过尾部后返回到数组头部
front = (front + 1) % capacity();
return num;
}
/* 访问队首元素 */
int peek() {
// 删除头结点
if (empty())
throw out_of_range("队列为空");
return nums[front];
}
2022-11-29 19:46:53 +00:00
/* 访问指定索引元素 */
int get(int index) {
if (index >= size())
throw out_of_range("索引越界");
return nums[(front + index) % capacity()];
}
/* 将数组转化为 Vector 并返回 */
2022-11-29 18:27:26 +00:00
vector<int> toVector() {
int siz = size();
int cap = capacity();
// 仅转换有效长度范围内的列表元素
vector<int> arr(siz);
for (int i = 0, j = front; i < siz; i++, j++) {
arr[i] = nums[j % cap];
}
return arr;
}
};
/* Driver Code */
int main() {
/* 初始化队列 */
int capacity = 10;
ArrayQueue* queue = new ArrayQueue(capacity);
/* 元素入队 */
queue->offer(1);
queue->offer(3);
queue->offer(2);
queue->offer(5);
queue->offer(4);
cout << "队列 queue = ";
PrintUtil::printVector(queue->toVector());
/* 访问队首元素 */
int peek = queue->peek();
cout << "队首元素 peek = " << peek << endl;
2022-11-29 19:46:53 +00:00
/* 访问指定索引元素 */
int num = queue->get(2);
cout << "队列第 3 个元素为 num = " << num << endl;
2022-11-29 18:27:26 +00:00
/* 元素出队 */
int poll = queue->poll();
cout << "出队元素 poll = " << poll << ",出队后 queue = ";
PrintUtil::printVector(queue->toVector());
/* 获取队列的长度 */
int size = queue->size();
cout << "队列长度 size = " << size << endl;
/* 判断队列是否为空 */
bool empty = queue->empty();
cout << "队列是否为空 = " << empty << endl;
/* 测试环形数组 */
for (int i = 0; i < 10; i++) {
queue->offer(i);
queue->poll();
cout << "" << i << " 轮入队 + 出队后 queue = ";
PrintUtil::printVector(queue->toVector());
}
return 0;
}