list的模拟实现

文章目录


模拟实现

namespace mine

{

  // List的节点类

  template<class T>

  struct ListNode

  {

    ListNode<T>* _pPre;

    ListNode<T>* _pNext;

    T _val;

    ListNode(const T& val = T())

      :_pPre(nullptr)

      , _pNext(nullptr)

      , _val(val)

    {}

  };





  //List的迭代器类

  template<class T, class Ref, class Ptr>

  class ListIterator

  {
	//创建指针指向结点
    typedef ListNode<T>* PNode;
	//用于运算符++和--的重载的返回值
    typedef ListIterator<T, Ref, Ptr> Self;

  public:

    ListIterator(PNode pNode)

      :_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 this->_pNode != l._pNode;

    }

    bool operator==(const Self& l)

    {

      return this->_pNode == l._pNode;

    }

   

    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)

    {

      //提供一个空节点

      this->CreateHead();



      //在这个空节点后面依次插入l的所有节点

      for (auto& e : l)

      {

        this->push_back(e);

      }

    }

    list<T>& operator=(const list<T> l)

    {

      this->swap(l);

      return *this;

    }

    ~list()

    {

      this->clear();

      delete _pHead;

      _pHead = nullptr;

    }





    ///

    // List Iterator

    iterator begin()

    {

      return this->_pHead->_pNext;

    }

    iterator end()

    {

      return this->_pHead;

    }

    const_iterator begin() const

    {

      return this->_pHead->_pNext;

    }

    const_iterator end() const

    {

      return this->_pHead;

    }





    ///

    // List Capacity

    size_t size()const

    {

      return _size;

    }

    bool empty()const

    {

      return _size == 0;

    }





    

    // List Access

    T& front()

    {

      return this->_pHead->_pPre->_val;

    }

    const T& front()const

    {

      return _pHead->_pPre->_val;

    }

    T& back()

    {

      return this->_pHead->_pNext->_val;

    }

    const T& back()const

    {

      return this->_pHead->_pNext->_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的节点

    void insert(iterator pos, const T& val)

    {

      PNode cur = pos._pNode;

      PNode pre = pos._pNode->_pPre;

      PNode newnode = new Node(val);



      pre->_pNext = newnode;

      newnode->_pPre = pre;



      newnode->_pNext = cur;

      cur->_pPre = newnode;

       

      _size++;



       

       

    }

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

    iterator erase(iterator pos)

    {

      PNode cur = pos._pNode;



      PNode pre = cur->_pPre;

      PNode next = cur->_pNext;



      delete cur;



      pre->_pNext = next;

      next->_pPre = pre;



      _size--;



      return next;

    }

    void clear()

    {

      auto it = this->begin();

      while (it != this->end())

      {

        it=erase(it);

      }

    }

    void swap(list<T>& l)

    {

      std::swap(_pHead, l._pHead);

      std::swap(_size, l._size);

    }

  private:

    void CreateHead()

    {

      _pHead = new Node;

      _pHead->_pNext = _pHead;

      _pHead->_pPre = _pHead;

      _size = 0;

    }

    PNode _pHead;

    size_t _size;

  };

};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值