STL:list实现

list是和vector类似的顺序型容器,也是是比vector更为复杂的容器。list是双向带头链表,初始有一个不存数据的头节点,并通过节点内指针将后续节点依次连接起来 。

相较于vector,list特点如下:

(1)list可以按需申请,释放不需要扩容操作,减少内存碎片。

(2)任意位置插入删除的效率是O(1)。

(3)不支持下标随机访问。

(4)cache缓存命中率低。

list结构较为复杂,就list节点来说,list自己本身和list节点不是一样的结构,list包含list节点,因此需要分开设计。

cpp 复制代码
#include<iostream>
using namespace std;


namespace YHY
{
    // List的节点类
    template<class T>
    struct ListNode
    {
        ListNode(const T& val = T())
        {
            _val = val;
        }
        ListNode<T>* _pPre = nullptr;
        ListNode<T>* _pNext = nullptr;
        T _val;
    };


    //List的迭代器类
    template<class T, class Ref, class Ptr>
    class ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T, Ref, Ptr> Self;
    public:
        ListIterator(PNode pNode = nullptr)
            :_pNode(pNode)
        {

        }
        ListIterator(const Self& l)
        {

            _pNode = l._pNode;
        }
        Ref operator*()
        {
            return _pNode->_val;
        }
        T* operator->()
        {
            return &(operator*());
        }
        Self& operator++()
        {
            _pNode = _pNode->_pNext;
            
            return *this;
        }
        Self operator++(int)
        {
            Self old = *this;
            _pNode = _pNode->_pNext;
            return old;
        }

        Self& operator--()
        {
            _pNode = _pNode->_pPre;
                return _pNode;
        }
        Self& operator--(int)
        {
            Self old = *this;
            _pNode = _pNode->_pPre;
            return old;
        }
        bool operator!=(const Self& l)
        {
            return _pNode != l._pNode;
        }
        bool operator==(const Self& l) //比较的是节点地址而不是节点内数据,因为比较节点内数据没意义,只有比较地址可以进行遍历的操作
        {
            return _pNode == l._pNode;
        }

        PNode _pNode;
    };


    //list类
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T*> const_iterator;
    public:
        // List的构造
        list()
        {
            CreateHead();
            
        }
        list(int n, const T& value = T())
        {
            CreateHead();
            for (int i = 0; i < n; i++)
            {
                push_back(value);
            }
            

        }
        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            CreateHead();
            while (first != last)
            {
                push_back(*first);
                first++;
                
            }

        }
        list(const list<T>& l)
        {
            CreateHead();
            for (auto e : l)
            {
                push_back(e);
            }

        }
        list<T>& operator=(const list<T> l)
        {
            CreateHead();
            list(l.begin(), l.end());
            return *this;

        }
        ~list()
        {
            clear();
            delete _pHead;
            _pHead = nullptr;
        }

        // List Iterator
        
        const_iterator begin() const
        { return const_iterator(_pHead->_pNext);}
        const_iterator end() const
        { return const_iterator(_pHead); }
        iterator begin() { return iterator(_pHead->_pNext); }
        iterator end() { return iterator(_pHead); }

    
        size_t size() const
        {
           size_t sum = 0;
            const_iterator it = begin();
            while (it != end())
            {
                it++;
                sum++;
            }
            return sum;    
        }
        bool empty()const
        {
            return _pHead->_pNext == _pHead->_pPre;
        }


        T& front()
        {
            return  _pHead->_pNext;
        }
        const T& front()const
        {
            return _pHead->_pNext;
        }
        T& back()
        {
            return  _pHead->_pPre;
        }
        const T& back()const
        {
            return _pHead->_pPre;

        }


        
        // List Modify
        void push_back(const T& val)
        { 
            insert(end(), val);
        }
        void pop_back()
        {      
            erase(end());
        }
        void push_front(const T& val) { 
            insert(begin(), val); 
        }
        void pop_front() 
        {
            erase(begin());
        }
        // 在pos位置前插入值为val的节点
        iterator insert(iterator pos, const T& val)
        {
            PNode newnode = new Node(val);
          
            PNode next = pos._pNode;
            PNode pre = next->_pPre;

            pre->_pNext = newnode;
            newnode->_pPre = pre;
            next->_pPre = newnode;
            newnode->_pNext = next;
     
            return iterator(newnode);
        }
        // 删除pos位置的节点,返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            PNode cur = pos._pNode->_pNext;
            PNode pre = pos._pNode->_pPre;

            cur->_pNext = pre;
            pre->_pPre = cur;

            pos._pNode->_pNext = nullptr;
            pos._pNode->_pPre = nullptr;
            delete pos._pNode;
           
              return iterator(cur);
        }
        void clear()
        {
            iterator it = begin();

            while (it != end())
            {
                it = erase(it);
            }
        }
        void swap(list<T>& l)
        {
            std::swap(_pHead, l._pHead);
        }
    private:
        void CreateHead()
        {
            PNode newhead = new Node;
            _pHead = newhead;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;

        }

        PNode _pHead;
 
    };
};
相关推荐
小米里的大麦3 分钟前
19 C、C++ 操作 MySQL
c语言·c++·mysql
青春逝如流水9 分钟前
Java 线程池的创建方式、注意要点与任务类型判断
java·开发语言·线程池·多线程·ai文章
徐小黑ACG10 分钟前
Golang goroutine、通道、锁
开发语言·后端·golang
开开心心_Every12 分钟前
内存清理工具定时自动清理开机自启动
java·开发语言·spring·计算机外设·ocr·intellij-idea·excel
有毒的教程16 分钟前
Dev-C++ 使用扩展库两种方式:DevPak一键安装 / 手动配置(重点)
android·c++·adb
纪念 22924 分钟前
c++ string(二)
开发语言·c++
李福春29 分钟前
斗破Google ADK Java苍穹-第5境斗灵-凝物通脉-Context与State
java·开发语言·人工智能·java-ee·google adk·深圳同盟·腾讯云架构师技术同盟
十年Java程序媛30 分钟前
Java 接口和抽象类对比|抛弃老旧八股,正确选型
java·开发语言·spring boot·后端
sanqima32 分钟前
MFC里的线程本地存储(TLS)与模块状态(Module State)
c++·mfc·tls·module state
见叶之秋32 分钟前
【C++】从基类到派生类:关于C++继承的务实梳理
开发语言·c++