当前位置: 首页 > 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…...

Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动

一、前言说明 在2011版本的gb28181协议中&#xff0c;拉取视频流只要求udp方式&#xff0c;从2016开始要求新增支持tcp被动和tcp主动两种方式&#xff0c;udp理论上会丢包的&#xff0c;所以实际使用过程可能会出现画面花屏的情况&#xff0c;而tcp肯定不丢包&#xff0c;起码…...

【SpringBoot】100、SpringBoot中使用自定义注解+AOP实现参数自动解密

在实际项目中,用户注册、登录、修改密码等操作,都涉及到参数传输安全问题。所以我们需要在前端对账户、密码等敏感信息加密传输,在后端接收到数据后能自动解密。 1、引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId...

线程与协程

1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指&#xff1a;像函数调用/返回一样轻量地完成任务切换。 举例说明&#xff1a; 当你在程序中写一个函数调用&#xff1a; funcA() 然后 funcA 执行完后返回&…...

UDP(Echoserver)

网络命令 Ping 命令 检测网络是否连通 使用方法: ping -c 次数 网址ping -c 3 www.baidu.comnetstat 命令 netstat 是一个用来查看网络状态的重要工具. 语法&#xff1a;netstat [选项] 功能&#xff1a;查看网络状态 常用选项&#xff1a; n 拒绝显示别名&#…...

【论文笔记】若干矿井粉尘检测算法概述

总的来说&#xff0c;传统机器学习、传统机器学习与深度学习的结合、LSTM等算法所需要的数据集来源于矿井传感器测量的粉尘浓度&#xff0c;通过建立回归模型来预测未来矿井的粉尘浓度。传统机器学习算法性能易受数据中极端值的影响。YOLO等计算机视觉算法所需要的数据集来源于…...

《基于Apache Flink的流处理》笔记

思维导图 1-3 章 4-7章 8-11 章 参考资料 源码&#xff1a; https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...

sipsak:SIP瑞士军刀!全参数详细教程!Kali Linux教程!

简介 sipsak 是一个面向会话初始协议 (SIP) 应用程序开发人员和管理员的小型命令行工具。它可以用于对 SIP 应用程序和设备进行一些简单的测试。 sipsak 是一款 SIP 压力和诊断实用程序。它通过 sip-uri 向服务器发送 SIP 请求&#xff0c;并检查收到的响应。它以以下模式之一…...

Linux离线(zip方式)安装docker

目录 基础信息操作系统信息docker信息 安装实例安装步骤示例 遇到的问题问题1&#xff1a;修改默认工作路径启动失败问题2 找不到对应组 基础信息 操作系统信息 OS版本&#xff1a;CentOS 7 64位 内核版本&#xff1a;3.10.0 相关命令&#xff1a; uname -rcat /etc/os-rele…...

GitHub 趋势日报 (2025年06月06日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 590 cognee 551 onlook 399 project-based-learning 348 build-your-own-x 320 ne…...

Golang——6、指针和结构体

指针和结构体 1、指针1.1、指针地址和指针类型1.2、指针取值1.3、new和make 2、结构体2.1、type关键字的使用2.2、结构体的定义和初始化2.3、结构体方法和接收者2.4、给任意类型添加方法2.5、结构体的匿名字段2.6、嵌套结构体2.7、嵌套匿名结构体2.8、结构体的继承 3、结构体与…...