设计一个循环队列QUEUE<T>,用data[0..MaxSize-1]存放队列元素,用front和rear分别作为队头和队尾指针,用tag标识队列可能空(false)或满(true),要求设计队列的相关基本运算算法。
时间: 2023-05-24 09:05:23 浏览: 75
```C++
template <typename T>
class QUEUE {
private:
T *data;
int front, rear, MaxSize;
bool tag; // false表示队列未满,true表示队列已满
public:
QUEUE(int MaxQueueSize) {
data = new T[MaxQueueSize];
MaxSize = MaxQueueSize;
front = rear = 0;
tag = false;
}
~QUEUE() {
delete[] data;
}
void EnQueue(T x) {
if (tag) {
cout << "Queue is Full.\n";
return;
}
data[rear] = x;
rear = (rear + 1) % MaxSize;
if (rear == front)
tag = true;
}
T DeQueue() {
if (!tag) {
cout << "Queue is Empty.\n";
return NULL;
}
T x = data[front];
front = (front + 1) % MaxSize;
if (rear == front)
tag = false;
return x;
}
bool IsEmpty() {
return (rear == front) && (!tag);
}
bool IsFull() {
return tag;
}
void ClearQueue() {
front = rear = 0;
tag = false;
}
int QueueLength() {
if (tag)
return MaxSize;
else
return (rear - front + MaxSize) % MaxSize;
}
T GetHead() {
if (!IsEmpty())
return data[front];
else
return NULL;
}
};
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)