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

链队列的存储结构

  将对头指针front指向链队列的头结点(头节点为空,不存数据),队尾指针rear指向终端结点。元素从队尾进入,队首出列。

元素为空时,队尾指针指向队头指针。

链式队列模版实现:

功能:

1 创建

2 遍历

4 入队,出队

5 获取队尾队首队中某位置值

6 修改队尾队首队中某位置值

7 删除队尾队首队中某位置值

8 清空复制等

节点定义

template <typename T> 
struct Link {
    T data;
    struct Link *next;
    构造函数
    Link(){this->next = nullptr;}
    Link(const T &data){this->data = data; this->next = nullptr;}
    Link(const T &data, struct Link *next){this->data = data; this->next = next;}
    Link(const struct Link &node){this->data = node.data; this->next = node.next;}
};

别名定义

template <typename T> 
using LinkNode = struct Link<T>;

队列定义

template <typename T> 
class LinkQueue {
    private:
        LinkNode <T> *front;
        LinkNode<T> *rear;
    public:

构造函数
        LinkQueue() {this->front = this->rear = new LinkNode<T>; this->rear->next = nullptr;}
        LinkQueue(const T &data){this->front = new LinkNode<T>; this->rear = new LinkNode<T>(data); this->front->next = this->rear;}
        LinkQueue(const LinkNode<T> &node){this->front = new LinkNode<T>; this->rear = new LinkNode<T>(node); this->front->next = this->rear;}
        LinkQueue(const LinkQueue<T> &queue);

        LinkQueue & operator=(const LinkQueue<T> &queue);
        ~LinkQueue();
        void clear();
        bool isEmpty();
        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 DelPosNode(int pos);
        
        
        /*
        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);

};

成员函数实现:

复制构造函数:

template <typename T>
LinkQueue<T>::LinkQueue(const LinkQueue<T> &queue)
{
    ULOGE("LINKQUEUE destruct");
    this->front = this->rear = new LinkNode<T>;
    this->rear->next = nullptr;
    if(queue.rear == queue.front){
        ULOGW("empty queue");
        this->front->next = nullptr;
        return;
    }
    LinkNode<T> *p1 = queue.front->next, *p2 = this->front, *p3;
    //this->front->next = p2;
    //p2 = p2->next;
    while(p1->next != nullptr){
        p3 = new LinkNode<T>(*p1);
        p2->next = p3;
        p2 = p2->next;
        p1 = p1->next;
    }    
    p3 = new LinkNode<T>(*p1);
    p2->next = p3;
    this->rear = p3;
    this->rear->next = nullptr;
}

=函数符重载

template <typename T>
LinkQueue<T> & LinkQueue<T>::operator=(const LinkQueue<T> &queue)
{
    ULOGE("LINKQUEUE111 destruct");
    this->front = this->rear = new LinkNode<T>;
    this->rear->next = nullptr;
    if(queue.rear == queue.front){
        ULOGW("empty queue");
        this->front->next = nullptr;
        return *this;
    }
    LinkNode<T> *p1 = queue.front->next, *p2 = this->front, *p3;
    //this->front->next = p2;
    //p2 = p2->next;
    while(p1->next != nullptr){
        p3 = new LinkNode<T>(*p1);
        p2->next = p3;
        p2 = p2->next;
        p1 = p1->next;
    }    
    p3 = new LinkNode<T>(*p1);
    p2->next = p3;
    this->rear = p3;
    this->rear->next = nullptr;
    return *this;
}

析构函数

template <typename T>
LinkQueue<T>::~LinkQueue()
{
    LinkNode<T> *p = this->front;
    while(p != nullptr){
        p = p->next;
        delete this->front;
        this->front = p;
    }
}

清除函数

template <typename T>
void LinkQueue<T>::clear(){
    LinkNode<T> *p1 = this->front->next;
    LinkNode<T> *p2 = p1;
    while(p1 != nullptr){    
        p1 = p1->next;
        delete p2;
        p2 = p1;
    }
    this->rear = this->front;
    this->front->next = nullptr;
}

判断是否为空

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

获取长度

template <typename T>
int LinkQueue<T>::length()
{
    int len = 0;
    LinkNode<T> *p = this->front->next;
    while(p != nullptr){
        p = p->next;
        ++len;
    }
    return len;
}

遍历

template <typename T>
void LinkQueue<T>::show()
{
    LinkNode<T> *p = this->front->next;
    std::cout<<"The sum of data in the queue is: "<<length()<<std::endl;
    std::cout<<"detail:";
    while(p != nullptr){
        std::cout<<p->data<<" ";
        p = p->next;
    }
    std::cout<<std::endl;
}

获取队头队尾及pos处值

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

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

template <typename T>
bool LinkQueue<T>::GetPosNode(T &data, int pos)
{
    if(pos < 0){
        ULOGW("invalue pos");
        return false;
    }
    
    int pos1 = pos;
    LinkNode<T> *p = this->front->next;
    while((p != nullptr)&& pos){
        --pos;
        p = p->next;
    }
    
    if(p == nullptr){
        ULOGW("no ", pos1, "th node");
        return false;
    }
    data = p->data;
    return true;
}

修改队头队尾及pos位置值

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

template <typename T>
bool LinkQueue<T>::SetRearNode(const T &data)
{
    return SetPosNode(data, length()-1);
}
        
template <typename T>
bool LinkQueue<T>::SetPosNode(const T &data, int pos)
{
    if(pos < 0){
        ULOGW("invalue pos");
        return false;
    }
    
    int pos1 = pos;
    LinkNode<T> *p = this->front->next;
    while((p != nullptr)&& pos){
        --pos;
        p = p->next;
    }
    
    if(p == nullptr){
        ULOGW("no ", pos1, "th node");
        return false;
    }
    p->data = data;
    return true;
}

入队

template <typename T>
bool LinkQueue<T>::PushRearNode(const T &data)
{
    LinkNode<T> *p = new LinkNode<T>(data);
    this->rear->next = p;
    this->rear = p;
}

出队

template <typename T>
bool LinkQueue<T>::PopFrontNode(T &data)
{
    if(this->rear == this->front){
        ULOGW("empty queue");
        return false;
    }
    
    LinkNode<T> *p = this->front->next;
    data = p->data;
    if(p != this->rear){
        this->front->next = p->next;
    }else{
        this->rear = this->front;
        this->front->next = nullptr;
    }
    delete p;
    return true;    
}
       删除队头队尾或pos处的值 
template <typename T>
bool LinkQueue<T>::DelFrontNode()
{
    return DelPosNode(0);
}

template <typename T>
bool LinkQueue<T>::DelRearNode(){
    return DelPosNode(length()-1);
}
        
template <typename T>
bool LinkQueue<T>::DelPosNode(int pos)
{
    if(pos < 0){
        ULOGW("invalue pos");
        return false;
    }
    
    int pos1 = pos;
    LinkNode<T> *p1 = this->front;
    LinkNode<T> *p2 = p1;
    p1 = p1->next;
    while((p1 != nullptr)&& pos){
        --pos;
        p2 = p1;
        p1 = p1->next;
    }
    if(p1 == nullptr){
        ULOGW("no ", pos1, "th node");
        return false;
    }
    if(p1 != this->rear){
        p2->next = p1->next;
        
    }else{
        this->rear = p2;
        this->rear->next = nullptr;
    }
    delete p1;
    
    return true;
}

如有问题欢迎大家指正。

源码下载地址:https://download.csdn.net/download/zhouchao_0321/10678106

猜你喜欢

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