数据结构学习——队列的数组描述

本文深入探讨了队列数据结构的实现,包括其抽象类定义、数组描述及循环队列的处理方式。详细讲解了队列的插入和删除操作,以及如何避免队列溢出的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要开始学习队列啦~(鬼知道我为什么这么开心)
首先,老规矩,在开始前,先给出队列的抽象类

template<class T>
class queue
{
public:
    virtual ~queue(){}
    virtual bool empty()const=0;//返回true,当且仅当队列为空
    virtual int size()const=0;//返回队列中元素个数
    virtual T& front()=0;//返回头元素的引用
    virtual T& back()=0;//返回尾元素的引用
    virtual void pop()=0;//删除首元素
    virtual void push(const T& theElement)=0;//把元素theElement加入队尾
};

队列的数组描述可以写成循环数组,重点是队列是否满了的判断。
如果队列插满,则队列为空和队列满的判断条件一样,为了避免这种情况,队列不能插满,用这种方法,队列元素个数最多是arrayLength-1。因此在判断队列是否将要满的时候需要用到:
if ((theBack + 1) % arrayLength == theFront)
arrayLength是队列数组的长度
theBack是数组的尾的位置,theFront是数组头的前一个位置
具体看下图
在这里插入图片描述
理解了这个,再来看数组描述的队列:

class arrayQueue:public queue<T>
{
private:
    int theFront;//队首
    int theBack;//队尾
    int arrayLength;//数组长度
    T *queue;
public:
    arrayQueue(int initialCapacity=10)//构造函数
    {
        theBack=0;
        theFront=0;
        arrayLength=initialCapacity;
        queue=new T[arrayLength];
    }
    bool empty()const
    {
        if(theBack==theFront)
            return true;
        return false;
    }
    int size()const
    {
        return (theBack-theFront+arrayLength)%arrayLength;
    }
    T& front()
    {
        if(theBack==theFront)
            //抛出异常,队列为空
        int tmp=(theFront+1)%arrayLength;
        return queue[tmp];
    }
    T& back()
    {
        if(theBack==theFront)
            //抛出异常,队列为空
        return queue[theBack];
    }
    void pop()
    {
        if(theBack==theFront)
            //抛出异常,队列为空
        theFront=(theFront+1)%arrayLength;//更新theFront的值
        queue[theFront].~T();  //在数组上调用一个Template模板的析构函数
    }
    void push(const T& theElement);
};
void arrayQueue<T>::push(const T& theElement)
{
    if((theBack + 1) % arrayLength == theFront)//如果队列将要满
        //增加数组长度
    theBack=(theBack+1)%arrayLength;
    queue[theBack]=theElement;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值