#include<iostream>
using namespace std;
const int QueueSize = 1000;
class Cirqueue
{
public:
Cirqueue()
{
front = rear = 0;
}
~Cirqueue() { cout << "fade"; }
void Aqueue(int x);
int Bqueue();
int Cqueue();
bool Empty()
{
if (front == rear)
return 1;
else
return 0;
}
private:
int data[QueueSize];
int front, rear;
};
void Cirqueue::Aqueue(int x)
{
if ((rear + 1) % QueueSize == front)
cout << "queue is full,can't put " << x << " into it" << endl;
else
{
data[rear] = x;
rear = (rear + 1) % QueueSize;
}
}
int Cirqueue::Bqueue()
{
if (Empty()) {
cout << "queue is empty" << endl;
return 0;
}
else
{
front = (front + 1) % QueueSize;
return data[front - 1];
}
}
int Cirqueue::Cqueue()
{
if (Empty()) {
cout << "queue is empty" << endl;
return 0;
}
else
{
return data[(front) % QueueSize];
}
}
int main()
{
Cirqueue Q;
Q.Aqueue(5);
Q.Aqueue(9);
Q.Aqueue(7);
cout << Q.Bqueue() << endl;
cout << Q.Cqueue() << endl;
cout << Q.Bqueue() << endl;
cout << Q.Bqueue() << endl;
Q.Bqueue();
return 0;
}
循环队列的实现
于 2023-05-20 12:09:45 首次发布
该文章定义了一个名为Cirqueue的环形队列类,包含了入队(Aqueue)、出队(Bqueue)和查看队首元素(Cqueue)的方法,并提供了队列满和队列空的判断。在主函数中,向队列添加元素并进行出队和查看操作。
摘要由CSDN通过智能技术生成