循环队列

#include
using namespace std;

template
class RQueue
{
private:
unsigned int size;
int head;
int rear;
T* data_buff;
public:
RQueue() :size(10), head(0), rear(0)
{
data_buff = new T[size];
}
RQueue(int s) :size(s), head(0), rear(0)
{
data_buff = new T[size];
}
~RQueue()
{
delete [] data_buff;
}
void enqueue(T data)
{
if (isfull())
{
cout << “Error: This RQueue is full” << endl;
cout << “File Path =” << FILE`` << endl;
cout << “Function Name =” << FUNCTION << endl;
cout << “Line =” << LINE << endl;
throw bad_exception();
}
data_buff[rear] = data;
rear = (rear + 1) % size;
}
void dequeue()
{
if (isempty())
{
cout << “Error: This RQueue is empty” << endl;
cout << “File Path =” << FILE << endl;
cout << “Function Name =” << FUNCTION << endl;
cout << “Line =” << LINE << endl;
throw bad_exception();
}
data_buff[head] = 0;
head++;
}
T getHeadElem()
{
if (isempty())
{
cout << “Error: This RQueue is empty” << endl;
cout << “File Path =” << FILE << endl;
cout << “Function Name =” << FUNCTION << endl;
cout << “Line =” << LINE << endl;
throw bad_exception();
}
return data_buff[head];
}
unsigned int elem_available()
{
int temp;
if (rear > head)
temp = rear - head;
else
temp = size - head + rear;
return temp;
}
bool isempty()
{
return head == rear ? true : false;
}
bool isfull()
{
return head == (rear + 1) % size ? true : false;
}
void show()
{
for (int i = 0; i < elem_available()?
{
cout << data_buff[(i+head)%size]<<" ";
i = (i + 1) % size;
}
cout << endl;
}
};

作者:shuoyueqishilove
来源:CSDN
原文:https://blog.csdn.net/shuoyueqishilove/article/details/80498432

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值