10.list常见接口及底层实现

目录

1.list常见接口使用

2.list模拟实现


1.list常见接口使用

//list构造
std::list<int> l1; // 构造空的l1
std::list<int> l2 (4,100); // l2中放4个值为100的元素
std::list<int> l3 (l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
std::list<int> l4 (l3); // 用l3拷贝构造l4
// 以数组为迭代器区间构造l5
int array[] = {16,2,77,29};
std::list<int> l5 (array, array + sizeof(array) / sizeof(int) );

//list迭代器
for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
}
for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it)//反向迭代器
  cout << *it << " ";

//modify
// 在list的尾部插入4,头部插入0
L.push_back(4);
L.push_front(0);
// 删除list尾部节点和头部节点
L.pop_back();
L.pop_front();
// 在pos前插入值为4的元素
L.insert(pos, 4);
// 在pos前插入5个值为5的元素
L.insert(pos, 5, 5);
// 删除pos位置上的元素
L.erase(pos);
// 删除list中[begin, end)区间中的元素,即删除list中的所有元素
L.erase(L.begin(), L.end());
// 交换l1和l2中的元素
l1.swap(l2);
// 将l2中的元素清空
l2.clear();

2.list模拟实现

namespace zy

{

  // List的节点类

  template<class T>

  struct ListNode//类模板

  {

    ListNode(const T& val = T())//构造函数
        : _pPre(nullptr)
        , _pNext(nullptr)
        , _val(val)
    {}

    ListNode<T>* _pPre;

    ListNode<T>* _pNext;

    T _val;

  };



  //List的迭代器类

  template<class T, class Ref, class Ptr>

  class ListIterator

  {

    typedef ListNode<T>* PNode;//pNode重命名

    typedef ListIterator<T, Ref, Ptr> Self;//类模板重命名

  public:

    ListIterator(PNode pNode = nullptr)//实例化pnode类构造list
        : _pNode(pNode)
    {}

    ListIterator(const Self& l)//拷贝构造
        : _pNode(l._pNode)
    {}

    T& operator*(){return _pNode->_val;}//重载*

    T* operator->(){return &(operator*());}

    Self& operator++()
    {
        _pNode = _pNode->_pNext;
        return *this;
    }

    Self operator++(int)
    {
        Self temp(*this);
        _pNode = _pNode->_pNext;
        return temp;
    }

    Self& operator--()
    {
        _pNode = _pNode->_pPre;
        return *this;
    }

    Self& operator--(int)
    {
        Self temp(*this);
        _pNode = _pNode->_pPre;
        return temp;
    }

    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;

  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();
        // 用l中的元素构造临时的temp,然后与当前对象交换
        list<T> temp(l.cbegin(), l.cend());
        this->swap(temp);
    }

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

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



    ///

    // List Iterator

    iterator begin(){return iterator(_pHead->_pNext);}

    iterator end(){return iterator(_pHead);}

    const_iterator begin(){return const_iterator(_pHead->_pNext);}

    const_iterator end(){return const_iterator(_pHead);}



    ///

    // List Capacity

    size_t size()const;

    bool empty()const;



    

    // List Access

    T& front();

    const T& front()const;

    T& back();

    const T& back()const;



    

    // List Modify

    void push_back(const T& val){insert(begin(), 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);

    // 删除pos位置的节点,返回该节点的下一个位置

    iterator erase(iterator pos)
    {
        // 找到待删除的节点
        PNode pDel = pos._pNode;
        PNode pRet = pDel->_pNext;
        // 将该节点从链表中拆下来并删除
        pDel->_pPre->_pNext = pDel->_pNext;
        pDel->_pNext->_pPre = pDel->_pPre;
        delete pDel;
        return iterator(pRet);
    }

    void clear();

    void swap(List<T>& l);

  private:

    void CreateHead();

    PNode _pHead;

  };

};


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学c的长弓狗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值