#include <QVector>
template<typename T>
class CircularQueue
{
public:
explicit CircularQueue(int size) : m_size(size), m_head(0), m_tail(0)
{
m_data.resize(size);
}
bool isEmpty() const { return m_head == m_tail; }
bool isFull() const { return (m_tail + 1) % m_size == m_head; }
int size() const { return (m_tail - m_head + m_size) % m_size; }
void enqueue(const T &value)
{
if (!isFull())
{
m_data[m_tail] = value;
m_tail = (m_tail + 1) % m_size;
}
}
T dequeue()
{
if (!isEmpty())
{
T value = m_data[m_head];
m_head = (m_head + 1) % m_size;
return value;
}
return T();
}
private:
QVector<T> m_data;
int m_size;
int m_head;
int m_tail;
};
QVector实现循环队列
最新推荐文章于 2024-07-23 09:39:50 发布