STL LIST实现


struct _List_node_base {
  _List_node_base* _M_next;
  _List_node_base* _M_prev;
};  //定义一个list的node的base class,包含一个指向前node和后node的pointer!

template <class _Tp>
struct _List_node : public _List_node_base {
  _Tp _M_data;
};  //泛化node中包含的数据.

struct _List_iterator_base {
  typedef size_t                     size_type;
  typedef ptrdiff_t                  difference_type;
  typedef bidirectional_iterator_tag iterator_category;  //iterator的特征
  _List_node_base* _M_node;  //通过包含node pointer来完成iterator模仿pointer

  _List_iterator_base(_List_node_base* __x) : _M_node(__x) {}  
  _List_iterator_base() {}

  void _M_incr() { _M_node = _M_node->_M_next; }  //模仿pointer的前进
  void _M_decr() { _M_node = _M_node->_M_prev; }  //模仿pointer的后退

  bool operator==(const _List_iterator_base& __x) const {
    return _M_node == __x._M_node;
  }
  bool operator!=(const _List_iterator_base& __x) const {
    return _M_node != __x._M_node;
  }
}; 

template<class _Tp, class _Ref, class _Ptr>
struct _List_iterator : public _List_iterator_base {
  typedef _List_iterator<_Tp,_Tp&,_Tp*>             iterator;
  typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
  typedef _List_iterator<_Tp,_Ref,_Ptr>             _Self;

  typedef _Tp value_type;
  typedef _Ptr pointer;
  typedef _Ref reference;
  typedef _List_node<_Tp> _Node;

  _List_iterator(_Node* __x) : _List_iterator_base(__x) {}
  _List_iterator() {}
  _List_iterator(const iterator& __x) : _List_iterator_base(__x._M_node) {}

  reference operator*() const { return ((_Node*) _M_node)->_M_data; }

  pointer operator->() const { return &(operator*()); }

  _Self& operator++() {
    this->_M_incr();
    return *this;
  }
  _Self operator++(int) {
    _Self __tmp = *this;
    this->_M_incr();
    return __tmp;
  }
  _Self& operator--() {
    this->_M_decr();
    return *this;
  }
  _Self operator--(int) {
    _Self __tmp = *this;
    this->_M_decr();
    return __tmp;
  }
};
inline bidirectional_iterator_tag
iterator_category(const _List_iterator_base&)
{
  return bidirectional_iterator_tag();
}

template <class _Tp, class _Ref, class _Ptr>
inline _Tp*
value_type(const _List_iterator<_Tp, _Ref, _Ptr>&)
{
  return 0;
}

inline ptrdiff_t*
distance_type(const _List_iterator_base&)
{
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++的STL(Standard Template Library)中,`list`是一个双向链表(doubly linked list)的容器类。它提供了一组用于操作链表的方法和算法。 要使用`list`,需要包含头文件`<list>`,然后使用`std::list`来定义一个链表对象。例如: ```cpp #include <list> std::list<int> myList; // 定义一个存储整数的链表 ``` `list`类提供了一系列的方法,可以在链表中进行插入、删除、访问等操作。下面是一些常用的方法示例: - `push_back(value)`: 在链表末尾插入一个元素。 - `push_front(value)`: 在链表头部插入一个元素。 - `pop_back()`: 删除链表末尾的元素。 - `pop_front()`: 删除链表头部的元素。 - `front()`: 返回链表头部的元素。 - `back()`: 返回链表末尾的元素。 - `insert(iterator, value)`: 在指定位置插入一个元素。 - `erase(iterator)`: 删除指定位置的元素。 除了上述方法之外,`list`还提供了其他一些方法和迭代器,可以方便地遍历、查找、排序等操作。 以下是一个示例代码,展示了如何使用`list`进行插入、删除和遍历操作: ```cpp #include <iostream> #include <list> int main() { std::list<int> myList; myList.push_back(1); myList.push_back(2); myList.push_back(3); std::cout << "List elements: "; for (auto it = myList.begin(); it != myList.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; myList.pop_front(); std::cout << "List elements after pop_front(): "; for (auto it = myList.begin(); it != myList.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; } ``` 这是`list`的基本用法,你可以根据需要选择适合的方法来操作链表。希望能帮到你!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值