使用数组实现固定长度的队列
主要的思路是:2个指针start和end,加上一个变量cur_size 用来表示当前队列的元素个数,只要size大于数组长度就不能再入队,size小于0就不能再出队列了。
入队:移动end变量,每次让end指向队尾元素的下一个位置,每次入队只需把元素放入到end位置。如果到达数组尾部且队列没有满,让end为0,继续循环
出队:移动start变量,每次start变量都指向队列头部,出队列时,取出start变量位置的元素,移动start,当start到达数组尾部但队列未空时,让start=0,继续循环。
class Queue
{
private:
int _size;
int *arr;
int _start=0;
int _end=0;
int cur_size=0;
public:
bool _empty()
{
return cur_size==0;
}
int length()
{
return cur_size;
}
bool EnQueue(int value)
{
if(cur_size>=_size)
return false;
arr[_end++]=value;
cur_size++;
if(_end==_size)
_end=0;
return true;
}
int ExQueue()
{
if(cu