当前位置:   article > 正文

C++从零开始的打怪升级之路(day31)

C++从零开始的打怪升级之路(day31)

这是关于一个普通双非本科大一学生的C++的学习记录贴

在此前,我学了一点点C语言还有简单的数据结构,如果有小伙伴想和我一起学习的,可以私信我交流分享学习资料

那么开启正题

今天分享的是关于list的模拟实现,今天只实现一部分基础函数

1.结点以及构造函数

  1. template<class T>
  2. struct __list_node
  3. {
  4. __list_node* _next;
  5. __list_node* _prev;
  6. T _data;
  7. __list_node(const T& x = T())
  8. :_data(x)
  9. ,_next(nullptr)
  10. ,_prev(nullptr)
  11. {}
  12. };
  13. class list
  14. {
  15. typedef __list_node<T> Node;
  16. public:
  17. list()
  18. {
  19. _head = new Node;
  20. _head->_next = _head;
  21. _head->_prev = _head;
  22. }
  23. private:
  24. Node* _head;
  25. };

list是一个双向带头循环的链表,所以在初始化时需要初始化哨兵位

2.尾插 

  1. void push_back(const T& x)
  2. {
  3. Node* tail = _head->_prev;
  4. Node* newnode = new Node(x);
  5. tail->_next = newnode;
  6. newnode->_next = _head;
  7. newnode->_prev = tail;
  8. _head->_prev = newnode;
  9. }

list的尾插比较简单,因为他的结构很完善

3.迭代器

  1. template<class T>
  2. struct __list_iterator
  3. {
  4. typedef __list_node<T> Node;
  5. Node* _node;
  6. __list_iterator(Node* node)
  7. :_node(node)
  8. {}
  9. T& operator*()
  10. {
  11. return _node->_data;
  12. }
  13. __list_iterator operator++()
  14. {
  15. _node = _node->_next;
  16. return *this;
  17. }
  18. bool operator!=(const __list_iterator<T>& it)
  19. {
  20. return _node != it._node;
  21. }
  22. };

因为迭代器要像指针一样去使用,所以我们必须对list的结点进行一些处理,而对于像vector的容器,他的原生指针就可以达到要求,我们没必要再打工干戈,但是对于list,我们就得动一些非常的手段

我们创造一个iterator的类,他存放结点的指针,再重载他的*   ++   --   ==   !=   等操作符,即可完成我们的要求 

  1. typedef __list_iterator<T> iterator;
  2. iterator begin()
  3. {
  4. return iterator(_head->_next);
  5. }
  6. iterator end()
  7. {
  8. return iterator(_head);
  9. }

当然,在begin()和end()函数调用时,就得用结点来创造迭代器再进行返回

 4.使用

  1. void test_list1()
  2. {
  3. list<int> l;
  4. l.push_back(1);
  5. l.push_back(2);
  6. l.push_back(3);
  7. l.push_back(4);
  8. l.push_back(5);
  9. list<int>::iterator it = l.begin();
  10. while (it != l.end())
  11. {
  12. cout << *it << " ";
  13. ++it;
  14. }
  15. cout << endl;
  16. }

将以上代码串起来,就可以简单模拟并验证list的实现了

下面是完整代码

  1. namespace wkl
  2. {
  3. template<class T>
  4. struct __list_node
  5. {
  6. __list_node* _next;
  7. __list_node* _prev;
  8. T _data;
  9. __list_node(const T& x = T())
  10. :_data(x)
  11. ,_next(nullptr)
  12. ,_prev(nullptr)
  13. {}
  14. };
  15. template<class T>
  16. struct __list_iterator
  17. {
  18. typedef __list_node<T> Node;
  19. Node* _node;
  20. __list_iterator(Node* node)
  21. :_node(node)
  22. {}
  23. T& operator*()
  24. {
  25. return _node->_data;
  26. }
  27. __list_iterator operator++()
  28. {
  29. _node = _node->_next;
  30. return *this;
  31. }
  32. bool operator!=(const __list_iterator<T>& it)
  33. {
  34. return _node != it._node;
  35. }
  36. };
  37. template<class T>
  38. class list
  39. {
  40. typedef __list_node<T> Node;
  41. public:
  42. typedef __list_iterator<T> iterator;
  43. iterator begin()
  44. {
  45. return iterator(_head->_next);
  46. }
  47. iterator end()
  48. {
  49. return iterator(_head);
  50. }
  51. list()
  52. {
  53. _head = new Node;
  54. _head->_next = _head;
  55. _head->_prev = _head;
  56. }
  57. void push_back(const T& x)
  58. {
  59. Node* tail = _head->_prev;
  60. Node* newnode = new Node(x);
  61. tail->_next = newnode;
  62. newnode->_next = _head;
  63. newnode->_prev = tail;
  64. _head->_prev = newnode;
  65. }
  66. private:
  67. Node* _head;
  68. };
  69. void test_list1()
  70. {
  71. list<int> l;
  72. l.push_back(1);
  73. l.push_back(2);
  74. l.push_back(3);
  75. l.push_back(4);
  76. l.push_back(5);
  77. list<int>::iterator it = l.begin();
  78. while (it != l.end())
  79. {
  80. cout << *it << " ";
  81. ++it;
  82. }
  83. cout << endl;
  84. }
  85. }
  86. int main()
  87. {
  88. wkl::test_list1();
  89. return 0;
  90. }

新手写博客,有不对的位置希望大佬们能够指出,也谢谢大家能看到这里,让我们一起学习进步吧!! 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AI算法构造者/article/detail/63118
推荐阅读
相关标签
  

闽ICP备14008679号