cpp-algo-cases/chapter_stack_and_queue/array_queue.cpp

144 lines
2.9 KiB
C++
Raw Permalink Normal View History

2024-02-21 09:31:34 +08:00
/**
* File: array_queue.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
2024-02-23 14:47:33 +08:00
/* <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>
2024-02-21 09:31:34 +08:00
nums = new int[capacity];
queCapacity = capacity;
2024-02-23 14:47:33 +08:00
front = queSize = 0; // <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>Ͷ<EFBFBD><CDB6>г<EFBFBD><D0B3><EFBFBD>
2024-02-21 09:31:34 +08:00
}
2024-02-23 14:47:33 +08:00
~ArrayQueue()
{
delete[] nums; // <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
2024-02-21 09:31:34 +08:00
}
2024-02-23 14:47:33 +08:00
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> */
int capacity()
{
2024-02-21 09:31:34 +08:00
return queCapacity;
}
2024-02-23 14:47:33 +08:00
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
int size()
{
2024-02-21 09:31:34 +08:00
return queSize;
}
2024-02-23 14:47:33 +08:00
/* <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
bool isEmpty()
{
2024-02-21 09:31:34 +08:00
return size() == 0;
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD><EFBFBD> */
void push(int num)
{
if (queSize == queCapacity)
{
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
return;
}
2024-02-23 14:47:33 +08:00
// <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>
2024-02-21 09:31:34 +08:00
int rear = (front + queSize) % queCapacity;
2024-02-23 14:47:33 +08:00
// <20><> num <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β
2024-02-21 09:31:34 +08:00
nums[rear] = num;
queSize++;
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD><EFBFBD> */
int pop()
{
2024-02-21 09:31:34 +08:00
int num = peek();
2024-02-23 14:47:33 +08:00
// <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>
2024-02-21 09:31:34 +08:00
front = (front + 1) % queCapacity;
queSize--;
return num;
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
int peek() // peek <20><>Ϊ<EFBFBD><CEAA><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><E9BFB4><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
{
2024-02-21 09:31:34 +08:00
if (isEmpty())
2024-02-23 14:47:33 +08:00
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
2024-02-21 09:31:34 +08:00
return nums[front];
}
2024-02-23 14:47:33 +08:00
/* <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>
2024-02-21 09:31:34 +08:00
vector<int> arr(queSize);
2024-02-23 14:47:33 +08:00
for (int i = 0, j = front; i < queSize; i++, j++)
{
2024-02-21 09:31:34 +08:00
arr[i] = nums[j % queCapacity];
}
return arr;
}
};
/* Driver Code */
2024-02-23 14:47:33 +08:00
int main()
{
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int capacity = 10; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
ArrayQueue *queue = new ArrayQueue(capacity);
2024-02-23 14:47:33 +08:00
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-02-21 09:31:34 +08:00
queue->push(1);
queue->push(3);
queue->push(2);
queue->push(5);
queue->push(4);
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> queue = ";
2024-02-21 09:31:34 +08:00
printVector(queue->toVector());
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
int peek = queue->peek();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> peek = " << peek << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
/* Ԫ<>س<EFBFBD><D8B3><EFBFBD> */
2024-02-21 09:31:34 +08:00
peek = queue->pop();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> pop = " << peek << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> queue = ";
2024-02-21 09:31:34 +08:00
printVector(queue->toVector());
2024-02-23 14:47:33 +08:00
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
2024-02-21 09:31:34 +08:00
int size = queue->size();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
/* <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
2024-02-21 09:31:34 +08:00
bool empty = queue->isEmpty();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << empty << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
for (int i = 0; i < 10; i++)
{
2024-02-21 09:31:34 +08:00
queue->push(i);
queue->pop();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD> " << i << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> + <20><><EFBFBD>Ӻ<EFBFBD> queue = ";
2024-02-21 09:31:34 +08:00
printVector(queue->toVector());
}
2024-02-23 14:47:33 +08:00
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
2024-02-21 09:31:34 +08:00
delete queue;
return 0;
}