list模拟实现C++

1.list介绍

2.默认成员函数

3.迭代器

4.增删查改

1.list介绍

       list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。

2.默认成员函数

        typedef ListNode<T> Node;
        typedef Node* PNode;
    private:
        Node* head;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T&> const_iterator;
    public:
        ///
        // List的构造
        void Initlist()
        {
            head = new Node;
            head->_pNext = head;
            head->_pPre = head;
            head->_val = 0;
        }

        list()
        {
            Initlist();
        }
        list(int n, const T& value = T())
        {
            Initlist();
            for (int i = 0; i < n; ++i)
                push_back(value);

        }
        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            Initlist();
            while (first != last)
            {
                push_back(*first);
                first++;
            }
        }
        list(const list<T>& l)
        {
            Initlist();
            for (auto e : l)
            {
                push_back(e);
            }
        }
        list<T>& operator=(const list<T> l)
        {
            for (auto it:l)
            {
                push_back(it);
            }
            return *this;
        }
        ~list()
        {
            clear();
            delete head;
            head = nullptr;
        }

3.迭代器

a.begin+end:返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器。

b.rbegin+rend:返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置。

迭代器失效:迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代 器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

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;
        }
        T& operator*()
        {
            return _pNode->_val;
        }
        T* operator->()
        {
            return &_pNode->_val;
        }
        Self& operator++()
        {
            _pNode = _pNode->_pNext;
            return *this;
        }
        Self operator++(int)
        {
            Self tmp(this);
            _pNode = _pNode->_pNext;
            return tmp;
        }
        Self& operator--()
        {
            _pNode = _pNode->_pPre;
            return *this;
        }
        Self& operator--(int)
        {
            Self tmp(this);
            _pNode = _pNode->_pPre;
            return tmp;
        }
        bool operator!=(const Self& l)
        {
            return _pNode!=l._pNode;
        }
        bool operator==(const Self& l)
        {
            return _pNode == l._pNode;
        }
    private:
        PNode _pNode;
    };
///
        // List Iterator
        iterator begin()
        {
            return head->_pNext;
        }
        iterator end()
        {
            return head;
        }
        const_iterator begin()
        {
            return head->_pNext;
        }
        const_iterator end()
        {
            return head;
        }

5.增删查改

a.empty:检测list是否为空,是返回true,否则返回false。

b.size:返回list中有效节点的个数。

c.front:返回list的第一个节点中值的引用。

d.back:返回list的最后一个节点中值的引用。

e.push_front:在list首元素前插入值为val的元素。

f.pop_back:删除list中第一个元素。

g.push_back::在list尾部插入值为val的元素。

h.pop_back: 删除list中最后一个元素。

i.insert: 在list position 位置中插入值为val的元素。

j.erase: 删除list position位置的元素。

k.swap: 交换两个list中的元素。

l.clear: 清空list中的有效元素。

///
        // List Capacity
        size_t size()const
        {
            size_t i = 0;
            auto it = begin();
            while (it!= end())
            {
                i++;
                it++;
            }
        }
        bool empty()const
        {
            return size() == 0;
        }


        
        // List 


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

        
        // 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)
        {
            Node* newnode = new Node(val);
            Node* cur = pos._pNode;
            cur->_pPre->_pNext = newnode;
            newnode->_pPre = cur->_pPre;
            newnode->_pNext = cur;
            cur->_pPre = newnode;
            return (iterator)newnode;
            //return newnode;
        }
        // 删除pos位置的节点,返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            Node* cur = pos._pNode;
            Node* next = cur->_next;
            cur->_pPre->_pNext = cur->_pNext;
            cur->_pNext->_pPre = cur->_pPre;
            delete cur;
            return  (iterator)next;
        }
        void clear()
        {
            auto it = begin();
            while (it != end())
            {
                erase(it);
            }
        }
        void swap(list<T>& l)
        {
            std::swap(head, l.head);
        }

完整代码如下:

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


    //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;
        }
        T& operator*()
        {
            return _pNode->_val;
        }
        T* operator->()
        {
            return &_pNode->_val;
        }
        Self& operator++()
        {
            _pNode = _pNode->_pNext;
            return *this;
        }
        Self operator++(int)
        {
            Self tmp(this);
            _pNode = _pNode->_pNext;
            return tmp;
        }
        Self& operator--()
        {
            _pNode = _pNode->_pPre;
            return *this;
        }
        Self& operator--(int)
        {
            Self tmp(this);
            _pNode = _pNode->_pPre;
            return tmp;
        }
        bool operator!=(const Self& l)
        {
            return _pNode!=l._pNode;
        }
        bool operator==(const Self& l)
        {
            return _pNode == l._pNode;
        }
    private:
        PNode _pNode;
    };
    


    //list类
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    private:
        Node* head;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T&> const_iterator;
    public:
        ///
        // List的构造
        void Initlist()
        {
            head = new Node;
            head->_pNext = head;
            head->_pPre = head;
            head->_val = 0;
        }

        list()
        {
            Initlist();
        }
        list(int n, const T& value = T())
        {
            Initlist();
            for (int i = 0; i < n; ++i)
                push_back(value);

        }
        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            Initlist();
            while (first != last)
            {
                push_back(*first);
                first++;
            }
        }
        list(const list<T>& l)
        {
            Initlist();
            for (auto e : l)
            {
                push_back(e);
            }
        }
        list<T>& operator=(const list<T> l)
        {
            for (auto it:l)
            {
                push_back(it);
            }
            return *this;
        }
        ~list()
        {
            clear();
            delete head;
            head = nullptr;
        }


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


        ///
        // List Capacity
        size_t size()const
        {
            size_t i = 0;
            auto it = begin();
            while (it!= end())
            {
                i++;
                it++;
            }
        }
        bool empty()const
        {
            return size() == 0;
        }


        
        // List 


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

        
        // 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)
        {
            Node* newnode = new Node(val);
            Node* cur = pos._pNode;
            cur->_pPre->_pNext = newnode;
            newnode->_pPre = cur->_pPre;
            newnode->_pNext = cur;
            cur->_pPre = newnode;
            return (iterator)newnode;
            //return newnode;
        }
        // 删除pos位置的节点,返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            Node* cur = pos._pNode;
            Node* next = cur->_next;
            cur->_pPre->_pNext = cur->_pNext;
            cur->_pNext->_pPre = cur->_pPre;
            delete cur;
            return  (iterator)next;
        }
        void clear()
        {
            auto it = begin();
            while (it != end())
            {
                erase(it);
            }
        }
        void swap(list<T>& l)
        {
            std::swap(head, l.head);
        }
    private:
        PNode _pHead;
    };
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sakura&NANA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值