数据结构(五)、C++链表实现队列

链表实现队列

1、实现原理

通过操作数组的下标来实现队列结构。
栈数组结构体定义:

    template<class T>
    class Queue
    {
    public:
        struct node
        {
            node() :m_data(), m_next(nullptr) {}
            T m_data;
            node* m_next;
        };
        Queue() :m_front(nullptr), m_fear(nullptr) {}
    private:
        node* m_front;   // 头节点
        node* m_fear;    // 尾节点
    }

(1)、入队实操说明

  • 插入元素(1)
        /*
             队列链表m_stNode:
             m_front = m_fear = nullptr     temp节点    -----  m_next           
                                                        | 1 |----------> nullptr
                                                        -----    

                                       ||
                                       ||   m_front = m_fear = temp
                                       ||
                                      \\//

                                -----  m_next              
                                | 1 |----------> nullptr   
                                -----                      
                                |   |
                           m_front  m_fear        
        */
  • 插入元素(2)
        /*
             队列链表m_stNode:
                      -----  m_next                  
                      | 1 |----------> nullptr         temp节点    -----  m_next
                      -----                                        | 2 |----------> nullptr
                      |   |                                        -----
                 m_front  m_fear    

                                       ||
                                       ||   m_fear->m_next = temp;
                                       ||   m_fear = temp;        
                                      \\//

                     -----  m_next    -----  m_next
                     | 2 |----------> | 1 |----------> nullptr
                     -----            -----
                      |                |   
                    m_fear          m_front  
        */
  • 插入元素(3)
        /*
             队列链表m_stNode:
                 -----  m_next    -----  m_next
                 | 2 |----------> | 1 |----------> nullptr       temp节点    -----  m_next
                 -----            -----                                      | 3 |----------> nullptr
                  |                |                                         -----
                m_fear          m_front

                                       ||
                                       ||   m_fear->m_next = temp;
                                       ||   m_fear = temp;
                                      \\//

                   -----  m_next     -----  m_next    -----  m_next
                   | 3 |---------->  | 2 |----------> | 1 |----------> nullptr
                   -----             -----            -----
                      |                                 |
                    m_fear                           m_front
        */

(2)、出队实操说明

  • 移除首元素(1)
        /*
             队列链表m_stNode:
                   -----  m_next     -----  m_next    -----  m_next
                   | 3 |---------->  | 2 |----------> | 1 |----------> nullptr
                   -----             -----            -----
                      |                                 |
                    m_fear                           m_front

                                       ||
                                       ||   m_front = m_front->m_next
                                       ||   
                                      \\//

                   -----  m_next     -----  m_next    
                   | 3 |---------->  | 2 |----------> nullptr
                   -----             -----            
                      |                |
                    m_fear          m_front
        */
  • 移除首元素(2)
        /*
             队列链表m_stNode:
                   -----  m_next     -----  m_next
                   | 3 |---------->  | 2 |----------> nullptr
                   -----             -----
                      |                |
                    m_fear          m_front

                                       ||
                                       ||   m_front = m_front->m_next
                                       ||
                                      \\//

                   -----  m_next   
                   | 3 |----------> nullptr
                   -----           
                   |  |
               m_fear m_front
        */
  • 移除首元素(3)
        /*
             队列链表m_stNode:
                   -----  m_next
                   | 3 |----------> nullptr
                   -----
                   |  |
               m_fear m_front

                                       ||   m_fear = m_fear->m_next;
                                       ||   m_front = m_front->m_next
                                       ||
                                      \\//
             
                                    nullptr                               
        */

2、整体代码

#include <iostream>

namespace queue_list
{
        template<class T>
    class Queue
    {
    public:
        struct node
        {
            node() :m_data(), m_next(nullptr) {}
            T m_data;
            node* m_next;
        };
        Queue() :m_front(nullptr), m_fear(nullptr) {}

        /**
         * @brief 尾插
         */
        void push(T m_pdata)
        {
            node* temp = new node;
            temp->m_data = m_pdata;
            temp->m_next = nullptr;

            if (m_front == nullptr)         // 插入第一个元素时,让头尾节点都指向temp这块内存
            {
                m_front = temp;
                m_fear = temp;
            }
            else
            {
                m_fear->m_next = temp;    // 将temp节点赋值到尾指针的下一个指针,因为头尾节点都指向同一块内存,所以头节点保存全部队列节点
                m_fear = temp;            // 将temp赋值给m_fear尾节点,方便下一次插入操作
            }
        }

        /**
         * @brief 头删
         */
        void pop()
        {
            if (m_front == nullptr)       // 头节点为空,表示队列为空
            {
                std::cout << "queue is empty" << std::endl;
            }
            else
            {
                node* temp = m_front;             // 先保存头节点
                if (m_front == m_fear)            // 当队列只剩下一个元素时,操作尾节点的下一个指针为空
                {
                    m_fear = m_fear->m_next;
                }
                m_front = m_front->m_next;        // 头节点指向头节点原来的下一个指针
                delete temp;
            }
        }

        /**
         * @brief 打印
         */
        void print_queue()
        {
            node* temp = m_front;
            while (temp != nullptr)
            {
                std::cout << temp->m_data << " ";
                temp = temp->m_next;
            }
            std::cout << std::endl;
        }

    private:
        node* m_front;   // 头节点
        node* m_fear;    // 尾节点
    };

    void test_queue_list()
    {
        queue_list::Queue<int> m_queue;
        m_queue.push(1);
        m_queue.push(2);
        m_queue.push(3);
        m_queue.print_queue();       // 1 2 3

        m_queue.pop();
        m_queue.print_queue();       // 2 3

        m_queue.pop();
        m_queue.print_queue();       // 3

        m_queue.pop();
        m_queue.print_queue();
    }

}// namespace queue_list

int main()
{
    queue_list::test_queue_list();
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值