From 0e64ffce94a2acd9d954f2fe8f35224c96c53abb Mon Sep 17 00:00:00 2001 From: ANY_10 <2810747109@qq.com> Date: Mon, 14 Apr 2025 01:57:02 +0000 Subject: [PATCH] =?UTF-8?q?update=2025=E5=B9=B4-04=E6=9C=88-02=E6=97=A5--l?= =?UTF-8?q?ist/List.h.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ANY_10 <2810747109@qq.com> --- .../List.h" | 437 +++++++----------- 1 file changed, 168 insertions(+), 269 deletions(-) diff --git "a/25\345\271\264-04\346\234\210-02\346\227\245--list/List.h" "b/25\345\271\264-04\346\234\210-02\346\227\245--list/List.h" index b4ac282..61ed5d1 100644 --- "a/25\345\271\264-04\346\234\210-02\346\227\245--list/List.h" +++ "b/25\345\271\264-04\346\234\210-02\346\227\245--list/List.h" @@ -1,197 +1,228 @@ #pragma once #include - -namespace bit +#include +using namespace std; +//实现list +//1,创建节点 +//2,构造链表,实现尾插 +//3,实现迭代器 +//4,完善链表功能,实现insert等 +//5,拷贝构造,赋值 +//6,const迭代器。方法一:拷贝一份迭代器修改为const。方法二:加一个模版参数。 +namespace A { + + //类型不确定,写为模版 + //使用struct不受到访问限定符的限制,如private template struct list_node { + //指向前,后一个结点指针,数据 list_node* _next; list_node* _prev; T _data; list_node(const T& x = T()) :_next(nullptr) - ,_prev(nullptr) - ,_data(x) - {} + , _prev(nullptr) + , _data(x) + { } }; - // typedef list_iterator iterator; - // typedef list_iterator const_iterator; - template + //迭代器 + template struct list_iterator { + //节点被频繁访问需要重命名一下 typedef list_node Node; typedef list_iterator Self; + Node* _node; list_iterator(Node* node) :_node(node) - {} + { } + ////添加的模板构造函数,允许从其他Ref和Ptr的迭代器构造 + ////可以不加,但是只能在传参数时使用const_iterator,加上可以直接使用 + //template + //list_iterator(const list_iterator& other) + // : _node(other._node) + //{} + + //重载*,能够修改返回T& Ref operator*() { return _node->_data; } + //结构体访问-> Ptr operator->() { return &_node->_data; } + //重载前置++,返回迭代器引用,能修改 Self& operator++() { _node = _node->_next; return *this; } - + //重载后置++ Self operator++(int) { Self tmp(*this); _node = _node->_next; - return tmp; } - + //重载前置--,返回迭代器引用,能修改 Self& operator--() { _node = _node->_prev; return *this; } - + //重载后置-- Self operator--(int) { Self tmp(*this); _node = _node->_prev; - return tmp; } + //比较节点是否不相等 + //不改变内容,可以加const bool operator!=(const Self& s) const { return _node != s._node; } - bool operator==(const Self& s) const { return _node == s._node; } }; - /*template - struct list_const_iterator - { - typedef list_node Node; - typedef list_const_iterator Self; - Node* _node; - - list_const_iterator(Node* node) - :_node(node) - {} - - const T& operator*() - { - return _node->_data; - } - - Self& operator++() - { - _node = _node->_next; - return *this; - } - - Self operator++(int) - { - Self tmp(*this); - _node = _node->_next; - - return tmp; - } - - Self& operator--() - { - _node = _node->_prev; - return *this; - } - - Self operator--(int) - { - Self tmp(*this); - _node = _node->_prev; - - return tmp; - } - - bool operator!=(const Self& s) const - { - return _node != s._node; - } - - bool operator==(const Self& s) const - { - return _node == s._node; - } - };*/ + ////const迭代器 + //template + //struct list_const_iterator + //{ + // //节点被频繁访问需要重命名一下 + // typedef list_node Node; + // typedef list_const_iterator Self; + + // Node* _node; + + // list_const_iterator(Node* node) + // :_node(node) + // { } + + // //重载*,能够修改返回T& + // const T& operator*() + // { + // return _node->_data; + // } + + // //重载前置++,返回迭代器引用,能修改 + // Self& operator++() + // { + // _node = _node->_next; + // return *this; + // } + // //重载后置++ + // Self operator++(int) + // { + // Self tmp(*this); + // _node = _node->_next; + // return tmp; + // } + // //重载前置--,返回迭代器引用,能修改 + // Self& operator--() + // { + // _node = _node->_prev; + // return *this; + // } + // //重载后置-- + // Self operator--(int) + // { + // Self tmp(*this); + // _node = _node->_prev; + // return tmp; + // } + + // //比较节点是否不相等 + // //不改变内容,可以加const + // bool operator!=(const Self& s) const + // { + // return _node != s._node; + // } + // bool operator==(const Self& s) const + // { + // return _node == s._node; + // } + //}; template class list { + //类型不忘写,list_node类名不能代替类型 typedef list_node Node; public: + + //迭代器 typedef list_iterator iterator; //typedef list_const_iterator const_iterator; typedef list_iterator const_iterator; + iterator begin() { - return iterator(_head->_next); + //构造匿名对象返回 + //注意_head是哨兵位,_head->_next才是头结点 + return _head->_next; } - iterator end() { - return iterator(_head); + //构造匿名对象返回 + //注意_head是哨兵位,也是尾节点的next节点,符合end()是尾节点的下一个结点 + return _head; } const_iterator begin() const { - return const_iterator(_head->_next); + //构造匿名对象返回 + //注意_head是哨兵位,_head->_next才是头结点 + return _head->_next; } - const_iterator end() const { - return const_iterator(_head); + //构造匿名对象返回 + //注意_head是哨兵位,也是尾节点的next节点,符合end()是尾节点的下一个结点 + return _head; } + //空初始化 void empty_init() { _head = new Node; - _head->_next = _head; _head->_prev = _head; - + _head->_next = _head; _size = 0; } + //构造链表 list() { empty_init(); } - list(initializer_list il) - { - empty_init(); - for (auto& e : il) - { - push_back(e); - } - } - - // lt2(lt1) + //拷贝构造 + //lt2(lt1),const对象转为范围for使用const迭代器,不写const迭代器会报错 //list(const list& lt) list(list& lt) { empty_init(); + + //复用push_back for (auto& e : lt) { push_back(e); @@ -204,20 +235,25 @@ namespace bit std::swap(_size, lt._size); } - // lt1 = lt3 + //lt1 = lt3; list& operator=(list lt) { swap(lt); return *this; } - ~list() + //多个数据,但需要先重载operator= + list(initializer_list il) { - clear(); - delete _head; - _head = nullptr; + empty_init(); + + for (auto& e : il) + { + push_back(e); + } } + //不清除哨兵位 void clear() { auto it = begin(); @@ -227,231 +263,94 @@ namespace bit } } - size_t size() + ~list() { - return _size; + clear(); + //删除哨兵位 + delete _head; + _head = nullptr; } + //尾插 void push_back(const T& x) { - /*Node* tail = _head->_prev; - Node* newnode = new Node(x); + ////记录新的尾节点 + //Node* tail = _head->_prev; - tail->_next = newnode; - newnode->_prev = tail; - newnode->_next = _head; - _head->_prev = newnode;*/ + ////创建新节点插入 + //Node* newnode = new Node; + //newnode->_data = x; + //newnode->_next = _head; + //newnode->_prev = tail; + + //tail->_next = newnode; + //_head->_prev = newnode; + + //写完insert后直接复用 insert(end(), x); } - + //头插 void push_front(const T& x) { insert(begin(), x); } + //尾删,复用erase + //但是注意!此处要删除的尾节点并非哨兵位,所以是哨兵位的prev结点,需要--end() void pop_back() { erase(--end()); } - + //头删,复用erase + //因为是复用所以_size已经修改好 void pop_front() { erase(begin()); } - void insert(iterator pos, const T& x) + iterator insert(iterator pos, const T& x) { Node* cur = pos._node; Node* prev = cur->_prev; Node* newnode = new Node(x); - // prev newnode cur - prev->_next = newnode; + newnode->_prev = prev; newnode->_next = cur; + + prev->_next = newnode; cur->_prev = newnode; ++_size; + + return newnode; } + //防止迭代器失效,返回迭代器 iterator erase(iterator pos) { assert(pos != end()); Node* cur = pos._node; - Node* prev = cur->_prev; Node* next = cur->_next; + Node* prev = cur->_prev; - prev->_next = next; next->_prev = prev; + prev->_next = next; delete cur; --_size; - - //return iterator(next); + //next前移到pos位置 return next; } - private: - Node* _head; - size_t _size; - }; - - void test_list1() - { - list lt; - lt.push_back(1); - lt.push_back(2); - lt.push_back(3); - lt.push_back(4); - - list::iterator it = lt.begin(); - while (it != lt.end()) - { - *it += 10; - cout << *it << " "; - ++it; - } - cout << endl; - - lt.push_front(1); - lt.push_front(2); - lt.push_front(3); - lt.push_front(4); - - for (auto e : lt) - { - cout << e << " "; - } - cout << endl; - - lt.pop_back(); - lt.pop_back(); - lt.pop_front(); - lt.pop_front(); - - for (auto e : lt) + size_t size() const { - cout << e << " "; - } - cout << endl; - } - - void test_list2() - { - list lt; - lt.push_back(1); - lt.push_back(2); - lt.push_back(3); - lt.push_back(4); - - list::iterator it = lt.begin(); - while (it != lt.end()) - { - if (*it % 2 == 0) - { - it = lt.erase(it); - } - else - { - ++it; - } - } - - for (auto e : lt) - { - cout << e << " "; - } - cout << endl; - } - - // const T* p1; - // T* const p2; - void print(const list& lt) - { - // constҪDZ޸ģָݲ޸ - //const list::iterator it = lt.begin(); - list::const_iterator it = lt.begin(); - while (it != lt.end()) - { - // *it = 10; - cout << *it << " "; - ++it; - } - cout << endl; - } - - void test_list3() - { - list lt1; - lt1.push_back(1); - lt1.push_back(2); - lt1.push_back(3); - lt1.push_back(4); - - for (auto e : lt1) - { - cout << e << " "; - } - cout << endl; - - list lt2(lt1); - - for (auto e : lt2) - { - cout << e << " "; - } - cout << endl; - - list lt3 = {1,2,3,4,5,6}; - lt1 = lt3; - - for (auto e : lt1) - { - cout << e << " "; + return _size; } - cout << endl; - } - struct AA - { - int _a1; - int _a2; - - AA(int a1 = 1, int a2 = 1) - :_a1(a1) - ,_a2(a2) - {} + private: + //头结点 + Node* _head; + size_t _size; }; - - void test_list4() - { - //AA aa1 = { 1,1 }; - vector v1; - v1.push_back({ 1,1 }); - v1.push_back({ 2,2 }); - v1.push_back({ 3,3 }); - vector::iterator it1 = v1.begin(); - while (it1 != v1.end()) - { - cout << it1->_a1<<":" <_a2<< endl; - ++it1; - } - cout << endl; - - list lt1; - lt1.push_back({ 1,1 }); - lt1.push_back({ 2,2 }); - lt1.push_back({ 3,3 }); - list::iterator lit1 = lt1.begin(); - while (lit1 != lt1.end()) - { - //cout << (*lit1)._a1 <<":"<< (*lit1)._a2 << endl; - // ⴦ʡһ->,Ϊ˿ɶ - cout << lit1->_a1 << ":" << lit1->_a2 << endl; - cout << lit1.operator->()->_a1 << ":" << lit1.operator->()->_a2 << endl; - - ++lit1; - } - cout << endl; - } -} +}; -- Gitee