C++系列之list的模拟实现

在这里插入图片描述

💗 💗 博客:小怡同学
💗 💗 个人简介:编程小萌新
💗 💗 如果博客对大家有用的话,请点赞关注再收藏 🌞

list的节点类

template
struct list_Node
{
public:
list_Node* _prev;
list_Node* _next;
T _val;
list_Node(const T& val = T())
{
_prev = _next = nullptr;
_val = val;
}
};`

list的迭代器类

//这里写入多个参数的目的是区分const迭代器
//传入不同的模板就会有不同的类
template<class T,class Ref ,class Ptr>
struct list_iterator
{
	public:
		typedef list_Node<T> Node;
		typedef list_iterator<T,Ref,Ptr> self;
		list_iterator(Node* node = nullptr)
		{
			_node = node;
		}
		list_iterator(const self& i)
		{
			_node(i._node);
		}
		//const对象不改变原数据
		T& operator*()
		{
			return _node->_val;
		}
		T* operator->()
		{
			return &_node->val;
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(_node);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self& operator--(int)
		{
			self tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& l)
		{
			return _node != l._node;
		}
		bool operator==(const self& l)
		{
			return _node == l._node;
		}

		Node* _node;
	};

构造函数

list(int n, const T& value = T())
{
	_head = new Node();
	_head->_prev = _head;
	_head->_next = _head;
	while (n--)
	{
		push_back(value);
	}
}
template <class Intiterator>
list(Intiterator first, Intiterator last)
{
	//这三行代码的作用是制造一个头结点
	_head = new Node();
	_head->_prev = _head;
	_head->_next = _head;
	
	while (first != last)
	{
		push_back(*first);
		first++;
	}
}
list(const list<T>& l)
{
	_head = new Node();
	_head->_prev = _head;
	_head->_next = _head;
	//这里制造一个list对象,构建与l对象一样的元素,在与*this进行调换。
	list<T> tmp (l.begin(),l.end());
	swap(tmp);
}	

析构函数

~list()
{
	clear();//复用clear()函数,如果元素是自定义类型,则一一析构,
	delete _head;
	_head = nullptr;
}

赋值运算符=

list<T>& operator=(const list<T> l)
{
	swap(l);
	return *this;
}

迭代器的使用


iterator begin()
{
	return iterator(_head->_next);
}
iterator end()
{
	return itertor(_head);
}
//const对象迭代器的使用返回的是const指针(实际上迭代器是一个模板,只是类型不同)
const_iterator begin()const
{
	return const_iterator(_head->_next);
}
const_iterator end()const
{
	return itertor(_head);
}		

list的元素大小和判空

size_t size()const//const与非const对象都可调用
{
	return _size;
}
bool empty()const
{
	return _size == 0;
}

访问list的头节点与尾节点

T& front()
{
	return _head->_next->_val;
}
const T& front()const
{
	return _head->_next->_val;
}
T& back()
{
	return _head->_prev->_val;
}
const T& back()const
{
	return _head->_prev->_val;
}

尾插,尾删,头插,尾删,插入,删除,交换,清空

//这里使用了函数的调用
void push_back(const T& val)
{
	insert(end(), val); 
}
void pop_back() 
{ 
	erase(--end()); 
}
void push_front(const T& val) 
{ 
	insert(begin(), val); 
}
void pop_front() 
{ 
	erase(begin());
}
// 在pos位置前插入值为val的节点
//这里不会发生迭代器的失效,迭代器没有被改变,返回时返回pos之前的迭代器
iterator insert(iterator pos, const T& val)
{
	Node* newnode = new Node(val);
	Node* node_pos = pos.Node;
	Node* prev = node_pos->_prev;
	Node* next = node_pos->_next;
	prev->_next = next;
	next->_prev = prev;
	return newnode;
}
// 删除pos位置的节点,返回该节点的下一个位置
//这里发生迭代器的失效。指向pos指针变成野指针,返回时需要更新到该节点的下一个位置
iterator erase(iterator pos)
{
	Node* node_pos = pos.Node;
	Node* node_next = pos.Node->_next;
	node_pos->_prev->_next = node_pos->_next;
	node_next->_prev = node_pos->_prev;
	delete node_pos;
	return iterator(node_next);
}
//清除链表,只保留头节点
void clear()
{
	iterator it = begin();
	while (it != end())
	{
		erase(it);
	}
		_head->_prev = _head;
		_head->_next = _head;
}
//交换链表
void swap(const list<T>& L)
{
	Node* tmp = L._head;
	L._head = tmp;
	tmp = _head;
}
#include  <assert.h>
#include <iostream>
using namespace std;
namespace zjy
{
	template<class T>
	struct list_Node
	{
	public:
		list_Node* _prev;
		list_Node* _next;
		T _val;
		list_Node(const T& val = T())
		{
			_prev = _next = nullptr;
			_val = val;
		}
	};
	template<class T,class Ref ,class Ptr>
	struct list_iterator
	{
	public:
		typedef list_Node<T> Node;
		typedef list_iterator<T,Ref,Ptr> self;
		list_iterator(Node* node = nullptr)
		{
			_node = node;
		}
		list_iterator(const self& i)
		{
			_node(i._node);
		}
		T& operator*()
		{
			return _node->_val;
		}
		T* operator->()
		{
			return &_node->val;
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(_node);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self& operator--(int)
		{
			self tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& l)
		{
			return _node != l._node;
		}
		bool operator==(const self& l)
		{
			return _node == l._node;
		}

		Node* _node;
	};

	template<class T>
	class list
	{
	public:
		typedef list_Node<T> Node;
		typedef list_iterator<T,T&,T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		list()
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;
		}
		
		/*list(int n, const T& value = T())
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;
			while (n--)
			{
				Node* newnode = new Node(value);
				Node* tail = _head->_prev;
				tail -> _next = newnode;
				newnode->_prev = _head;

				newnode->_next = _head;
				_head->_prev = newnode;
				tail = newnode;
			}
		}*/
		list(int n, const T& value = T())
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;
			while (n--)
			{
				push_back(value);
			}
		}

		/*template <class Intiterator>
		list(Intiterator first, Intiterator last)
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;

			Node* begin= first._node;
			Node* end = last._node;
			Node* tail = _head->_prev;
			while (begin != last)
			{
				tail->_next = begin;
				begin->_prev = tail;

				begin->_next = _head;
				_head->_prev = begin;
				
				tail = begin;
				begin++;
			}
		}*/
		template <class Intiterator>
		list(Intiterator first, Intiterator last)
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;

		
			while (first != last)
			{
				push_back(*first);
				first++;
			}

		}
		void  swap(const list<T>& L)
		{
			Node* tmp = L._head;
			L._head = tmp;
			tmp = _head;

		}

		list(const list<T>& l)
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;

			list<T> tmp (l.begin(),l.end());
			swap(tmp);
		}


		list<T>& operator=(const list<T> l)
		{
			swap(l);
			return *this;
		}

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


		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return itertor(_head);
		}
		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end()const
		{
			return const_itertor(_head);
		}
		size_t size()const
		{
			return _size;
		}

		bool empty()const
		{
			return _size == 0;
		}
		T& front()
		{
			return _head->_next->_val;
		}
		const T& front()const
		{
			return _head->_next->_val;
		}
		T& back()
		{
			return _head->_prev->_val;
		}

		const T& back()const
		{
			return _head->_prev->_val;
		}
		void push_back(const T& val) {
			insert(end(), val); 
		}
		void pop_back() { erase(--end()); }
		void push_front(const T& val) { insert(begin(), val); }
		void pop_front() { erase(begin()); }
		// 在pos位置前插入值为val的节点
		iterator insert(iterator pos, const T& val)
		{
			Node* newnode = new Node(val);
			Node* node_pos = pos.Node;

			Node* prev = node_pos->_prev;
			Node* next = node_pos->_next;
			
			prev->_next = next;
			next->_prev = prev;


			return newnode;
		}
		// 删除pos位置的节点,返回该节点的下一个位置
		iterator erase(iterator pos)
		{
			Node* node_pos = pos.Node;
			Node* node_next = pos.Node->_next;

			node_pos->_prev->_next = node_pos->_next;
			node_next->_prev = node_pos->_prev;

			delete node_pos;

			return iterator(node_next);
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it);
				
			}
			_head->_prev = _head;
			_head->_next = _head;
		}

		void test()
		{
			Node* tmp = _head->_next;
			while (tmp != _head)
			{
				cout << tmp->_val << endl;
				tmp = tmp->_next;
			}
		}
	private:
		Node* _head;
		size_t _size;
	};
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/107986.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

构建自动化测试环境:使用Docker和Selenium!

随着软件开发的日益复杂和迭代速度的加快&#xff0c;自动化测试被越来越广泛地应用于软件开发流程中。它能够提高测试效率、减少测试成本&#xff0c;并保证软件质量的稳定性。在构建自动化测试环境方面&#xff0c;Docker 和 Selenium 是两个非常有用的工具。下面将介绍如何使…

JDK21下载和安装

说明 本文介绍 JDK21&#xff08;Oracle版&#xff09;的下载和安装。 下载 Oracle官网JDK21下载页面 根据操作系统的类型&#xff0c;下载相应的版本。本文下载的是Windows64位的安装版。 下载页面示例 安装包示例 安装 双击安装包&#xff0c;开始安装。 把路径改为自定…

Linux操作系统的基础IO

目录 系统文件IOopen函数0 & 1 & 2文件描述符的分配规则重定向输入重定向输出重定向追加重定向dup2 FILE 文件系统inode 软硬链接软链接硬链接 动态库和静态库动静态库的命名方式静态库制作一个库使用库 动态库制作一个库使用库 系统文件IO open函数 int open(const …

高等数学前置知识——二次函数

文章目录 二次函数1.1 二次函数1.2 二次函数的图像1.2.1 a > 0 时1.2.2 a < 0 时1.2.3 二次函数的平移1.2.4 普通二次型函数图像总结 1.3 其他形式的二次函数1.3.1 顶点式1.3.2 交点式 1.4 二次函数与直线的交点 二次函数 1.1 二次函数 二次函数的定义&#xff1a;y a…

C#的DataGridView数据控件(直接访问SQL vs 通过EF实体模型访问SQL)

目录 一、在DataGridView控件中显示数据 1.直接编程访问SQL &#xff08;1&#xff09;源码 &#xff08;2&#xff09;生成效果 2.通过EF实体模型访问SQL &#xff08;1&#xff09;源码 &#xff08;2&#xff09;生成效果 二、获取DataGridView控件中的当前单元格 …

1024 云上见 · 上云挑战(ChatGPT搭建)

【玩转1024】使用函数计算X通义千问搭建AI助手&#xff0c;参与1024小说创作大赛 【使用函数计算X通义千问搭建AI助手&#xff0c;参与小说创作大赛】&#xff1a;本活动基于函数计算X 通义千问快速部署 AI 个人助手应用&#xff0c;用户可以根据需要选择不同角色的AI助手开启…

刀具磨损状态识别(Python代码,MSCNN_LSTM_Attention模型,初期磨损、正常磨损和急剧磨损分类,解压缩直接运行)

1.运行效果&#xff1a;刀具磨损状态识别&#xff08;Python代码&#xff0c;MSCNN_LSTM_Attention模型&#xff0c;初期磨损、正常磨损和急剧磨损&#xff09;_哔哩哔哩_bilibili 环境库&#xff1a; NumPy 版本: 1.19.4 Pandas 版本: 0.23.4 Matplotlib 版本: 2.2.3 Keras …

【C语言】【goto语句】复习捡拾~

goto语句可以在同一个函数内跳转到设定好的位置&#xff1a; #include<stdio.h> int main() {printf("hello world\n");goto next;printf("hehe"); next:printf("leap here\n");return 0; }goto 语句如果使⽤的不当&#xff0c;就会导致在…

构建外卖小程序:技术要点和实际代码

1. 前端开发 前端开发涉及用户界面设计和用户交互。HTML、CSS 和 JavaScript 是构建外卖小程序界面的主要技术。 <!-- HTML 结构示例 --> <header><h1>外卖小程序</h1><!-- 其他导航元素 --> </header> <main><!-- 菜单显示 -…

vm_flutter

附件地址 https://buuoj.cn/match/matches/195/challenges#vm_flutter 可以在buu下载到。 flutter我也不会&#xff0c;只是这个题目加密算法全部在java层&#xff0c;其实就是一个异或和相加。 反编译 package k;import java.util.Stack;/* loaded from: classes.dex */ pu…

【Linux】部署单机项目以及前后端分离项目

Linux部署单机项目&#xff1a; 优点&#xff1a; 简化了系统管理&#xff1a;由于所有服务都在同一台机器上运行&#xff0c;因此可以简化系统管理和维护。 提高了性能&#xff1a;由于没有网络延迟和其他因素的影响&#xff0c;所以可以提高系统的性能。 缺点&#xff1a; 容…

SSH安全登录远程主机

SSH服务器简介 SSH即Security SHell的意思&#xff0c;它可以将连线的封包进行加密技术&#xff0c;之后进行传输&#xff0c;因此相当的安全。 SSH是一种协议标准&#xff0c;其目的是实现安全远程登录以及其它安全网络服务。 SSH协定&#xff0c;在预设的状态下&#xff0c;…

系统架构设计师之系统应用集成

应用集成是指两个或多个应用系统根据业务逻辑的需要而进行的功能之间的相互调用和互操作。应用集成需要在数据集成的基础上完成。应用集成在底层的网络集成和数据集成的基础上实现异构应用系统之间语用层次上的互操作。它们共同构成了实现企业集成化运行最顶层会聚臭成所需要的…

开源利器:it-tools 项目介绍

作为一名开发人员&#xff0c;我们在日常工作和学习中常常需要使用一系列小工具&#xff0c;如JSON格式化、JSON转表格、当前时间戳、XML格式化、SQL格式化、密码生成以及UUID生成等。通常情况下&#xff0c;我们会在网上搜索各种在线工具来满足这些需求。然而&#xff0c;这些…

私有云:【2】AD域的安装

私有云&#xff1a;【2】AD域的安装 1、使用vmwork创建虚拟机2、启动配置虚拟机3、安装域服务4、配置域服务器 1、使用vmwork创建虚拟机 新建虚拟机 稍后安装操作系统 选择win2012&#xff0c;如下图 设置名称及路径 分配硬盘大小&#xff0c;默认60G即可 镜像选择win2012的is…

【网安AIGC专题10.19】论文6:Java漏洞自动修复+数据集 VJBench+大语言模型、APR技术+代码转换方法+LLM和DL-APR模型的挑战与机会

How Effective Are Neural Networks for Fixing Security Vulnerabilities 写在最前面摘要贡献发现 介绍背景&#xff1a;漏洞修复需求和Java漏洞修复方向动机方法贡献 数据集先前的数据集和Java漏洞Benchmark数据集扩展要求数据处理工作最终数据集 VJBenchVJBench 与 Vul4J 的…

SSM度假村管理系统开发mysql数据库web结构java编程计算机网页源码eclipse项目

一、源码特点 SSM 度假村管理系统是一套完善的信息系统&#xff0c;结合SSM框架完成本系统&#xff0c;对理解JSP java编程开发语言有帮助系统采用SSM框架&#xff08;MVC模式开发&#xff09;&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要 采用B/S模式开发…

系列二十五、@Configuration的作用及解析原理

一、作用 Configuration是用来代替传统的xml的配置方式配置bean的。 二、不加Configuration注解不能配置bean吗 能。 三、加与不加的区别 3.1、区别 加了Configuration注解&#xff0c;会为配置类创建cglib动态代理&#xff0c;Bean方法的调用就会通过容器getBean进行获取…

七层负载均衡 HAproxy

一、HAproxy 1、负载均衡类型&#xff1a; (1) 无负载均衡&#xff1a; 没有负载均衡&#xff0c;用户直接连接到 Web 服务器。当许多用户同时访问服务器时&#xff0c;可能无法连接。 (2) 四层负载均衡&#xff1a; 用户访问负载均衡器&#xff0c;负载均衡器将用户的请求…

深入探究深度学习、神经网络与卷积神经网络以及它们在多个领域中的应用

目录 1、什么是深度学习&#xff1f; 2、深度学习的思想 3、深度学习与神经网络 4、深度学习训练过程 4.1、先使用自下上升非监督学习&#xff08;就是从底层开始&#xff0c;一层一层的往顶层训练&#xff09; 4.2、后自顶向下的监督学习&#xff08;就是通过带标签的数…
最新文章