C++(List的模拟实现)

news/2024/10/29 3:38:50/

1.成员变量

上一节已知信息 list是带哨兵卫的双向链表链表 ,所以list类成员变量应该有 节点以及节点个数信息

	private://定义哨兵位Node* _head;//记录插入节点个数size_t _size;

2.节点类

每个节点应包含指向下一节点指针、上一节点指针以及自身数据的信息

	template<class T>//节点最好用struct定义 保证对外是共有 避免class默认私有 的麻烦struct list_node{list_node<T>* _next;list_node<T>* _prev;T _val;//构造函数list_node(const T& val = T()):_next(nullptr), _prev(nullptr), _val(val){}};

3.迭代器封装类

3.1类型重定义,定义节点

//定义类模板 Ref迭代器返回的类型。 Ptr指针指向template<class T,class Ref,class Ptr>// 迭代器封装的类struct __list_iterator{typedef list_node<T> Node;//c++中可以这么使用,在内部引用自身类型 (别名)typedef __list_iterator <T, Ref, Ptr> self;Node* _node;

3.2构造函数

		//构造函数__list_iterator(Node* node):_node(node){}

3.3 operator*()

		//Ref代表返回值的类型 在我们这里 可以是T& 也可以是 const T&Ref operator*(){return _node->_val;}

3.4operator->()

	//Ptr代表返回值的类型,在这里可以是T*Ptr operator->(){return &_node->_val;}

3.5operator++()

		self& operator++(){_node = _node->_next;return *this;}

3.6operator++(int)

		//后置++,返回值 因为tmp是临时变量self operator++ (int){self tmp(*this);_node = _node->_next;return tmp;}

3.7operator--()

		//前置--self& operator--(){_node = _node->_prev;return *this;}

3.8operator++(int)

		//后置--self operator--(int){self tmp(*this);_node = _node->_prev;return tmp;}

 3.9比较

		//比较bool operator!= (const self& it) const //传的对象是const{return _node != it._node;}bool operator== (const self& it) const //{return _node == it._node;}

 4.list类

4.1类型重定义

	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;

4.2成员函数

4.2.1构造函数

		list(){empty_init();}

4.2.2 初始化函数

		void empty_init()  // {_head = new Node;_head->_prev = _head;_head->_next = _head;_size = 0;}

4.2.3拷贝构造函数

		list(const list<T>& lt)// 拷贝构造{empty_init();for (auto& e : lt){push_back(e);}}

4.2.4交换函数

		//交换void swap(const list<T>& lt){std:swap(_head, lt._head);std:swap(_size, lt._size);}list<T>& operator= (list<T> lt){swap(lt);return *this;}

4.2.5析构函数

		~list(){clear();delete _head;_head = nullptr;}

4.2.6清除函数

		void clear(){iterator it = begin();while(it != end()){it = erase(it);}_size = 0;}

4.2.7插入insert

		//插入iterator insert(iterator pos, const T& x){Node* cur = pos._node;Node* prev = cur->_prev;Node* newnode = new Node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;++_size;return newnode;}

4.2.8 擦除erase

		iterator 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;//返回删除节点的下个位置return next;}

4.2.9 push_back()

		void push_back(const T& x){insert(end(), x);}

4.2.10push_front()

		void push_front(const T& x){insert(begin(), x);}

4.2.11pop_front()

		void pop_front(){erase(begin());}size_t size(){return _size;}

4.2.12 pop_back()

		void pop_back(){erase(--end());//end()前一个位置}

5测试函数

	void test_list1(){list<int>lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list<int>::iterator it = lt.begin();while (it != lt.end()){(*it) += 1;std::cout << *it << " ";++it;}std::cout << std::endl;//范围for是傻瓜式替代方式for (auto e : lt){std::cout << e << " ";}std::cout << std::endl;Print(lt);}struct A{A(int a1 = 0, int a2 = 0):_a1(a1), _a2(a2){};int _a1;int _a2;};void test2_list2(){list<A> lt;lt.push_back(A(1, 1));lt.push_back(A(2, 2));lt.push_back(A(3, 3));lt.push_back(A(4, 4));list<A> ::iterator it = lt.begin();while(it != lt.end()){//对it解引用 得到是结构体对象std::cout << (*it)._a1 << " " << (*it)._a2 << std::endl;//通过结构体指针指向 -> -> 编译器直接省略成一个std::cout << it->_a1 << " " << it->_a2 << std::endl;++it;}std::cout << std::endl;}


http://www.ppmy.cn/news/1134492.html

相关文章

柠檬水找零【贪心1】

由于是贪心算法的第一道题&#xff0c;所以先介绍一下贪心算法。 贪心策略&#xff1a;一种解决问题的策略&#xff0c;局部最优->全局最优。&#xff08;贪婪鼠目寸光&#xff09; 1、把解决问题的过程分为若干步 2、解决每一步时&#xff0c;都选择当前看起来最优的解法。…

常见开发、测试模型

开发模型瀑布模型螺旋模型增量、迭代敏捷开发模型 测试模型V模型W模型 开发模型 瀑布模型 瀑布模型的每一个阶段都只执行一次&#xff0c;是线性顺序进行的软件开发模式。 优点&#xff1a;每个阶段做什么&#xff1b;产生什么非常清晰&#xff1b; 缺点&#xff1a;风险往…

uni-app:实现元素在屏幕中的居中(绝对定位absolute)

一、实现水平居中 效果 代码 <template><view><view class"center">我需要居中</view></view> </template><style>.center {position: absolute;left:50%;transform: translateX(-50%);border:1px solid black;} </s…

维基百科启用HTTPS的全球影响

2015 年 6 月&#xff0c;维基媒体基金会宣布维基百科默认启用 HTTPS&#xff0c;基金会称此举旨在保护访问者的隐私和安全&#xff0c;让用户能安全和不被审查的自由获取知识。在没有启用 HTTPS 的年代&#xff0c;审查者能知道访问者访问了维基百科上的哪些条目&#xff0c;它…

GROMACS Tutorial 5: Protein-Ligand Complex 中文实战教程

GROMACS Tutorial 5: Protein-Ligand Complex 中文实战教程 前言系统环境特别强调一、预处理阶段1.1 蛋白质配体分离以及除水操作1.2 选择力场识别JZ4配体1.2.1 使用在线力场解析1.2.2 使用官方推荐力场CHARMM36解析 1.3 蛋白的top文件准备1.4 配体的top文件准备1.5 使用CgenFF…

91、Redis - 事务 与 订阅-发布 相关的命令 及 演示

★ 事务相关的命令 Redis事务保证事务内的多条命令会按顺序作为整体执行&#xff0c;其他客户端发出的请求绝不可能被插入到事务处理的中间&#xff0c; 这样可以保证事务内所有命令作为一个隔离操作被执行。 Redis事务同样具有原子性&#xff0c;事务内所有命令要么全部被执…

关于“兆易创新杯”中国研究生电子设计竞赛的一点个人小经验

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、一些基本常识二、赛前准备要充分三、选题很重要&#xff01;&#xff01;&#xff01;四、队友很关键五、一些碎碎念总结 前言 请注意这是我参加“兆易创新…

websocket逆向

Websocket逆向技术的实际应用 前言一、websocket逆向实际应用1. 插件开发2. 弹幕互动游戏3. (半)无人直播 二、直播间常见通信技术1. http轮询2. websocket通信 三. 直播间常见消息解析技术四、 逆向直播间常用技术1. RPC远程服务调用2. 通信拦截3. API调用 五、学习路线实战1&…