当前位置:C++技术网 > 资讯 > 数据结构笔记分享:1 循环队列

数据结构笔记分享:1 循环队列

更新时间:2015-11-05 22:58:36浏览次数:1+次

定义:把数组从逻辑上看成是一个头尾相连的环。

下面几个图形象的给你解释一下循环队列

实现循环队列几个小操作操作:
    (1) 为使入队和出队实现循环,可以利用取余运算符%。
    (2) 队头指针进一:
           front=(front+1) % maxSize;
    (3) 队尾指针进一:
           rear=(rear+1) % maxSize;
    (4) 空队列:当front==rear 时为空队列,
    (5) 满队列:当(rear+1) % maxSize ==front 时
       为满队列。(满队列时实际仍有一个元素的空

       间未使用。看上图)

模板抽象定义:

template <class T>
class SeqQueue:public Queue<T>
{ public:
    SeqQueue(int mSize);
    ~SeqQueue(){ delete []q;}
    bool IsEmpty() const;      
    bool IsFull() const;
    bool Front(T& x)const;
    bool EnQueue(T x);
    bool DeQueue();
    void Clear(){front=rear=0;}
  private:
    int front,rear;//定义对头队尾元素
    int maxSize;
    T *q;
};