C++数据结构之数组队列模版实现

队列是一种先入先出的数据结构(FIFO),只允许在前端(front)删除,在后端(rear)插入。容量为capacity大小的内存,只能存capacity-1的元素,其中rear的位置始终为空。

本文实现的队列,功能如下:

1 获取元素内容

2 修改任意元素内容

3 出入队列

4 删除首尾元素

5 手动增加队列大小

定义:

template <typename T>
class ArrayQueue {
    private:
        T *array;
        
        int front;
        int rear;
        int capacity;
    
    public:
        ArrayQueue(const int m_size = 100):capacity(m_size), front(0), rear(0){this->array = new T[capacity];}
        ArrayQueue(const T &data, const int m_size):capacity(m_size), front(0), rear(1){this->array = new T[capacity], this->array[0] = data;}
        ArrayQueue(const ArrayQueue<T> &queue);
        ArrayQueue & operator=(const ArrayQueue<T> &queue);
        ~ArrayQueue();
        
        void clear();
        bool isEmpty();
        bool isFull();
        int length();
        void show();
        
        bool GetFrontNode(T &data);
        bool GetRearNode(T &data);
        bool GetPosNode(T &data, int pos);
        
        bool SetFrontNode(const T &data);
        bool SetRearNode(const T &data);
        bool SetPosNode(const T &data, int pos);
                
        bool PushRearNode(const T &data);

        bool PopFrontNode(T &data);
        
        bool DelFrontNode();
        bool DelRearNode();
        bool IncreaseQueue(int len);
        
        /*
        function:find the position of node which equal to data in the list
        data:the data which compared
        return value:the position which equal to data;
        this function need edit accoding the T
        */
        int GetFirstNodePos(T &data);
        /*
        function:find the position of node which equal to data in the list
        data:the data which compared
        pos:the array which storage the position
        return value:the num which equal to data;
        this function need edit accoding the T
        */
        int GetNodePos(T &data,int *pos);
};

成员函数实现:

扫描二维码关注公众号,回复: 3396922 查看本文章

复制构造函数

template <typename T>
ArrayQueue<T>::ArrayQueue(const ArrayQueue<T> &queue)
{
    this->array = new T[queue.capacity];
    memcpy(this->array, queue.array, queue.capacity);
    this->front = queue.front;
    this->rear = queue.rear;
    this->capacity = queue.capacity;
}

=运算符重载

template <typename T>
ArrayQueue<T> & ArrayQueue<T>::operator=(const ArrayQueue<T> &queue)
{
    this->array = new T[queue.capacity];
    memcpy(this->array, queue.array, queue.capacity);
    this->front = queue.front;
    this->rear = queue.rear;
    this->capacity = queue.capacity;
    return *this;
}

析构函数

template <typename T>
ArrayQueue<T>::~ArrayQueue()
{
    delete [] this->array;
}
       清除队列元素
template <typename T>
void ArrayQueue<T>::clear()
{
    this->front = 0;
    this->rear = 0;
}

判断队列是否为空

template <typename T>
bool ArrayQueue<T>::isEmpty()
{
    if(this->front == this->rear)
        return true;
    return false;
}

判断队列是否满

template <typename T>
bool ArrayQueue<T>::isFull()
{
    if((this->rear + 1)%this->capacity == this->front)
        return true;
    return false;
    
}

获取队列长度
template <typename T>
int ArrayQueue<T>::length()
{
    return (this->rear - this->front + capacity)%capacity;
}

遍历队列

template <typename T>
void ArrayQueue<T>::show()
{
    std::cout<<"The sum of data in the queue is: "<<this->length()<<" max size is: "<<this->capacity<<std::endl;
    std::cout<<"detail:";
    
    int i, len = this->length(), j = this->front;
    for(int i=0; i<len; i++){
        std::cout<<this->array[j]<<" ";
        j = (j+1)%capacity;
    }
    std::cout<<std::endl;
}

获取队首队尾及pos处元素

template <typename T>
bool ArrayQueue<T>::GetFrontNode(T &data)
{
    return GetPosNode(data, 0);
}

template <typename T>
bool ArrayQueue<T>::GetRearNode(T &data)
{
    return GetPosNode(data, this->length()-1);
}

template <typename T>
bool ArrayQueue<T>::GetPosNode(T &data, int pos)
{
    if(this->front == this->rear){
        ULOGW("empty array");
        return false;
    }
    
    if((pos<0) || (pos>(this->length()-1))){
        ULOGW("invalid pos or pos ",pos ,"th is beyond the max array index");
        return false;
    }
    
    data = this->array[(this->front + pos)%capacity];
    return true;
}

修改队尾队尾及pos处元素

template <typename T>
bool ArrayQueue<T>::SetFrontNode(const T &data)
{
    return SetPosNode(data, 0);
}

template <typename T>
bool ArrayQueue<T>::SetRearNode(const T &data)
{
    return SetPosNode(data, this->length()-1);
}
        
template <typename T>
bool ArrayQueue<T>::SetPosNode(const T &data, int pos)
{
    if(this->front == this->rear){
        ULOGW("empty array");
        return false;
    }
    
    if((pos<0) || (pos>(this->length()-1))){
        ULOGW("invalid pos or pos ",pos ,"th is beyond the max array index");
        return false;
    }
    
    this->array[(this->front + pos)%capacity] = data;
    return true;
}

元素入队

template <typename T>
bool ArrayQueue<T>::PushRearNode(const T &data)
{
    if((this->rear + 1)%this->capacity == this->front){
        ULOGW("full array");
        return false;
    }
    this->array[this->rear] = data;
    this->rear = (this->rear + 1)%capacity;
    return true;
}

元素出队

template <typename T>
bool ArrayQueue<T>::PopFrontNode(T &data)
{
    if(this->rear == this->front){
        ULOGW("empty array");
        return false;
    }
    data = this->array[this->front];
    this->front = (this->front+1)%capacity;
    return true;
}

删除队首队尾元素

template <typename T>
bool ArrayQueue<T>::DelFrontNode()
{
    if(this->rear == this->front){
        ULOGW("empty array");
        return false;
    }
    this->front = (this->front+1)%capacity;
    return true;
}

template <typename T>
bool ArrayQueue<T>::DelRearNode()
{
    if(this->rear == this->front){
        ULOGW("empty array");
        return false;
    }
    this->rear = (this->rear + capacity -1)%capacity;
    return true;
}

手动增加队列空间大小

template <typename T>
bool ArrayQueue<T>::IncreaseQueue(int len)
{    
    T *array_ = new T[this->capacity + len]; //未检测分配内存是否成功
    memcpy(array_, this->array, this->capacity);
    this->capacity = this->capacity + len;
    delete [] this->array;
    this->array = array_;
    return true;
}

如有问题欢迎指正。

源码链接:https://download.csdn.net/download/zhouchao_0321/10679295

猜你喜欢

转载自blog.csdn.net/zhouchao_0321/article/details/82789978