C++——list的实现

2024-09-07 11:44
文章标签 c++ 实现 list

本文主要是介绍C++——list的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

0.前言

1.节点类 

2.迭代器类 

①普通迭代器

②const迭代器 

③模板迭代器

3.list类 

3.1 clear、析构函数、swap

①clear

② 析构函数 

③ swap

3.2构造函数 

①无参构造

 ②赋值构造

3.3 迭代器

3.4插入函数

①insert插入

②头插

③尾插

3.5 删除函数

①erase删除

②头删 

③尾删

4.测试

源代码(list.h) 


 

0.前言

我们知道,list是一个双向循环链表,所以list的每个节点中需要存在一个指向前一个节点的指针prev、一个指向下一个节点的指针next和一个数据域data

35f348f4278543a9a4d796c80ea00ba8.png

1.节点类 

因为list的底层是节点,而节点的底层又是prev、next指针和数据域data,所以我们先将节点封装为一个类,然后再用list类调用节点类。节点类的代码如下:

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

2.迭代器类 

在string和vector中我们使用迭代器访问数据时需要有这样的操作:

vector<int>::iterator it = l1.begin();while (it != l1.end()){cout << *it << " ";++it;}cout << endl;

 需要知悉的是,在C++中,为了方便和统一,无论什么类我们大多都采用此方法进行访问,string与vector都是连续的,如此访问必然不会有差错,可是list并不是连续的

我们希望++it就能找到下一个节点,而it解引用(*it)就得到节点里存的data,所以,为了使迭代器能够正常访问,我们在自定义的类中必须实现以下方法:

1. 指针可以解引用,迭代器的类中必须重载operator*()

2. 指针可以通过->访问其所指空间成员,迭代器类中必须重载oprator->()

3. 指针可以++向后移动,迭代器类中必须重载operator++()与operator++(int)

4. 迭代器需要进行是否相等的比较,因此还需要重载operator==()与operator!=()

代码如下:

①普通迭代器

//①普通迭代器 可读可写template<class T>struct __list_iterator{typedef ListNode<T> Node;typedef __list_iterator D;Node* _node;//迭代器构造函数__list_iterator(Node* x):_node(x){}//重载++//前置++D& operator++()//返回迭代器的引用{_node = _node->_next;//指向下一个节点return *this;}//后置++D operator++(int){D tmp(*this);_node = _node->_next;return tmp;//返回拷贝之前的值}//重载--//前置--D& operator--(){_node = _node->_prev;return *this;}//后置--D operator--(int){D tmp(*this);_node = _node->_prev;return tmp;}//重载解引用T& operator*()//返回数据的引用{return _node->_data;//返回节点里的数据}//重载->T* operator->(){return &_node->_data;}//重载!=bool operator !=(const D& s){return _node != s._node;}//重载==bool operator==(const D& s){return _node == s._node;}};

②const迭代器 

const迭代器的作用是只可读不可写,防止数据被修改,因此我们只需在普通迭代器的基础上对operator*()和operator->()添加const操作即可:

//②const迭代器 可读不可写template<class T>struct __list_const_iterator{typedef ListNode<T> Node;typedef __list_const_iterator D;Node* _node;//迭代器构造函数__list_const_iterator(Node* x):_node(x){}//重载++//前置++D& operator++()//返回迭代器的引用{_node = _node->_next;//指向下一个节点return *this;}//后置++D operator++(int){D tmp(*this);_node = _node->_next;return tmp;//返回拷贝之前的值}//重载--//前置--D& operator--(){_node = _node->_prev;return *this;}//后置--D operator--(int){D tmp(*this);_node = _node->_prev;return tmp;}//重载解引用const T& operator*()//返回数据的引用{return _node->_data;//返回节点里的数据}//重载->const T* operator->(){return &_node->_data; }//重载!=bool operator !=(const D& s){return _node != s._node;}//重载==bool operator==(const D& s){return _node == s._node;}};

③模板迭代器

观察以上两个迭代器,不同之处也就在于对operator*()和operator->()的操作不同,代码相似度可以说达到了90%,那么有没有办法减少冗余,提高代码的可读性呢?

答案当然是有的,我们可以为两个迭代器提供一个共同的模板,再提供两个参数,当调用普通迭代器和const迭代器时,只需根据所传递的参数而选择不同的迭代器。

template<class T, class Ref, class Ptr>struct __list_iterator{typedef ListNode<T> Node;typedef __list_iterator<T, Ref, Ptr> D;Node* _node;//迭代器构造函数__list_iterator(Node* x):_node(x){}//重载++//前置++D& operator++()//返回迭代器的引用{_node = _node->_next;//指向下一个节点return *this;}//后置++D operator++(int){D tmp(*this);_node = _node->_next;return tmp;//返回拷贝之前的值}//重载--//前置--D& operator--(){_node = _node->_prev;return *this;}//后置--D operator--(int){D tmp(*this);_node = _node->_prev;return tmp;}//重载解引用Ref operator*()//返回数据的引用{return _node->_data;//返回节点里的数据}//重载->Ptr operator->(){return &_node->_data;}//重载!=bool operator !=(const D& s){return _node != s._node;}//重载==bool operator==(const D& s){return _node == s._node;}};

3.list类 

做好了节点类和迭代器类的准备工作,终于来到了主角list类

//定义链表template<class T>class list{typedef ListNode<T> Node;public:/*typedef __list_iterator<T> iterator;typedef __list_const_iterator<T> const_iterator;*/typedef __list_iterator<T, T&, T*> iterator;typedef __list_iterator<T, const T&, const T*> const_iterator;private:Node* _head;};

3.1 clear、析构函数、swap

①clear

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

② 析构函数 

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

③ swap

//swapvoid swap(list<T>& tmp){std::swap(_head, tmp._head);}

3.2构造函数 

①无参构造

//链表构造函数list(){_head = new Node;_head->_next = _head;_head->_prev = _head;}

 ②赋值构造

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

3.3 迭代器

//普通迭代器iterator begin(){//return iterator(_head->_next);return _head->_next;//单参数的构造函数支持隐式类型转换}iterator end(){return _head;}//const迭代器const_iterator begin() const{//return iterator(_head->_next);return _head->_next;//单参数的构造函数支持隐式类型转换}const_iterator end() const{return _head;}

3.4插入函数

①insert插入

//insert插入
iterator insert(iterator pos, const T& x)
{Node* cur = pos._node;//取当前节点Node* prev = cur->_prev;//当前节点的前一个节点Node* newnode = new Node(x);//创建并初始化新节点prev->_next = newnode;//前一个节点的_next指针指向新节点newnode->_prev = prev;//新节点的_prev指针指向前一个节点newnode->_next = cur;//新节点的_next指针指向当前节点(此时相对于新节点就变成了后一个节点)cur->_prev = newnode;//当前节点的_prev指针指向新节点(此时相对于新节点就变成了后一个节点)//return iterator(newnode);return newnode;
}

②头插

//push_front头插void push_front(const T& x){insert(begin(), x);}

③尾插

原始写法

void push_back(const T& x){Node* newnode = new Node(x);//开辟并初始化新节点newnode Node* tail = _head->_prev;//定义上一个节点为tailtail->_next = newnode;//上一个节点tail的next指针指向新节点newnodenewnode->_prev = tail;//新节点newnode的prev指针指向上一个节点tailnewnode->_next = _head;//新节点newnode的next指针指向头节点_head_head->_prev = newnode;//头节点_head的prve指针指向新节点newnode}

复用insert

void push_back(const T& x){insert(end(), x);}

复用尾插,写拷贝构造:

//拷贝构造list(list<T>& lt){_head = new Node;_head->_next = _head;_head->_prev = _head;//拷贝之前先创建一个头节点,自己指向自己for (const auto& e : lt){push_back(e);}}

3.5 删除函数

①erase删除

iterator erase(iterator pos){assert(pos != end());//避免删除哨兵位的头节点Node* cur = pos._node;//取当前节点Node* prev = cur->_prev;//取前一个节点Node* next = cur->_next;//取后一个节点prev->_next = next;next->_prev = prev;//销毁当前节点delete cur;return next;}

②头删 

//pop_front头删void pop_front(){erase(begin());}

③尾删

//pop_back尾删void pop_back(){erase(--end());}

4.测试

void test_list(){//无参构造list<int> l1;for (auto e : l1){cout << e << " ";}cout << endl;//插入//insert插入l1.insert(l1.begin(), 1);for (auto e : l1){cout << e << " ";}cout << endl;//头插l1.push_front(0);for (auto e : l1){cout << e << " ";}cout << endl;//尾插l1.push_back(2);l1.push_back(3);l1.push_back(4);for (auto e : l1){cout << e << " ";}cout << endl;//删除//erase删除l1.erase(l1.begin());for (auto e : l1){cout << e << " ";}cout << endl;//头删l1.pop_front();for (auto e : l1){cout << e << " ";}cout << endl;//尾删l1.pop_back();for (auto e : l1){cout << e << " ";}cout << endl;//赋值构造list<int> l2 = l1;for (auto e : l1){cout << e << " ";}cout << endl;}

86afb71c747f45f981e321057edb713d.png

源代码(list.h) 

#pragma once#include <iostream>
#include <assert.h>
using namespace std;
#include <assert.h>namespace xxk
{//定义链表节点template<class T>struct ListNode{ListNode<T>* _next;ListNode<T>* _prev;T _data;//链表节点构造函数ListNode(const T& x = T()):_next(nullptr), _prev(nullptr), _data(x){}};//定义迭代器//在vector使用迭代器时:/*vector<int>::iterator it = l1.begin();while (it != l1.end()){cout << *it << " ";++it;}cout << endl;*///在这段代码中我们希望++it就能找到下一个节点,而it解引用(*it)我们需要得到节点里存的data//可是链表并不是连续的,因迭代器使用形式与指针完全相同,想要实现以上功能,我们必须要在自定义类中实现以下方法://1. 指针可以解引用,迭代器的类中必须重载operator*()//2. 指针可以通过->访问其所指空间成员,迭代器类中必须重载oprator->()//3. 指针可以++向后移动,迭代器类中必须重载operator++()与operator++(int)//   至于operator--() / operator--(int)释放需要重载,根据具体的结构来抉择,双向链表可以向前移动,//   所以需要重载,如果是forward_list就不需要重载--//4. 迭代器需要进行是否相等的比较,因此还需要重载operator == ()与operator != ()//③为减少冗余,提高代码的可读性,使用模板将两个类写到一起template<class T, class Ref, class Ptr>struct __list_iterator{typedef ListNode<T> Node;typedef __list_iterator<T, Ref, Ptr> D;Node* _node;//迭代器构造函数__list_iterator(Node* x):_node(x){}//重载++//前置++D& operator++()//返回迭代器的引用{_node = _node->_next;//指向下一个节点return *this;}//后置++D operator++(int){D tmp(*this);_node = _node->_next;return tmp;//返回拷贝之前的值}//重载--//前置--D& operator--(){_node = _node->_prev;return *this;}//后置--D operator--(int){D tmp(*this);_node = _node->_prev;return tmp;}//重载解引用Ref operator*()//返回数据的引用{return _node->_data;//返回节点里的数据}//重载->Ptr operator->(){return &_node->_data;}//重载!=bool operator !=(const D& s){return _node != s._node;}//重载==bool operator==(const D& s){return _node == s._node;}};//定义链表template<class T>class list{typedef ListNode<T> Node;public:/*typedef __list_iterator<T> iterator;typedef __list_const_iterator<T> const_iterator;*/typedef __list_iterator<T, T&, T*> iterator;typedef __list_iterator<T, const T&, const T*> const_iterator;//普通迭代器iterator begin(){//return iterator(_head->_next);return _head->_next;//单参数的构造函数支持隐式类型转换}iterator end(){return _head;}//const迭代器const_iterator begin() const{//return iterator(_head->_next);return _head->_next;//单参数的构造函数支持隐式类型转换}const_iterator end() const{return _head;}//链表构造函数list(){_head = new Node;_head->_next = _head;_head->_prev = _head;}//clearvoid clear(){iterator it = begin();while (it != end()){it = erase(it);}}//析构函数~list(){clear();delete _head;_head = nullptr;}//拷贝构造list(list<T>& lt){_head = new Node;_head->_next = _head;_head->_prev = _head;//拷贝之前先创建一个头节点,自己指向自己for (const auto& e : lt){push_back(e);}}//swapvoid swap(list<T>& tmp){std::swap(_head, tmp._head);}//operator=list<T>& operator=(list<T> lt){swap(lt);return *this;}//尾插①//void push_back(const T& x)//{//	Node* newnode = new Node(x);//开辟并初始化新节点newnode //	Node* tail = _head->_prev;//定义上一个节点为tail//	tail->_next = newnode;//上一个节点tail的next指针指向新节点newnode//	newnode->_prev = tail;//新节点newnode的prev指针指向上一个节点tail//	newnode->_next = _head;//新节点newnode的next指针指向头节点_head//	_head->_prev = newnode;//头节点_head的prve指针指向新节点newnode//}//②复用insertvoid push_back(const T& x){insert(end(), x);}//insert插入iterator insert(iterator pos, const T& x){Node* cur = pos._node;//取当前节点Node* prev = cur->_prev;//当前节点的前一个节点Node* newnode = new Node(x);//创建并初始化新节点prev->_next = newnode;//前一个节点的_next指针指向新节点newnode->_prev = prev;//新节点的_prev指针指向前一个节点newnode->_next = cur;//新节点的_next指针指向当前节点(此时相对于新节点就变成了后一个节点)cur->_prev = newnode;//当前节点的_prev指针指向新节点(此时相对于新节点就变成了后一个节点)//return iterator(newnode);return newnode;}//push_front头插void push_front(const T& x){insert(begin(), x);}//erase删除函数iterator erase(iterator pos){assert(pos != end());//避免删除哨兵位的头节点Node* cur = pos._node;//取当前节点Node* prev = cur->_prev;//取前一个节点Node* next = cur->_next;//取后一个节点prev->_next = next;next->_prev = prev;//销毁当前节点delete cur;return next;}//pop_back尾删void pop_back(){erase(--end());}//pop_front头删void pop_front(){erase(begin());}private:Node* _head;};void test_list(){//无参构造list<int> l1;for (auto e : l1){cout << e << " ";}cout << endl;//插入//insert插入l1.insert(l1.begin(), 1);for (auto e : l1){cout << e << " ";}cout << endl;//头插l1.push_front(0);for (auto e : l1){cout << e << " ";}cout << endl;//尾插l1.push_back(2);l1.push_back(3);l1.push_back(4);for (auto e : l1){cout << e << " ";}cout << endl;//删除//erase删除l1.erase(l1.begin());for (auto e : l1){cout << e << " ";}cout << endl;//头删l1.pop_front();for (auto e : l1){cout << e << " ";}cout << endl;//尾删l1.pop_back();for (auto e : l1){cout << e << " ";}cout << endl;//赋值构造list<int> l2 = l1;for (auto e : l1){cout << e << " ";}cout << endl;}
}

 

 

这篇关于C++——list的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144964

相关文章

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景