10.List common interfaces and underlying implementation

content

1. List common interface usage

2.List simulation implementation


1. List common interface usage

//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 simulation implementation

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;

  };

};


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324304195&siteId=291194637