cpp-algo-cases/chapter_stack_and_queue/array_queue.cpp

130 lines
2.8 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: array_queue.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵĶ<D6B5><C4B6><EFBFBD> */
class ArrayQueue {
private:
int *nums; // <20><><EFBFBD>ڴ洢<DAB4><E6B4A2><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
int front; // <20><><EFBFBD><EFBFBD>ָ<EFBFBD>룬ָ<EBA3AC><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
int queSize; // <20><><EFBFBD>г<EFBFBD><D0B3><EFBFBD>
int queCapacity; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public:
ArrayQueue(int capacity) {
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
nums = new int[capacity];
queCapacity = capacity;
front = queSize = 0;
}
~ArrayQueue() {
delete[] nums;
}
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> */
int capacity() {
return queCapacity;
}
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
int size() {
return queSize;
}
/* <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
bool isEmpty() {
return size() == 0;
}
/* <20><><EFBFBD><EFBFBD> */
void push(int num) {
if (queSize == queCapacity) {
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
return;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>βָ<CEB2>룬ָ<EBA3AC><D6B8><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD> + 1
// ͨ<><CDA8>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5> rear Խ<><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ͷ<EFBFBD><CDB7>
int rear = (front + queSize) % queCapacity;
// <20><> num <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β
nums[rear] = num;
queSize++;
}
/* <20><><EFBFBD><EFBFBD> */
int pop() {
int num = peek();
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD>򷵻ص<F2B7B5BB><D8B5><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
front = (front + 1) % queCapacity;
queSize--;
return num;
}
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
int peek() {
if (isEmpty())
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
return nums[front];
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ Vector <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
vector<int> toVector() {
// <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD>ȷ<EFBFBD>Χ<EFBFBD>ڵ<EFBFBD><DAB5>б<EFBFBD>Ԫ<EFBFBD><D4AA>
vector<int> arr(queSize);
for (int i = 0, j = front; i < queSize; i++, j++) {
arr[i] = nums[j % queCapacity];
}
return arr;
}
};
/* Driver Code */
int main() {
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int capacity = 10;
ArrayQueue *queue = new ArrayQueue(capacity);
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
queue->push(1);
queue->push(3);
queue->push(2);
queue->push(5);
queue->push(4);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> queue = ";
printVector(queue->toVector());
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
int peek = queue->peek();
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> peek = " << peek << endl;
/* Ԫ<>س<EFBFBD><D8B3><EFBFBD> */
peek = queue->pop();
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> pop = " << peek << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> queue = ";
printVector(queue->toVector());
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
int size = queue->size();
cout << "<EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
/* <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
bool empty = queue->isEmpty();
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << empty << endl;
/* <20><><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
for (int i = 0; i < 10; i++) {
queue->push(i);
queue->pop();
cout << "<EFBFBD><EFBFBD> " << i << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> + <20><><EFBFBD>Ӻ<EFBFBD> queue = ";
printVector(queue->toVector());
}
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
delete queue;
return 0;
}