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;
 
    };
};
相关推荐
斐波娜娜18 分钟前
Maven详解
java·开发语言·maven
小码氓41 分钟前
Java填充Word模板
java·开发语言·spring·word
暮鹤筠1 小时前
[C语言初阶]操作符
c语言·开发语言
蜉蝣之翼❉3 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
Boilermaker19923 小时前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
aramae3 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周3 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
lixzest3 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
沉默媛4 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
_Chipen4 小时前
C++基础问题
开发语言·c++