如何实现一个循环队列
2008-04-04 18:37
399 查看
下面是一个循环队列的完整实现,欢迎读者朋友参考和指正:
template
class CyclicQueue {
public:
typedef T value_type;
typedef size_t size_type;
typedef T& reference;
typedef const T& const_reference;
CyclicQueue() : m_popPos(0), m_count(0) {
assert(N > 0);
m_beginPtr = (T*)(::operator new(sizeof(T) * N)); // 分配原始空间
}
~CyclicQueue() {
_Clear(); // this->_Clear();
::operator delete((void*)m_beginPtr);
}
CyclicQueue(const CyclicQueue& copy) : m_popPos(0), m_count(0) {
assert(N > 0);
m_beginPtr = (T*)(::operator new(sizeof(T) * N)); // 分配原始空间
size_t copyPos = copy.m_popPos;
for (size_type idx = 0; idx < copy.m_count; ++idx) {
_Copy(idx, copy.m_beginPtr[copyPos]); // this->_Copy();
++copyPos; copyPos %= N; ++m_count;
}
}
CyclicQueue& operator=(const CyclicQueue& other) {
if (this != &other) { // 检查自赋值
CyclicQueue temp(other); // 调用拷贝构造函数
_Swap(temp); // this->_Swap();
}
return (*this);
}
bool is_empty() const { return (m_count == 0); }
bool is_full() const { return (m_count == N); }
value_type front() {
assert(m_count != 0);
return (m_beginPtr[m_popPos]);
}
value_type front() const {
assert(m_count != 0);
return (m_beginPtr[m_popPos]);
}
value_type back() {
return _Back(); // this->_Back();
}
value_type back() const {
return _Back(); // this->_Back();
}
bool push(const_reference data = T()) {
if (m_count < N) { // 不满!
size_type pushPos = (m_popPos + m_count) % N;
_Copy(pushPos, data); // this->_Copy();
++m_count;
return true;
}
return false;
}
bool pop(reference data) {
if (m_count > 0) { // 不空!
data = m_beginPtr[m_popPos]; // operator=
_Destroy(m_popPos); // this->_Destroy();
--m_count; ++m_popPos; m_popPos %= N; // 新的pop位置
return true;
}
return false;
}
size_type size() const { return m_count; }
size_type capacity() const { return N; }
void clear() { _Clear(); } // this->_Clear();
void swap(CyclicQueue& other) {
_Swap(other); // this->_Swap();
}
private:
void _Clear() {
for (; m_count > 0; --m_count) {
_Destroy(m_popPos); // this->_Destroy();
++m_popPos; m_popPos %= N;
}
m_popPos = 0;
}
void _Destroy(size_type idx) {
assert(idx < N);
T *pTemp = (m_beginPtr + idx);
pTemp->~T(); // 调用析构函数销毁元素对象
}
void _Copy(size_type idx, const_reference data) {
assert(idx < N);
T *pTemp = (m_beginPtr + idx);
new ((void*)pTemp) T(data); // 调用placement new和拷贝构造函数复制对象
}
void _Swap(CyclicQueue& other) {
std::swap(m_beginPtr, other.m_beginPtr);
std::swap(m_popPos, other.m_popPos);
std::swap(m_count, other.m_count);
}
value_type _Back() const {
assert(m_count != 0);
size_type pushPos = (m_popPos + m_count) % N;
if (pushPos == 0)
return (*(m_beginPtr + N - 1));
return (m_beginPtr[pushPos - 1]);
}
value_type *m_beginPtr; // 队列存储空间起始位置
size_type m_popPos; // 下次pop位置
size_type m_count; // 有效元素个数
};
template
class CyclicQueue {
public:
typedef T value_type;
typedef size_t size_type;
typedef T& reference;
typedef const T& const_reference;
CyclicQueue() : m_popPos(0), m_count(0) {
assert(N > 0);
m_beginPtr = (T*)(::operator new(sizeof(T) * N)); // 分配原始空间
}
~CyclicQueue() {
_Clear(); // this->_Clear();
::operator delete((void*)m_beginPtr);
}
CyclicQueue(const CyclicQueue& copy) : m_popPos(0), m_count(0) {
assert(N > 0);
m_beginPtr = (T*)(::operator new(sizeof(T) * N)); // 分配原始空间
size_t copyPos = copy.m_popPos;
for (size_type idx = 0; idx < copy.m_count; ++idx) {
_Copy(idx, copy.m_beginPtr[copyPos]); // this->_Copy();
++copyPos; copyPos %= N; ++m_count;
}
}
CyclicQueue& operator=(const CyclicQueue& other) {
if (this != &other) { // 检查自赋值
CyclicQueue temp(other); // 调用拷贝构造函数
_Swap(temp); // this->_Swap();
}
return (*this);
}
bool is_empty() const { return (m_count == 0); }
bool is_full() const { return (m_count == N); }
value_type front() {
assert(m_count != 0);
return (m_beginPtr[m_popPos]);
}
value_type front() const {
assert(m_count != 0);
return (m_beginPtr[m_popPos]);
}
value_type back() {
return _Back(); // this->_Back();
}
value_type back() const {
return _Back(); // this->_Back();
}
bool push(const_reference data = T()) {
if (m_count < N) { // 不满!
size_type pushPos = (m_popPos + m_count) % N;
_Copy(pushPos, data); // this->_Copy();
++m_count;
return true;
}
return false;
}
bool pop(reference data) {
if (m_count > 0) { // 不空!
data = m_beginPtr[m_popPos]; // operator=
_Destroy(m_popPos); // this->_Destroy();
--m_count; ++m_popPos; m_popPos %= N; // 新的pop位置
return true;
}
return false;
}
size_type size() const { return m_count; }
size_type capacity() const { return N; }
void clear() { _Clear(); } // this->_Clear();
void swap(CyclicQueue& other) {
_Swap(other); // this->_Swap();
}
private:
void _Clear() {
for (; m_count > 0; --m_count) {
_Destroy(m_popPos); // this->_Destroy();
++m_popPos; m_popPos %= N;
}
m_popPos = 0;
}
void _Destroy(size_type idx) {
assert(idx < N);
T *pTemp = (m_beginPtr + idx);
pTemp->~T(); // 调用析构函数销毁元素对象
}
void _Copy(size_type idx, const_reference data) {
assert(idx < N);
T *pTemp = (m_beginPtr + idx);
new ((void*)pTemp) T(data); // 调用placement new和拷贝构造函数复制对象
}
void _Swap(CyclicQueue& other) {
std::swap(m_beginPtr, other.m_beginPtr);
std::swap(m_popPos, other.m_popPos);
std::swap(m_count, other.m_count);
}
value_type _Back() const {
assert(m_count != 0);
size_type pushPos = (m_popPos + m_count) % N;
if (pushPos == 0)
return (*(m_beginPtr + N - 1));
return (m_beginPtr[pushPos - 1]);
}
value_type *m_beginPtr; // 队列存储空间起始位置
size_type m_popPos; // 下次pop位置
size_type m_count; // 有效元素个数
};
相关文章推荐
- 如何实现一个循环队列
- 如何实现一个循环队列
- 如何实现一个循环队列
- 如何实现一个循环队列
- 如何实现一个循环队列
- 如何实现一个循环队列
- 如何实现一个循环显示超长图片的控件
- C++实现的一个循环队列
- 如何实现一个循环缓冲区模型
- 循环队列的一个简单实现
- 如何实现一个高效的队列
- 如何用两个队列实现一个栈,并分析有关栈操作的运行时间。
- 如何实现重复调用一个函数,非循环
- 如何实现一个循环缓冲区模型
- 这里实现一个基于数组的线程安全的循环队列
- 如何用两个栈实现一个队列
- 如何实现多个线程写、一个线程读的队列,并且所有线程间不需要等待同步
- 数据结构:栈和列之如何用两个队列实现一个栈?两个栈实现一个队列?
- 如何用两个栈来实现一个队列,并分析有关队列操作的运行时间。
- 如何用golang实现一个定时器任务队列