循环队列的实现

该文章定义了一个名为Cirqueue的环形队列类,包含了入队(Aqueue)、出队(Bqueue)和查看队首元素(Cqueue)的方法,并提供了队列满和队列空的判断。在主函数中,向队列添加元素并进行出队和查看操作。
摘要由CSDN通过智能技术生成

#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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tinkinon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值