当前位置: 首页 > news >正文

C++:list

目录

List的模拟实现

List节点类

List链表结构

List迭代器类

结构

T& operator*();

T& operator->();

Self& operator++();

Self operator++(int);

Self& operator--();

Self& operator--(int);

bool operator!=(const Self& l);

bool operator==(const Self& l);

List的构造

构造函数

迭代器区间构造

拷贝构造

运算符重载

析构函数

List iterator

begin

end

List Capacity

size

empty

List Access

front

back

List Modify

在pos位置前插入值为val的节点

删除pos位置的节点

clear

其它功能进行复用


学习目标

1.会模拟实现list的基础功能

2.const迭代器(模板类的参数)

3.->的重载

List的模拟实现

List节点类

1.指针域         2.数据域

	//1,定义节点template<class T>struct list_node {list_node<T>* _pre;list_node<T>* _next;T _data;//构造函数list_node(const T& x = T()) :_pre(nullptr),_next(nullptr),_data(x){}};

List链表结构

1.结构:双向带头循环链表

	//2.list(链表)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->_pre = _head;_head->_next = _head;}private:node* _head;};

List迭代器类

链表的成员都是节点,需要使用节点指针,于是不再使用原生指针,而对原生指针进行封装

1.迭代器要么是原生指针

2.迭代器要么是自定义类型对原生指针的封装,模拟指针的行为

(Vector中由于存储空间连续,指针ptr++就跳到下一个成员的位置 )

( List中,存储空间不是连续的,指针ptr++不一定会跳到下一个成员的位置)

结构

	//3.list的迭代器类template<class T, class Ref,class Ptr>struct __list_iterator{typedef list_node<T> node;typedef __list_iterator<T, Ref,Ptr> self;node* pnode;//构造函数__list_iterator(node* n):pnode(n){}};

T& operator*();

		//*(解引用)Ref operator*(){return pnode ->_data;}


T& operator->();

		Ptr operator->() {return &pnode->_data;}

Self& operator++();

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


Self operator++(int);

		self operator++(int) {self tmp(*this);//拷贝构造pnode = pnode->_next;return tmp;}


Self& operator--();

		self& operator--() {pnode = pnode->_pre;return *this;}


Self& operator--(int);

		self operator--(int){self tmp(*this);pnode = pnode->_pre;return tmp;}


bool operator!=(const Self& l);

		bool operator==(const self& I) {return pnode == I.pnode;}


bool operator==(const Self& l);

		bool operator!=(const self& I){return pnode != I.pnode;}

List的构造

构造函数

		//构造函数void init(){_head = new node;_head->_pre = _head;_head->_next = _head;}list() {_head = new node;_head->_pre = _head;_head->_next = _head;}list(int n, const T& value = T()) {init();while (n--) {push_back(value);}}

迭代器区间构造

		//迭代器区间构造template <class Iterator>list(Iterator first, Iterator last) {init();while (first != last) {push_back(*first);++first;}}

拷贝构造

		void swap(list<T>& L) {std::swap(_head,L._head);}//拷贝构造(现代写法)list(const list<T>& L) {init();list<T> tmp(L.begin(), L.end());swap(tmp);}

运算符重载

		//运算符重载(现代写法)list<T>& operator=(const list<T> L) {swap(L);return *this;}

这里不加引用就是用其拷贝构造,若加了引用,赋值后改变这个list,L也会被改变

析构函数

		//析构函数~list() {clear();delete _head;_head = nullptr;}

List iterator

迭代器

		//迭代器typedef __list_iterator<T, T&,T*> iterator;typedef __list_iterator<T, const T&,const T*> const_iterator;

begin

		iterator begin() {return iterator(_head->_next);}const_iterator begin()const{return const_iterator(_head->_next);}

end

		iterator end(){return iterator(_head);}const_iterator end()const{return const_iterator(_head);}

List Capacity

size

		size_t size()const {size_t count = 0;const_iterator it = begin();while (it != end()) {count++;++it;}return count;}

empty

		bool empty()const {return _head->_next == _head;}

List Access

front

		T& front() {return _head->_next->_data;}const T& front()const {return _head->_next->_data;}

back

		T& back() {return _head->_pre->_data;}const T& back()const {return _head->_pre->_data;}

List Modify

头插,头删,尾插,尾删,在pos位置插入val,删除pos位置的节点并返回下一节点位置

clear,swap

在pos位置前插入值为val的节点

		iterator insert(iterator pos, const T& val) {node* cur = pos.pnode;node* pre = cur->_pre;node* new_node = new node(val);pre->_next = new_node;//pos前的节点与new_node连接new_node->_pre = pre;new_node->_next = cur;//new_node与pos的节点连接cur->_pre = new_node;return new_node;}

删除pos位置的节点

返回该节点的下一个位置

		iterator erase(iterator pos) {node* pre = pos.pnode->_pre;node* next = pos.pnode->_next;pre->_next = next;//连接pos的前后节点next->_pre = pre;delete pos.pnode;//删除pos位置的节点return next;}

clear

		void clear() {iterator it = begin();while (it != end()) {//it = erase(it);erase(it++);}}

其它功能进行复用

		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位置插入值

相关文章:

C++:list

目录 List的模拟实现 List节点类 List链表结构 List迭代器类 结构 T& operator*(); T& operator->(); Self& operator(); Self operator(int); Self& operator--(); Self& operator--(int); bool operator!(const Self& l); bool oper…...

【C++】搜索二叉树底层实现

目录 一&#xff0c;概念 二&#xff0c;实现分析 1. 插入 &#xff08;1.&#xff09;非递归版本 &#xff08;2.&#xff09;递归版本 2. 打印搜索二叉树 3.查找函数 &#xff08;1.&#xff09;非递归版本 &#xff08;2.&#xff09;递归版本 4. 删除函数&#x…...

C8051F020 SMBus一直处于busy状态解决办法

当SMBus总线处于busy状态切且无法自动释放时&#xff0c;SMB0CN寄存器的第7位一直为 1&#xff0c;总线没有释放。 SMBus总线释放超时的一个纠错机制&#xff0c;它允许SMBus状态机在 SDA 和 SCL 信号线同为高电平超过 10个SMBus时钟源周期后判断总线为释放状态。 如果总线释放…...

Activiz 9.2 for Linux Crack

Activiz 9.2 在 C#、.Net 和 Unity 软件中为您的 3D 内容释放可视化工具包的强大功能。 ActiViz 允许您轻松地将 3D 可视化集成到您的应用程序中。 ActiViz 功能 用 C# 封装的 3D 可视化软件系统 允许在 .NET 环境中快速开发可投入生产的交互式3D 应用程序 支持窗口演示基础 (…...

数据结构 - 链表

线性表的链式存储结构 概念 将线性表 L (a0, a1, … , an-1)中各元素分布在存储器的不同存储块&#xff0c;成为结点&#xff0c;通过地址或指针建立元素之间的联系。 结点的 data 域存放数据元素 ai &#xff0c;而 next 域是一个指针&#xff0c;指向 ai 的直接后继 ai1 …...

Android 12 Bluetooth源码分析蓝牙配对

本文主要是列出一些蓝牙配对重要的类和方法/函数&#xff0c;遇到相关问题时方便查找添加log排查。 蓝牙扫描列表页面&#xff1a;packages/apps/Settings/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java点击其中一个设备会调用&#xff1a;onPrefere…...

Python异步编程并发执行爬虫任务,用回调函数解析响应

一、问题&#xff1a;当发送API请求&#xff0c;读写数据库任务较重时&#xff0c;程序运行效率急剧下降。 异步技术是Python编程中对提升性能非常重要的一项技术。在实际应用&#xff0c;经常面临对外发送网络请求&#xff0c;调用外部接口&#xff0c;或者不断更新数据库或文…...

React组件化开发

1.组件的定义方式 函数组件Functional Component类组件Class Component 2.类组件 export class Profile extends Component {render() {console.log(this.context);return (<div>Profile</div>)} } 组件的名称是大写字符开头&#xff08;无论类组件还是函数组件…...

LuatOS-SOC接口文档(air780E)--crypto - 加解密和hash函数

crypto.md5(str) 计算md5值 参数 传入值类型 解释 string 需要计算的字符串 返回值 返回值类型 解释 string 计算得出的md5值的hex字符串 例子 -- 计算字符串"abc"的md5 log.info("md5", crypto.md5("abc"))crypto.hmac_md5(str, k…...

自动化测试的定位及一些思考

大家对自动化的理解&#xff0c;首先是想到Web UI自动化&#xff0c;这就为什么我一说自动化&#xff0c;公司一般就会有很多人反对&#xff0c;因为自动化的成本实在太高了&#xff0c;其实自动化是分为三个层面的&#xff08;UI层自动化、接口自动化、单元测试&#xff09;&a…...

展会动态 | 迪捷软件邀您参加2023世界智能网联汽车大会

*9月18日之前注册的观众免收门票费* 由北京市人民政府、工业和信息化部、公安部、交通运输部和中国科学技术协会联合主办的2023世界智能网联汽车大会将于9月21日-24日在北京中国国际展览中心&#xff08;顺义馆&#xff09;举行。 论坛背景 本届展会以“聚智成势 协同向新——…...

jenkins自动化部署springboot、gitee项目

服务器需要安装jdk11、maven、gitee 1. jenkins安装 # yum源 sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo # 公钥 sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io-2023.key # 安装 yum install jenkins如果yum源报…...

Python环境配置及基础用法Pycharm库安装与背景设置及避免Venv文件夹

目录 一、Python环境部署及简单使用 1、Python下载安装 2、环境变量配置 3、检查是否安装成功 4、Python的两种模式&#xff08;编辑模式&交互模式&#xff09; 二、Pycharm库安装与背景设置 1、Python库安装 2、Pycharm自定义背景 三、如何避免Venv文件夹 一、P…...

PHP常见的SQL防注入方法

利用Mysqli和PDO 产生原因主要就是一些数据没有经过严格的验证&#xff0c;然后直接拼接 SQL 去查询。导致产生漏洞&#xff0c;比如&#xff1a; $id $_GET[id]; $sql "SELECT name FROM users WHERE id $id";因为没有对 $_GET[‘id’] 做数据类型验证&#xf…...

分布式和中间件等

raft协议 paxos算法ddos 如何避免?怎么预防?怎么发现?利用了TCP什么特点?怎么改进TCP可以预防?服务端处理不了的请求怎么办?连接数最大值需要设置吗?怎么设置? Thrift RPC过程是什么样子的?异构系统怎么完成通信?跟http相比什么优缺点?了解grpc吗?kafka topic part…...

通过http发送post请求的三种Content-Type分析

通过okhttp向服务端发起post网络请求&#xff0c;可以通过Content-Type设置发送请求数据的格式。 常用到的三种&#xff1a; 1&#xff09;application/x-www-form-urlencoded; charsetutf-8 2&#xff09;application/json; charsetutf-8 3&#xff09;multipart/form-dat…...

Vue中的自定义指令详解

文章目录 自定义指令自定义指令-指令的值&#xff08;给自定义指令传参数&#xff09; 自定义指令 自定义指令&#xff1a;自己定义的指令&#xff0c;可以封装一些dom 操作&#xff0c;扩展额外功能&#xff08;自动聚焦&#xff0c;自动加载&#xff0c;懒加载等复杂的指令封…...

[管理与领导-100]:管理者到底是什么?调度器?路由器?交换机?监控器?

目录 前言&#xff1a; 二层交换机 三层路由器 监视器&#xff08;Monitor&#xff09; 调度器 前言&#xff1a; 人在群体中&#xff0c;有点像设备在网络中&#xff0c;管理者到底承担什么的功能&#xff1f; 二层交换机 交换机是计算机网络中&#xff0c;用于连接多台…...

保研CS/软件工程/通信问题汇总

机器学习 1.TP、TN、FP、FN、F1 2.机器学习和深度学习的区别和联系 模型复杂性&#xff1a;深度学习是机器学习的一个子领域&#xff0c;其主要区别在于使用深层的神经网络模型。深度学习模型通常包含多个隐层&#xff0c;可以学习更加复杂的特征表示&#xff0c;因此在某些任…...

word、excel、ppt转为PDF

相关引用对象在代码里了 相关依赖 <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.0.1</version></dependency> <dependency><groupId>org.apache.poi</group…...

synchronized 学习

学习源&#xff1a; https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖&#xff0c;也要考虑性能问题&#xff08;场景&#xff09; 2.常见面试问题&#xff1a; sync出…...

Xshell远程连接Kali(默认 | 私钥)Note版

前言:xshell远程连接&#xff0c;私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...

React Native 开发环境搭建(全平台详解)

React Native 开发环境搭建&#xff08;全平台详解&#xff09; 在开始使用 React Native 开发移动应用之前&#xff0c;正确设置开发环境是至关重要的一步。本文将为你提供一份全面的指南&#xff0c;涵盖 macOS 和 Windows 平台的配置步骤&#xff0c;如何在 Android 和 iOS…...

三维GIS开发cesium智慧地铁教程(5)Cesium相机控制

一、环境搭建 <script src"../cesium1.99/Build/Cesium/Cesium.js"></script> <link rel"stylesheet" href"../cesium1.99/Build/Cesium/Widgets/widgets.css"> 关键配置点&#xff1a; 路径验证&#xff1a;确保相对路径.…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

04-初识css

一、css样式引入 1.1.内部样式 <div style"width: 100px;"></div>1.2.外部样式 1.2.1.外部样式1 <style>.aa {width: 100px;} </style> <div class"aa"></div>1.2.2.外部样式2 <!-- rel内表面引入的是style样…...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

多模态大语言模型arxiv论文略读(108)

CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题&#xff1a;CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者&#xff1a;Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...

LINUX 69 FTP 客服管理系统 man 5 /etc/vsftpd/vsftpd.conf

FTP 客服管理系统 实现kefu123登录&#xff0c;不允许匿名访问&#xff0c;kefu只能访问/data/kefu目录&#xff0c;不能查看其他目录 创建账号密码 useradd kefu echo 123|passwd -stdin kefu [rootcode caozx26420]# echo 123|passwd --stdin kefu 更改用户 kefu 的密码…...