怎么写循环队列adt模板c++代码
时间: 2023-10-13 15:17:05 浏览: 40
很高兴为您解答问题,以下是一个简单的循环队列ADT模板C++代码:
```
#include <iostream>
using namespace std;
template<typename T>
class CircularQueue {
private:
T *data;
int head, tail, capacity;
public:
CircularQueue(int k) {
data = new T[k];
capacity = k;
head = -1;
tail = -1;
}
bool enqueue(T value) {
if (isFull()) {
return false;
}
if (isEmpty()) {
head = 0;
}
tail = (tail + 1) % capacity;
data[tail] = value;
return true;
}
bool dequeue() {
if (isEmpty()) {
return false;
}
if (head == tail) {
head = -1;
tail = -1;
return true;
}
head = (head + 1) % capacity;
return true;
}
T front() {
return data[head];
}
T rear() {
return data[tail];
}
bool isEmpty() {
return head == -1;
}
bool isFull() {
return ((tail + 1) % capacity) == head;
}
};
```
希望能够帮到您。如果您有其他问题,请随时询问。