C++ list容器的底层实现

一.list是什么

  list 是 C++容器中的带头双向链表,头结点不存储数据,头结点的下一个元素是第一个存储数据的元素,头结点的前一个元素连接着最后一个存储数据的元素。(结构如下图所示)

 其中链表里每一个节点的结构分为,数据部分,前指针和后指针构成(下面是结构图和代码)

二.list 代码实现和语法解析

2.1 定义结点的结构

链表中的数据和前后指针的类型都是模板生成的,可以适配内置类型和自定义类型。这里用struct来定义类的原因是struct默认的成员变量类型都是public,其他函数可以很方便的进行调用。在这里还有一个list_node的构造函数 T()是一个匿名变量,其作用不止是用于存放数据,此外如果调用list_node构造函数时没有写参数的话,这里也会有一个默认的值。

 2.2 定义链表的结构

 在list中,有两个成员变量的存在一个是节点类型的指针_node 和计算节点数量的_size。

template<class T>
class list
{
typedef list_node<T> Node;
void empty_init()
{_head = new Node;_head -> next = _head;_head -> prev = _head;_size = 0;
}
list()
{empty_init();
}private:Node* _node;size_t _size;
}

2.3 添加链表功能(插入 push_back)

 我们已经定义好了list的基础结构,下面我们就要添加一些基本的功能了,例如往list中插入数据,用迭代器来遍历list链表之类的操作。

Push_back 函数:首先构建一个新的节点newnode,然后把需要存放的数据X存到newnode中,找到尾结点并且将newnode插入到尾节点的下一个节点。

template<class T>
struct list_node
{T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T& x=T()):_data(x), _next(nullptr), _prev(nullptr){}
};template<class T>
class list
{
typedef list_node<T> Node;
void empty_init()
{_head = new Node;_head -> next = _head;_head -> prev = _head;_size = 0;
}
list()
{empty_init();
}
void push_back(const T& x)
{Node* newnode = new Node(x);Node* tail = _head->prev;tail->_next = newnode;newnode->prev = tail;newnode->_next = _head;_head->prev = newnode;_size++; 
}private:Node* _node;size_t _size;
}

2.4 添加链表功能(迭代器和迭代器的运算符重载)

和顺序表不同,因为链表的节点不是存放在一起的连续空间,所以指针的++和解引用都没有办法直接的获取到数据,因此在这里我们需要自己去重载 *  ->  ++ --等等的运算符。

template<class T>
struct list_node
{T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T& x=T()):_data(x), _next(nullptr), _prev(nullptr){}
};
template<class T>
struct __list_iterator
{typedef list_node<T> Node;typedef __list_iterator self;Node* _node;__list_iterator(Node* node):_node(node){}T& operator *(Node * node){return _node->_data;}T* operator(Node * node){return &_node->_data;}self& operator++(){_node = _node->next;return *this;}self& operator--(){_node = _node->prev;return *this;}bool operator!=(const self& s){return _node != s._node;}
}template<class T>
class list
{
typedef list_node<T> Node;
typedef __list_iterator<T> iterator;iterator begin()
{return iterator(_head->next);
}iterator end()
{return iterator(_head);
}void empty_init()
{_head = new Node;_head -> next = _head;_head -> prev = _head;_size = 0;
}list()
{empty_init();
}void push_back(const T& x)
{Node* newnode = new Node(x);Node* tail = _head->prev;tail->_next = newnode;newnode->prev = tail;newnode->_next = _head;_head->prev = newnode;_size++; 
}private:Node* _node;size_t _size;
}

做完了以上的操作以后,我们就可以这样来调试程序了,虽然List并不是数组,但是我们可以用相似的方式来对List内部的元素进行访问。虽然迭代器迭代容器各个元素的方式是一样的,但是迭代器屏蔽了底层实现的细节,提供了统一访问的方式

test_list()
{list<int> lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);list<int>::iterator it = lt.begin();while(it != lt.end()){cout << *it << " ";++it;}cout<< endl;
}

2.5 添加链表功能(插入操作的升级 insert()和 eraser()删除)

首先我们可以写一个 insert()插入的函数,这个函数可以被其他的函数所复用例如push_back和push_front ,也可以在任意的位置插入数据。

iterator insert(iterator pos , const T& x)
{Node* newnode = new Node(x);Node* cur = pos._node;Node* prev = cur ->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;return newnode; 
}iterator erase(iterator pos)
{Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;delete cur;prev->_next = next;next->_prev = prev;return next;
}void push_back(const T& x)
{insert(this.end(),x);
}void push_front(const T& x)
{insert(begin(),x);
}void pop_back()
{erase(--end());
}void pop_front()
{erase(begin());
}

2.6 添加链表功能(clear函数和析构函数)

因为erase函数的返回值是被删除数据的下一个位置,所以这里不写++ 也会有++的效果。

~list()
{clear();delete _head;_head = nullpre;
}void clear()
{iterator it = begin();while(it != end()){it = erase(*it);}
}    

2.7 添加链表功能(拷贝构造和赋值重载)

(1)拷贝构造

list( list<int> lt )
{empty_init();for(auto e : lt){this.push_back(e);}
}

(2)赋值重载

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

2.8 const 类型的迭代器

对于const类型的迭代器,我们需要了解,const 类型的迭代器不可以修改迭代器所指向的数据,而不是迭代器本身不可修改,迭代器本身需要完成++,--等操作进行遍历操作,所以在写const类型的迭代器时,我只对迭代器返回指向的内容的函数进行const修饰即可,其余函数均不改变。

template<class T>
struct __list_const_iterator
{typedef list_node<T> Node;typedef __list_const_iterator<T> self;Node* _node;__list_const_iterator(Node* node):_node(node){}const T& operator *(){return _node->_data;}const T* operator->(){return &_node->_data;}
};

三.完整代码

#pragma once
#include<iostream>
using namespace std;
namespace hjy
{template<class T>struct list_node{T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T& x=T()):_data(x), _next(nullptr), _prev(nullptr){}};template<class T,class Ref,class Ptr>struct __list_iterator{ typedef list_node<T> Node;typedef __list_iterator<T,Ref,Ptr> self;Node* _node;__list_iterator(Node* node):_node(node){}self& operator ++(){_node = _node->_next;return *this;}self& operator --(){_node = _node->_prev;return *this;}self operator ++(int){self tmp(*this);_node = _node->_next;return tmp;}self operator --(int){self tmp(*this);_node = _node->_prev;return tmp;}Ref operator *(){return _node->_data;}Ptr operator->(){return &_node->_data;}bool operator!=(const self& s){return _node != s._node;}};/*template<class T>struct __list_const_iterator{typedef list_node<T> Node;typedef __list_const_iterator<T> self;Node* _node;__list_const_iterator(Node* node):_node(node){}self& operator ++(){_node = _node->_next;return *this;}self& operator --(){_node = _node->_prev;return *this;}self operator ++(int){self tmp(*this);_node = _node->_next;return tmp;}self operator --(int){self tmp(*this);_node = _node->_prev;return tmp;}const T& operator *(){return _node->_data;}const T* operator->(){return &_node->_data;}bool operator!=(const self& s){return _node != s._node;}};*/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;//typedef __list_const_iterator<T> const_iterator;const_iterator begin()const{return const_iterator(_head->_next);}const_iterator end()const{return const_iterator(_head);}iterator begin(){return _head->_next;}iterator end(){return _head;}void empty_init(){_head = new Node;_head->_next = _head;_head->_prev = _head;_size = 0;}list(){empty_init();}//list<int>& operator=(const list<int>& lt) 传统写法//{//	if (this != &lt)//		clear();//	for (auto e : lt)//	{//		push_back(e);//	}//	return *this;//}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}list<int>& operator=(list<int> lt){swap(lt);return *this;}~list(){clear();delete _head;_head = nullptr;}//list(const list<T>& lt)//没有const迭代器所以这样写不行//{//	empty_init();//	for (auto e : lt)//	{//		push_back(e);//	}//}list( list<T>& lt){empty_init();for (auto e : lt){push_back(e);}}void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_front(){erase(begin());}void pop_back(){erase(end()--);}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}iterator insert(iterator pos, const T& val){Node* cur = pos._node;Node* newnode = new Node(val);Node* prev = cur->_prev;prev->_next = newnode;newnode->_prev = prev;newnode->_next = cur;cur->_prev = newnode;++_size;return iterator(newnode);}iterator erase(iterator pos){Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;delete cur;prev->_next = next;next->_prev = prev;--_size;return iterator(next);}size_t size(){return _size;}private:Node* _head;size_t _size;};void test_list1(){list<int>lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);lt.push_back(5);for (auto e : lt)cout << e << " ";cout << endl;list<int>lt1 = lt;for (auto e : lt1)cout << e << " ";cout << endl;}/*void print_list(const list<int>& lt){list<int>::const_iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;for (auto e : lt){cout << e << " ";}cout << endl;}*/template<typename T>void print_list(const list<T>& lt){typename list<T>::const_iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;}void test_list2(){list<string>lt;lt.push_back("111");lt.push_back("111");lt.push_back("111");print_list(lt);}
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI究竟是在帮助开发者还是取代他们

前言 人工智能&#xff08;AI&#xff09;的迅猛发展正在各行各业引发深远影响。尤其是在软件开发领域&#xff0c;AI的应用日益广泛&#xff0c;带来了效率和创新的提升。然而&#xff0c;随着AI技术的不断进步&#xff0c;人们也开始担心AI是否会取代人类开发者&#xff0c;…

PyQt5开发笔记:2. 2D与3D散点图、水平布局和边框修饰

一、装pyqtgraph和PyOpenGL库 pip install pyqtgraph pip install PyOpenGL 注意&#xff1a;一定不要pip install OpenGL&#xff0c;否则会找不到 二、3D散点图效果 import pyqtgraph as pg import pyqtgraph.opengl as gl import numpy as np# 创建应用程序 app pg.mkQ…

【计算机组成原理 | 第二篇】计算机硬件架构的发展

目录 前言&#xff1a; 冯诺依曼计算机架构 现代计算机架构&#xff1a; 总结&#xff1a; 前言&#xff1a; 在当今数字化时代&#xff0c;计算机硬件不仅是技术进步的见证者&#xff0c;更是推动这一进步的基石。它们构成了我们日常生活中不可或缺的数字生态系统的核心&a…

数据失踪了?小米手机数据恢复并不难,3个方法就能搞定

手机数据就如同我们的“数字生命线”&#xff0c;一旦失去&#xff0c;便仿佛陷入了一片数据的荒漠&#xff0c;感到无助与迷茫。小米手机用户们&#xff0c;你是否曾遭遇过这样的困境&#xff1a;打开手机&#xff0c;却发现重要的照片、联系人、短信等数据不见了&#xff0c;…

Flutter和React Native(RN)的比较

Flutter和React Native&#xff08;RN&#xff09;都是用于构建跨平台移动应用程序的流行框架。两者都具有各自的优势和劣势&#xff0c;选择哪个框架取决于您的具体需求和项目。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 以下是…

数据库作业六

创建视图v_emp_dept_id_1&#xff0c;查询销售部门的员工姓名和家庭住址 CREATE VIEW v_emp_dept_id_1 AS SELECT e.emp_name,e.address FROM dept d, emp e WHERE e.dept_id (SELECT dept_id FROM dept WHERE dept_name 销售部); SELECT * FROM v_emp_dept_id_1; 创建视…

乐财业:打造财税服务的“硬核“竞争力

乐财业 智慧财税赋能平台 乐财业是目前市面上唯一一家真正实现“业财税”"三位一体全面融合的综合赋能平台&#xff0c;全新打造一站式、流程化、生态化的全产品供应链&#xff0c;立足于企业“业财"融合的发展趋势&#xff0c;凭借20年的财税服务经验&#xff0c;站…

CoreDump使用与实现原理

一、背景 系统发生native crash时&#xff0c;针对内存异常访问、内存踩踏等疑难问题&#xff0c;由于tombstone信息量不足无法精确定位分析这类问题。 二、coredump介绍 2.1 什么是coredump 当用户程序运行过程中发生异常, 程序异常退出时, 由Linux内核把程序当前的内存状…

C语言笔记29 •单链表经典算法OJ题-1.合并两个升序链表•

1.合并两个升序链表&#xff08;创建头节点 简化代码&#xff09; ListNode* lowlisthead(ListNode*)malloc(sizeof(ListNode)); 新颖之处就是创建头节点&#xff08;哨兵位&#xff09;能够减少代码&#xff0c;不用每次都判断链表是否为NULL&#xff0c; 注意的是&#xff1a…

笔记:如何使用Microsoft.Extensions.Options

一、目的&#xff1a; Microsoft.Extensions.Options 是 .NET Core 中用于处理配置选项的一个库。它提供了一种强类型的方式来读取和绑定配置数据&#xff08;例如来自 appsettings.json 文件、环境变量或其他配置源的数据&#xff09;&#xff0c;并将其注入到应用程序中。这个…

ss工具dump出vsock 端口号异常分析

端口冲突时&#xff0c;会出现bind fail异常&#xff0c;这时可以用ss --vsock -pl命令dump出所有listen状态的vsock,但实际发现传入的9000端口&#xff0c;dump出来却是10275&#xff0c;如下图&#xff1a; 难道是内核把端口改了&#xff1f;分析内核态源码&#xff0c;ss最终…

模拟器大揭秘:功能多样,热门APP一网打尽

在咱们日常的数字生活中&#xff0c;模拟器这个词儿你可能不陌生&#xff0c;但它到底能干啥&#xff1f;又有哪些好用的模拟器APP呢&#xff1f; 今天&#xff0c;咱们就来聊聊模拟器的功能&#xff0c;并推荐几款热门的模拟器APP&#xff0c;帮助大家更好地利用这一技术。 …

Math/System/Runtime/Object

1、Math &#xff08;1&#xff09;常用方法 类型方法名说明public static intabs (int a)返回整数的绝对值public static doublepow (double a,double b)计算a的b次幂的值public static int max (int a,int b) 获取两个int值中的较大值public static intmin (int a,int…

java读取配置文件(包含国家于二字码对应关系文件)

读取配置文件 1.java文件 import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.ClassPathResource;import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Map; Slf4j public class…

无障碍快捷方式图标

问题背景 测试反馈&#xff0c;无障碍快捷方式和setting里的无障碍图标不一致。 无障碍快捷方式悬浮窗 1、悬浮窗在systemui中 frameworks\base\packages\SystemUI\src\com\android\systemui\accessibility\floatingmenu\AccessibilityTargetAdapter.java 图标获取方式&…

数据结构笔记之连通图与强连通图

一、引言 在图论中&#xff0c;我们常常会遇到连通图和强连通图的概念。它们描述了图中顶点之间的连接情况&#xff0c;对于理解和分析复杂网络具有重要意义。 二、连通图 定义&#xff1a;若图G中任意两个顶点都是连通的&#xff0c;则称图G为连通图&#xff1b;否则称为非…

如何使用可道云结合内网穿透工具实现远程访问打造私人云盘

文章目录 1.前言2. Kodcloud网站搭建2.1. Kodcloud下载和安装2.2 Kodcloud网页测试 3. cpolar内网穿透的安装和注册4. 本地网页发布4.1 Cpolar云端设置4.2 Cpolar本地设置 5. 公网访问测试6.结语 &#x1f4a1; 推荐 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易…

【HTML入门】第十课 - 表格,也就是table标签

这一小节&#xff0c;我们说一下HTML中的表格。比如我们常常看见的学生成绩单&#xff0c;比如excel一个单元格一个单元格的&#xff0c;这些都是表格。 表格的标签名是 table 。 目录 1 表格中的一些子标签 1.1 表头区域 1.2 表格内容区域 1.3 行和列 2 实战一小下 2.…

Python:数学运算及导入math的应用

guess&#xff1f; x 3 - 1 2 * 2 ** 3 % 2 # 算术运算 # 精度高 print(5 / 3) # 保留整数 向下取整 print(5 // 3) # 取余数 print(5 % 3) # n*m 表示有m个n print(* * 3) # n**m 表示n的m次方 print(10 ** 3) # 没有自增自减 写成 x-1 x1# 运算优先级&#xff1a; # 括号里…

JavaWeb(一:基础知识和环境搭建)

一、基本概念 1.前言 JavaWeb&#xff1a;在Java中&#xff0c;动态web资源开发网页的技术。 web应用分两种&#xff1a;静态web资源和动态web资源 Ⅰ. 静态web资源&#xff08;如html 页面&#xff09;&#xff1a; 指web页面中的数据始终是不变。 所有用户看到都是同一个…