【牛客带你学编程C++方向】项目练习第8期(参考答案)
参考答案:
template <typename T>
class Queue
{
private:
T* Array;
int Capacity; //队列最大容量
int Front; //队列头位置
int Rear; //队列尾的位置
int Size; //队列中对象的数量
public:
Queue(int QueueSize)
:Front(0)
, Rear(1)
, Size(0)
, Capacity(QueueSize)
{
Array = new T[QueueSize];
}
~Queue()
{
delete[] Array;
}
//入队函数
void push(T x)
{
if (isFull())
{
return;
}
Array[Rear++] = x;
Rear = (++Rear) % Capacity;
++Size;
}
//出队函数
void pop()
{
if (isEmpty())
{
return;
}
++Front;
--Size;
}
bool isFull()
{
return Size == Capacity;
}
bool isEmpty()
{
return Size == 0;
}
int size()
{
return Size;
}
};
Tips:
牛客带你学编程-C++方向:【牛客带你学编程】【C++方向】0基础小白入门培养计划!

