队列(数组描述)

队列一个先进先出的线性表
(环形队列 逆时针)

#include<iostream>
#include"queue.h"
using namespace std;
template<class T>

class arrayQueue :public queue<T>
{
public:
    arrayQueue(int initialCapacity = 10);
    ~arrayQueue(){ delete[]queue; }
    bool empty()const { return queueFront == queueBack; }
    int size() const{ return (queueBack - queueFront + arrayLength) % arrayLength; }
    T & front()
    {
        if (queueFront == queueBack)
            cout << "the queue is Empty" << endl;
        return queue[(queueFront + 1) % arrayLength];
    }
    T & back()
    {
        if (queueBack == queueFront)
        {
            cout << "the queue is empty";
            //break;
        }
        return queue[queueBack];
    }
    void pop();
    void push(const T & theElement);
    //void addQueueLength(T *queue,int arrayLength);
private:
    int queueFront; //队首元素位置
    int queueBack; //队尾元素位置
    int arrayLength; //数组长度
    T *queue; //队列数组
};

template <class T>
arrayQueue<T>::arrayQueue(int initialCapacity)
{
    arrayLength = initialCapacity;
    queueBack = 0;
    queueFront = 0;
    queue = new T[arrayLength];
}

template<class T>
void arrayQueue<T>::pop()
{
    if (queueFront == queueBack)
    {
        cout << "Empty" << endl;
    }
    queueFront = (queueFront + 1) % arrayLength;
    queue[queueFront].~T();

}
template<class T>
void arrayQueue<T>::push(const T & theElement)
{
    if ((queueBack + 1) % arrayLength==queueFront)
    {
        int start = (queueFront + 1) % arrayLength;
        T *newQueue = new T[2 * arrayLength];
        if (start < 2)
        {//没有形成环型
            copy(queue + start, queue + start + arrayLength - 1, newQueue);
        }
        else
        {//队列成环形
            copy(queue + start, queue + arrayLength, newQueue);
            copy(queue, queue + queueBack, newQueue + arrayLength - start);
        }
        //设置新队列的首和尾元素
        queueFront = 2 * arrayLength - 1;
        queueBack = arrayLength - 2;
        arrayLength *= 2;
        delete[] queue;
        queue = newQueue;
    }
    queueBack = (queueBack + 1) % arrayLength;
    queue[queueBack] = theElement;
}

//template<class T>
//void arrayQueue<T>::addQueueLength(T *queue, int arrayLength)
//{
//  
//}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值