现在的位置: 首页 > 综合 > 正文

如何实现一个循环队列

2013年10月02日 ⁄ 综合 ⁄ 共 3344字 ⁄ 字号 评论关闭

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

template<typename T  /*元素类型*/ , unsigned int N /*容量*/ >
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<T, N>& 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<T, N>& other) {
        if (this != &other) {                  // 检查自赋值
             CyclicQueue<T, N> 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<T, N>& 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<T, N>& 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;             // 有效元素个数
};

抱歉!评论已关闭.