C++STL(list类)


1.list类的介绍

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

2.list的基本用法

2.1 基本用法

在这里插入图片描述
前面的用法大多都和string和vector差不都,这些用法就不再赘述!

2.2 迭代器失效

在这里插入图片描述

2.3 reverse(逆置)

在这里插入图片描述

2.3 sort(排序)

在这里插入图片描述

2.4 unique(去重)

在这里插入图片描述

2.5 splice(转移)

在这里插入图片描述

3.list的底层(模拟实现)

3.1 list的++

大致思路还是和之前一样的,但是List这里有需要专门说明的地方!
因为list的存储空间不像vector一样是连续的,链表吗,就是一个地址连一个地址。
所以它不能像vector一样随便的++和+n这样操作。
所以我们需要typedef一个模板,来重载我们的运算符!

 template<class T>
    struct ListIterator
    {
        typedef ListNode<T> Node;
        typedef ListIterator<T> Self;
        Node* _node;

        ListIterator(Node* node)
            :_node(node)
        {}

        // *it
        T& operator*()
        {
            return _node->_data;
        }

        // it->
        T* operator->()
        {
            return &_node->_data;
        }

        // ++it前置++
        Self& operator++()
        {
            _node = _node->_next;
            return *this;
        }
        //it++后置++
        Self operator++(int)
        {
            Self tmp(*this);
            _node = _node->_next;
            return tmp;
        }
        //同样的还有前置--
        Self& operator--()
        {
            _node = _node->_prev;
            return *this;
        }
        //后置--
        Self operator--(int)
        {
            Self tmp(*this);
            _node = _node->_prev;
            return tmp;
        }

        bool operator!=(const Self& it)
        {
            return _node != it._node;
        }

        bool operator==(const Self& it)
        {
            return _node == it._node;
        }
    };

3.2 修改链表问题

在这里插入图片描述
在这里插入图片描述

但是这样分别写两个ListIterator和ListConstIterator就非常的浪费,那么有没有什么办法合二为一呢?

添加类模板参数!
在这里插入图片描述

3.3 完整代码

mylist.h:

#pragma once
#include<iostream>
using namespace std;

namespace my
{
    // List的节点类
    template<class T>
    struct ListNode
    {
        ListNode<T>* _pPre;
        ListNode<T>* _pNext;
        T _val;

        ListNode(const T& val = T())
            :_pNext(nullptr)
            , _pPre(nullptr)
            , _val(val)
        {}
    };

    //List的迭代器类
    template<class T, class Ref, class Ptr>
    class ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T, Ref, Ptr> Self;

    public:
        PNode _pNode;

    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) const
        {
            return _pNode != l._pNode;
        }
        bool operator==(const Self& l) const
        {
            return _pNode == l._pNode;
        }
    };

    //list类
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    private:
        void CreateHead()
        {
            // 创建头节点
            _pHead = new Node;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
            _size = 0;
        }
        PNode _pHead;
        size_t _size;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T&> const_iterator;
    public:
        ///
        // List的构造
        void empty_init()
        {
            _pHead = new Node;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
            _size = 0;
        }
        list()
        {
            empty_init();
        }

        list(int n, const T& value = T())
        {
            // 创建包含 n 个值为 value 的节点的链表
            empty_init();
            for (int i = 0; i < n; ++i) {
                push_back(value);
            }
        }

        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            // 通过迭代器范围 [first, last) 创建链表
            empty_init();
            while (first != last) {
                push_back(*first);
                ++first;
            }
        }

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

        list<T>& operator=(const list<T>& l)
        {
            if (this != &l) {
                list<T> tmp(l);
                swap(tmp);
            }
            return *this;
        }

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

        ///
        // List Iterator
        iterator begin()
        {
            return _pHead->_pNext;
        }
        iterator end()
        {
            return _pHead;
        }
        const_iterator begin() const
        {
            return _pHead->_pNext;
        }
        const_iterator end() const
        {
            return _pHead;
        }
        ///
        // List Capacity
        size_t size()const
        {
            return _size;
        }
        bool empty()const
        {
            return _size == 0;
        }

        
        // List Access
        T& front()
        {
            // 返回链表的第一个元素
            return _pHead->_pNext->_val;
        }
        const T& front()const
        {
            // 返回链表的第一个元素(常量版本)
            return _pHead->_pNext->_val;
        }
        T& back()
        {
            // 返回链表的最后一个元素
            return _pHead->_pPre->_val;
        }
        const T& back()const
        {
            // 返回链表的最后一个元素
            return _pHead->_pPre->_val;
        }

        
        // List Modify

        /*void push_back(const T& x)
        {
            Node* newnode = new Node(x);
            Node* tail = _head->_prev;

            tail->_next = newnode;
            newnode->_prev = tail;
            newnode->_next = _head;
            _head->_prev = newnode;
        }*/


        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* cur = pos._pNode;
            Node* newnode = new Node(val);
            Node* prev = cur->_pPre;

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

            prev->_pNext = next;
            next->_pPre = prev;
            delete cur;
            _size--;

            return iterator(next);
        }

        void clear()
        {
            iterator l = begin();
            while (l != end())
            {
                l = erase(l);
            }
        }
        void swap(list<T>& l)
        {
            std::swap(_pHead, l._pHead);
            std::swap(_size, l._size);
        }
    };

    void test_list1()
    {
        my::list<int> lt;
        lt.push_back(1);
        lt.push_back(2);
        lt.push_back(3);
        lt.push_back(4);
        lt.push_back(5);

        // 使用迭代器遍历链表并修改元素值
        for (auto it = lt.begin(); it != lt.end(); ++it)
        {
            *it += 10; // 通过迭代器访问元素值
            cout << *it << " ";
        }
        cout << endl;

        lt.push_front(10);
        lt.push_front(20);
        lt.push_front(30);

        // 使用范围-based for 循环遍历链表
        for (auto& e : lt)
        {
            cout << e << " ";
        }
        cout << endl;

        lt.pop_back();
        lt.pop_back();
        lt.pop_front();
        lt.pop_front();

        // 再次使用范围-based for 循环遍历链表
        for (auto& e : lt)
        {
            cout << e << " ";
        }
        cout << endl;
    }
};

main.cpp:

#define _CRT_SECURE_NO_WARNINGS 1
#include"mylist.h"

int main()
{
	my::test_list1();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Chris·Bosh

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

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

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

打赏作者

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

抵扣说明:

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

余额充值