循环队列的实现
作者:互联网
#include<iostream>
using namespace std;
const int QueueSize = 100;
class CirQueue
{
public:
CirQueue();
void EnQueue(int x);
int DeQueue();
int GetHead();
private:
int data[QueueSize];
int front, rear;
};
CirQueue::CirQueue()
{
rear = front = QueueSize - 1;
}
void CirQueue::EnQueue(int x)
{
if ((rear + 1) % QueueSize == front)throw"上溢";
else
{
rear = (rear + 1) % QueueSize;
data[rear] = x;
}
}
int CirQueue::DeQueue()
{
if (rear == front)throw"下溢";
else
{
front = (front + 1) % QueueSize;
return data[front];
}
}
int CirQueue::GetHead()
{
if (rear == front)throw"下溢";
else
{
return data[(front + 1) % QueueSize];
}
}
int main()
{
CirQueue s;
cout << "对5和8进行入队操作" << endl;
s.EnQueue(5);
s.EnQueue(8);
cout << s.GetHead() << endl;
cout << s.DeQueue() << endl;
cout << s.GetHead() << endl;
}
标签:CirQueue,cout,队列,实现,int,循环,front,QueueSize,rear 来源: https://blog.csdn.net/Cc_010501/article/details/122029814