文章目录
- list的模拟实现
- list的迭代器
- begin()和end()
list的模拟实现
#pragma once
#include<iostream>
#include<list>using namespace std;namespace wbc
{// 类模版template<class T>struct list_node // 链表的节点{T _data;list_node<T>* _next;list_node<T>* _prev;// 默认构造list_node(const T& val = T()):_data(val),_next(nullptr),_prev(nullptr){}};template<class T>struct list_iterator{typedef list_node<T> Node;typedef list_iterator<T> Self;Node* _node;// 构造list_iterator(Node* node):_node(node){}T& operator*(){return _node->_data;}Self& operator++(){_node = _node->_next;return *this;}Self& operator--(){_node = _node->_prev;return *this;}bool operator!=(const Self& s) const {return _node != s._node;}bool operator==(const Self& s) const {return _node == s._node;}};template<class T>class list // 链表的结构{typedef list_node<T> Node;public:typedef list_iterator<T> iterator;iterator begin(){// 有名对象/*iterator it(_head->_next);return it;*/// 匿名对象//return iterator(_head->_next);return _head->_next;// 单参数的构造函数支持隐式类型的转化}// end是最后一个位置的下一个iterator end(){return _head;}// 构造list(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}// 头插void push_front(const T& x){insert(begin(), x);}// 尾插void push_back(const T& x){/*Node* newnode = new Node(x);Node* tail = _head->_prev;tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;++_size;*/insert(end(), x);}// 插入,在pos之前插入一个xvoid insert(iterator pos,const T& x){Node* cur = pos._node;Node* prev = cur->_prev;// prev newnode curNode* newnode = new Node(x);prev->_next = newnode;cur->_prev = newnode;newnode->_next = cur;newnode->_prev = prev;++_size;}// 删除void erase(iterator pos){// 不能删除哨兵位的头节点assert(pos != end());Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;prev->_next = next;next->_prev = prev;delete cur;--_size;}// 尾删void pop_back(){erase(--end());}// 头删void pop_front(){erase(begin());}size_t size() const{return _size;}bool empty() const{return _size == 0;}private:Node* _head;size_t _size;};void test_list1(){list<int> ls;ls.push_back(1);ls.push_back(2);ls.push_back(3);ls.push_back(4);ls.push_back(5);list<int>::iterator it = ls.begin();while (it != ls.end()){cout << *it << endl;++it;}cout << endl;for (auto ch : ls){cout << ch << " ";}cout << endl;}
}
list的迭代器
1.list迭代器重载的operator++不是原生指针,不能直接加到下一个位置,vector,string的迭代器是原生指针可以直接加到下一个位置
2.list的operator* 也需要重载不然* 之后是一个节点,++不能到下一个位置,实现的operator *解引用拿到的节点里的值
template<class T>struct list_iterator{typedef list_node<T> Node;typedef list_iterator<T> Self;Node* _node;// 构造list_iterator(Node* node):_node(node){}T& operator*(){return _node->_data;}Self& operator++(){_node = _node->_next;return *this;}Self& operator--(){_node = _node->_prev;return *this;}bool operator!=(const Self& s) const {return _node != s._node;}bool operator==(const Self& s) const {return _node == s._node;}};
begin()和end()
iterator begin(){// 有名对象/*iterator it(_head->_next);return it;*/// 匿名对象//return iterator(_head->_next);// 返回的是哨兵位节点的下一个位置return _head->_next;// 单参数的构造函数支持隐式类型的转化}// end是最后一个位置的下一个iterator end(){return _head;}