当前位置: 首页 > 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位置插入值

http://www.lryc.cn/news/173134.html

相关文章:

  • 【C++】搜索二叉树底层实现
  • C8051F020 SMBus一直处于busy状态解决办法
  • Activiz 9.2 for Linux Crack
  • 数据结构 - 链表
  • Android 12 Bluetooth源码分析蓝牙配对
  • Python异步编程并发执行爬虫任务,用回调函数解析响应
  • React组件化开发
  • LuatOS-SOC接口文档(air780E)--crypto - 加解密和hash函数
  • 自动化测试的定位及一些思考
  • 展会动态 | 迪捷软件邀您参加2023世界智能网联汽车大会
  • jenkins自动化部署springboot、gitee项目
  • Python环境配置及基础用法Pycharm库安装与背景设置及避免Venv文件夹
  • PHP常见的SQL防注入方法
  • 分布式和中间件等
  • 通过http发送post请求的三种Content-Type分析
  • Vue中的自定义指令详解
  • [管理与领导-100]:管理者到底是什么?调度器?路由器?交换机?监控器?
  • 保研CS/软件工程/通信问题汇总
  • word、excel、ppt转为PDF
  • 2023华为杯D题——基于Kaya模型的碳排放达峰实证研究
  • 有哪些好用的上网行为管理软件?(上网行为管理软件功能好的软件推荐)
  • npm install报错 code:128
  • 爬虫 — Scrapy 框架(一)
  • Python编程语言学习笔记
  • 【运维面试100问】(三)说说你在故障排除方面的经历
  • Postman 全局配置接口路径变量等
  • 一文掌握CodiMD安装与使用
  • 无人机顶会顶刊2023
  • 【Java毕设项目】基于SpringBoot+Vue校园便利平台的设计与实现
  • 03Nginx的静态资源部署,反向代理,负载均衡,动静分离的配置