`
阿尔萨斯
  • 浏览: 4144617 次
社区版块
存档分类
最新评论

如何实现一个循环队列

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

下面是一个循环队列的完整实现,欢迎读者朋友参考和指正:

template<typename t unsigned int n><br>class CyclicQueue {<br>public:<br> typedef T value_type;<br> typedef size_t size_type;<br> typedef T&amp; reference;<br> typedef const T&amp; const_reference;</typename>

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<t n>&amp; copy) : m_popPos(0), m_count(0) {<br> assert(N &gt; 0);<br> m_beginPtr = (T*)(::operator new(sizeof(T) * N)); // 分配原始空间<br> size_t copyPos = copy.m_popPos;<br> for (size_type idx = 0; idx _Copy(idx, copy.m_beginPtr[copyPos]); // this-&gt;_Copy();<br> ++copyPos; copyPos %= N; ++m_count;<br> }<br> }</t>

CyclicQueue& operator=(const CyclicQueue<t n>&amp; other) {<br> if (this != &amp;other) { // 检查自赋值<br> CyclicQueue<t n> temp(other); // 调用拷贝构造函数<br> _Swap(temp); // this-&gt;_Swap();<br> }<br> return (*this);<br> }</t></t>

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 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<t n>&amp; other) {<br> _Swap(other); // this-&gt;_Swap();<br> }</t>

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 T *pTemp = (m_beginPtr + idx);
pTemp->~T(); // 调用析构函数销毁元素对象
}

void _Copy(size_type idx, const_reference data) {
assert(idx T *pTemp = (m_beginPtr + idx);
new ((void*)pTemp) T(data); // 调用placement new和拷贝构造函数复制对象
}

void _Swap(CyclicQueue<t n>&amp; other) {<br> std::swap(m_beginPtr, other.m_beginPtr);<br> std::swap(m_popPos, other.m_popPos);<br> std::swap(m_count, other.m_count);<br> }</t>

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; // 有效元素个数
};




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics