循环队列中添加和删除元素时并不像链式队列那样使用动态内存分配,一般采用固定的队列长度,一次分配一片内存用于存储数据,等到队列使用完毕后释放掉即可。内存使用效率比链式队列高,也比较容易实现。队列的数据定义如下:
unsigned int size;//存储总的队列大小,最多可以存储size-1个数据
int head;//记录队头的下标
int rear;//记录队尾的下标
T* data_buff;//数据存储缓冲区
队列为空时:
rear=head;
元素入队时,向前移动rear一次,但是rear所指的那个地方并不存储数据,循环队列总会空出一个位置不存储元素:
data_buff[rear] = data;
rear = (rear + 1) % size;
上图中,表示向队列中依次添加元素‘A’、‘B’。
出队时,队头head向前移动一次;
data_buff[head] = 0;
head++;
上图中将队头的元素‘A’出队。
判断队列已满时,采用的方法与链式队列不同,判断的条件变为:
head ==(rear + 1) % size
队列已满的情况。
接下来使用C++模板编程实现一个循环队列类。
template<typename T>
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;
}
};
老套路,测试一下:
int main()
{
RQueue<float> rq;
for (int i = 0; i < 9; i++)//将9个元素入队
rq.enqueue(i);
rq.show();
for (int i = 0; i < 6; i++)//出队6个元素
{
cout << rq.getHeadElem() << " ";
rq.dequeue();
}
cout << endl;
for (int i = 0; i < 5; i++)//入队5个元素
rq.enqueue(i);
rq.show();
system("pause");
return 0;
}