0


【STL_list 模拟】——打造属于自己的高效链表容器

一、list节点

​ list是一个双向循环带头的链表,所以链表节点结构如下:

  1. template<classT>structListNode{
  2. T val;
  3. ListNode* next;
  4. ListNode* prve;ListNode(int x){
  5. val = x;
  6. next = prve =this;}};

二、list迭代器

2.1、list迭代器与vector迭代器区别

​ list迭代器与vector迭代器不一样,不能使用简单的原生指针了;

vector中迭代器可以使用原生指针,因为vector的存储空间是连续的,可以通过指针+/-/++/–找到下一个(上一个)位置;而list(链表)我们知道存储空间是不连续的,不能通过指针++/—找到下一个(上一个)节点;那我们就不能使用原生指针。

2.2、list迭代器实现

​ 既然原生指针不能满足我们的需求,那我们就要用其他的方法来实现迭代器,这时候类的封装的意义就体现出来了;我们只需要对原生指针进行封装,然后使用运算符重载来修改迭代器++/–这些行为,这样就可以满足我们的需求了。

具体代码如下:

  1. template<classT,classRef,classStr>classlist_iterator{typedef ListNode Node;typedef list_iterator iterator;public:list_iterator(Node* node){
  2. _node = node;}//前置++
  3. iterator operator++(){
  4. Node tmp(_node);
  5. _node = _node->_next;return tmp;}//后置++
  6. iterator operator++(int){
  7. _node = _node->_next;return*this;}//前置--
  8. iterator operator--(){
  9. Node tmp(_node);
  10. _node = _node->_prve;return tmp;}
  11. iterator operator++(int){
  12. _node = _node->_prve;return*this;}booloperator==(const iterator& it){return _node == it._node;}booloperator!=(const iterator& it){return _node != it._node;}
  13. Ref operator*(){return _node->val;}
  14. Ptr operator->(){return&(_node->_val);}private:
  15. Node* _node;};

三、list实现

3.1、构造函数

在这里插入图片描述

先看一下list构造函数的接口
构造函数接口说明**list()默认构造函数,构造一个空的listlist (size_type n, const value_type& val =value_type())用n个val值构造listlist (InputIterator first, InputIterator last)还有一段迭代器区间初始化list (const list& x)**拷贝构造函数
​ 这里我们实现的list是带头双向循环链表,具体结构与之前的双向链表相同。

在构造之前,我们需要先构造一个头结点,这里就写一个函数 empty_init 来构造这个头结点。

  1. empty_init(){
  2. _head =newNode();
  3. _head->_next = _head;
  4. _head->_prve = _head;}

默认构造函数

  1. list(){empty_init();
  2. _size =0;}

用n个val值构造

  1. list(size_t n,const T& val){/*_head = new Node();
  2. _head->_next = _head;
  3. _head->_prve = _head;*/empty_init();for(size_t i =0; i < n; i++){
  4. Node* newnode =newNode(val);
  5. newnode->_next = _head->_next;
  6. newnode->_prve = _head->_next;
  7. _head->_next->_prve = newnode;
  8. _head->_next = newnode;}
  9. _size = n;}

迭代器区间构造

​ 使用迭代器区间构造,这里复用一下后面的push_back直接尾插来构造。

  1. template<classInputIterator>list(InputIterator first, InputIterator last){empty_init();
  2. _size =0;while(first != last){push_back(*first);
  3. _size++;}}voidpush_back(const T& val){
  4. Node* newnode =newNode(val);
  5. newnode->_next = _head;
  6. newnode->_prve = _head->_prve;
  7. _head->_prve->_next = newnode;
  8. _head->_prve = newnode;}

拷贝构造函数

​ 拷贝构造,这里直接复用上面迭代器构造即可。

  1. list(const list& l){empty_init();list(l.begin(), l.end());
  2. _size = l.size();}

3.2、迭代器

  1. //iterator
  2. iterator end(){//return _head->_next;returniterator(_head);}
  3. iterator begin(){//return _head;returniterator(_head->_next);}
  4. const_iterator end()const{//return _head->_next;returnconst_iterator(_head);}
  5. const_iterator begin()const{//return _head;returnconst_iterator(_head->_next);}

​ 这里迭代器返回值,可以直接返回节点指针(因为单参数的构造函数支持隐式类型转换)。

3.3、Capacity

在这里插入图片描述
​ 主要就是empty和size(判断空和数据个数)

  1. //Capacityboolempty(){return _head == _head->_next;}
  2. size_t size(){/*size_t ret = 0;
  3. Node* ptail = _head->_next;
  4. while (ptail != head)
  5. {
  6. ret++;
  7. ptail = ptail->_next;
  8. }
  9. return ret;*/return _size;}

​ 这里遍历链表来计算数据个数,代价太大了,我们直接写一个成员变量,在初始化,插入和删除时修改,会方便很多。

3.4、增删查改

在这里插入图片描述

​ 这里增删查改就实现其中的一部分,其他的不太常用。

3.4.1、push_back、push_front、pop_back、pop_front

​ 头插尾插,头删尾删,对于list而言,只需要修改指针的指向;

  1. voidpush_back(const T& val){
  2. Node* newnode =newNode(val);
  3. newnode->_next = _head;
  4. newnode->_prve = _head->_prve;
  5. _head->_prve->_next = newnode;
  6. _head->_prve = newnode;
  7. _size++;}voidpush_front(const T& val){
  8. Node* newnode =newNode(val);
  9. newnode->_next = _head->_next;
  10. newnode->_prve = _head;
  11. _head->_next->_prve = newnode;
  12. _head->_next = newnode;
  13. _size++;}//删voidpop_back(){if(!empty()){
  14. Node* del = _head->_prve;
  15. _head->_prve->_prve->_next = _head;
  16. _head->_prve = _head->_prve->_prve;delete del;
  17. _size--;}}voidpop_front(){if(!empty()){
  18. Node* del = _head->_next;
  19. _head->_next->_next->_prve = _head;
  20. _head->_next = _head->_next->_next;delete del;
  21. _size--;}}

3.4.2、insert

在这里插入图片描述

​ insert在指定位置插入数据,看它的参数列表,大概就知道,要在迭代器position 迭代器位置(之前)插入数据(1个或者n个),或者插入一段迭代器区间的数据。

  1. //insertvoidinsert(iterator pos,const T& val){
  2. Node* newnode =newNode(val);
  3. Node* tmp = pos._node;
  4. newnode->_next = tmp;
  5. newnode->_prve = tmp->_prve;
  6. tmp->_prve->_next = newnode;
  7. tmp->_prve = newnode;++_size;}voidinsert(iterator pos, size_t n,const T& val){for(size_t i =0; i < n; i++){
  8. Node* newnode =newNode(val);
  9. Node* tmp = pos._node;
  10. newnode->_next = tmp;
  11. newnode->_prve = tmp->_prve;
  12. tmp->_prve->_next = newnode;
  13. tmp->_prve = newnode;}
  14. _size+=n;}voidinsert(iterator pos,int n,const T& val){for(size_t i =0; i < n; i++){
  15. Node* newnode =newNode(val);
  16. Node* tmp = pos._node;
  17. newnode->_next = tmp;
  18. newnode->_prve = tmp->_prve;
  19. tmp->_prve->_next = newnode;
  20. tmp->_prve = newnode;}}

这里需要注意:

​ 看到这里可能会疑惑,为什么实现了两个插入n个数据 的insert函数;

因为,当数据类型是int时,下面模版实例化出的函数(iterator , int , int)比这里实现的(iterator , size_t , int)更加匹配;编译器就会去调用更加匹配的函数,就会出错。这里就是为了防止这种出错。

​ 插入迭代器的数据,与使用迭代器区间构造初始化相似,list只需改变指针指向即可。

  1. template<classInputIterator>voidinsert(iterator pos, InputIterator first, InputIterator last){while(first != last){
  2. Node* newnode =newNode(*first);
  3. Node* tmp = pos._node;
  4. newnode->_next = tmp;
  5. newnode->_prve = tmp->_prve;
  6. tmp->_prve->_next = newnode;
  7. tmp->_prve = newnode;++first;}}

3.4.3、erase

​ erase删除一个节点,我们要修改前后节点的指针指向。

  1. iterator erase(iterator pos){assert(pos !=end());
  2. Node* del = pos._node;
  3. Node* next = del->_next;
  4. Node* prve = del->_prve;
  5. next->_prve = prve;
  6. prve->_next = next;delete del;return next;}

​ 在我们删除节点之后,之前的迭代器就会失效,为了解决迭代器失效问题,我们就使用返回值,返回新的迭代器。

3.4.4、swap

​ 这里实现的list只有一个成员函数(头结点的指针),直接交换即可。

  1. voidswap(list& l){
  2. std::swap(_head, l._head);}

3.4.5、clear

​ clear函数,清理数据,(保留头结点)。

  1. //清除数据voidclear(){
  2. iterator it =begin();while(it !=end()){
  3. it =erase(it);}}

3.5、析构函数

​ 析构函数相对就比较简单了,我们清理完数据,再释放头结点即可。

  1. ~list(){clear();delete _head;}

到这里,list的模拟实现就完成了;这里只是实现了其中的一部分内容,感兴趣的可以继续深入了解学习。

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2oul0hvapjsws
ear函数,清理数据,(保留头结点)。

  1. //清除数据voidclear(){
  2. iterator it =begin();while(it !=end()){
  3. it =erase(it);}}

3.5、析构函数

​ 析构函数相对就比较简单了,我们清理完数据,再释放头结点即可。

  1. ~list(){clear();delete _head;}

到这里,list的模拟实现就完成了;这里只是实现了其中的一部分内容,感兴趣的可以继续深入了解学习。

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2oul0hvapjsws

标签: c++ list 链表

本文转载自: https://blog.csdn.net/LH__1314/article/details/143454671
版权归原作者 努力学习的小廉 所有, 如有侵权,请联系我们删除。

“【STL_list 模拟】——打造属于自己的高效链表容器”的评论:

还没有评论