欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > C++之LIST模拟实现(代码纯享版)

C++之LIST模拟实现(代码纯享版)

2025/5/7 11:56:02 来源:https://blog.csdn.net/2201_76018839/article/details/142824880  浏览:    关键词:C++之LIST模拟实现(代码纯享版)

目录

文章目录

前言

一、代码

总结



前言

本文主要展示了模拟List的代码实现

一、代码

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace zlh
{template<class T>struct list_node{T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T& data=T()):_data(data),_next(nullptr),_prev(nullptr){}};template<class T,class ref,class ptr>struct list_iterator{typedef list_node<T> node;typedef list_iterator<T,ref,ptr> self;node* _node;list_iterator(node* node):_node(node){}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;}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,T&,T*> iterator;typedef list_iterator<T, const T&,const T*> const_iterator;iterator begin(){return _head->_next;}iterator end(){return _head;}const_iterator begin() const{return _head->_next;}const_iterator end() const{return _head;}void empty_init(){_head = new node;_head->_next = _head;_head->_prev = _head;_size = 0;}list(){empty_init();}list(initializer_list<T> li){empty_init();for (auto e : li){push_back(e);}}list(const list<T>& x){empty_init();for (auto& e : x){push_back(e);//插入e不是x}}~list(){clear();delete _head;_head = nullptr;}void clear(){auto it = begin();while (it != end()){it = erase(it);}}void push_back(const T& x){//node* newnode = new node(x);//node* tail = _head->_prev;//tail->_next = newnode;//newnode->_next = _head;//newnode->_prev = tail;//_head->_prev = newnode;//++_size;		insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}size_t size() const{return _size;}bool empty()const{return _size == 0;}iterator insert(iterator pos, const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);// prev newnode curnewnode->_next = cur;cur->_prev = newnode;newnode->_prev = prev;prev->_next = newnode;++_size;return newnode;}iterator erase(iterator pos){assert(pos!=end());node* next = pos._node->_next;node* prev = pos._node->_prev;prev->_next = next;next->_prev = prev;delete pos._node;--_size;return next;}private:node* _head;size_t _size;};void test_list1(){list<int> l1;l1.push_back(1);l1.push_back(2);l1.push_back(3);l1.push_back(4);l1.push_back(5);list<int>::iterator is= l1.begin();is=l1.insert(is,20);//l1.erase(is);while (is != l1.end()){cout << *is << " ";++is;}cout << endl;//for (auto e : l1)//{//	cout << e << " ";//}list<int>l2(l1);list<int>::iterator it = l2.begin();for (auto e : l2){cout << e << " ";}}
}


总结

通过模拟实现List类,可以加深我们对list的印象,了解它的底层逻辑,可以使我们更好地去使用它。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词