栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

1.list常见接口使用

2.list模拟实现


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

//list迭代器
for (list::const_iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
}
for (list::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

  struct ListNode//类模板

  {

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

    ListNode* _pPre;

    ListNode* _pNext;

    T _val;

  };



  //List的迭代器类

  template

  class ListIterator

  {

    typedef ListNode* PNode;//pNode重命名

    typedef ListIterator 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 list

  {

    typedef ListNode Node;

    typedef Node* PNode;

  public:

    typedef ListIterator iterator;

    typedef ListIterator 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 

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

    list(const list& l)
    {
        CreateHead();
        // 用l中的元素构造临时的temp,然后与当前对象交换
        list temp(l.cbegin(), l.cend());
        this->swap(temp);
    }

    list& operator=(const list 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& l);

  private:

    void CreateHead();

    PNode _pHead;

  };

};


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604361.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号