实验三、循环队列
程序员文章站
2022-07-09 19:11:22
...
入队操作:将队尾指针在循环意义上+1,然后将待插入元素插入队尾
出队操作:将队头指针在循环意义上+1,然后读取并返回
#include
using namespace std;
const int cd = 10;
class cirqueue
{
public:
cirqueue()
{
front = rear = cd - 1;
}
~cirqueue() {}
void enqueue(int x);
int dequeue();
int get();
void print();
int empty()
{
return(front == rear) ? 1 : 0;
}
private:
int data[cd];
int front, rear;
};
void cirqueue::enqueue(int x)
{
if ((rear + 1) % cd == front)throw"上溢";
rear = (rear + 1) % cd;
data[rear] = x;
}
int cirqueue::dequeue()
{
if (rear == front)throw"下溢";
front = (front + 1) % cd;
return data[front];
}
int cirqueue::get()
{
if (rear == front)throw"下溢";
int i = (front + 1) % cd;
cout << data[i] << endl;
return 0;
}
void cirqueue::print()
{
for (int i = 0; i
上一篇: 阻塞队列(三):DelayQueue
下一篇: 队列(三) --- 队列的链式存储