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

C++list类的模拟实现

一、list类的实现(放在my_list命名空间域中)

将整体的实现放在list.h头文件中

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace my_list
{//struct定义节点template<class T>struct list_node{list_node* _next;list_node* _prev;T _val;//构造函数list_node(const T& val = T()):_next(nullptr),_prev(nullptr),_val(val){ }};//迭代器封装(这里把迭代器封装成了类,然后进行使用迭代器的++等操作)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){}//解引用运算符重载Ref operator*(){return _node->_val;}//->重载ptr operator->(){return &_node->_val;}//后置++运算符重载self& operator++() {_node = _node->_next;return *this;}//前置++运算符重载self operator++(int){self tmp(*this);//拷贝构造一份_node = _node->_next;return tmp;}//后置--运算符重载self& operator--(){_node = _node->_prev;return *this;}//前置--运算符重载self operator--(int){self tmp(*this);//拷贝构造一份_node = _node->_prev;return tmp;}//!=运算符重载bool operator!=(const self& it)const{return _node != it._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;//const迭代器如何设计呢?//typedef const_list_iterator<T> const_iterator;//不能像上面这样设计const迭代器,因为const迭代器是指向的内容不能修改,而不是本身不能修改//在operator*运算符重载处返回的T上加const九不能修改了//构造函数list(){_head = new node;_head->_next = _head;_head->_prev = _head;}//析构函数~list(){clear();delete _head;_head = nullptr;}//cear函数void clear(){iterator it = begin();while (it != end()){it = erase(it);}}//拷贝构造list(const list<T>& lt){_head = new node;_head->_next = _head;_head->_prev = _head;for (auto& element : lt){push_back(element);}}//迭代器iterator begin(){return _head->_next;//单参数的构造函数支持隐式类型转换}iterator end(){return _head;}//const迭代器const_iterator begin()const {return const_iterator( _head->_next);//单参数的构造函数支持隐式类型转换}const_iterator end()const{return _head;}//push_back函数(也可以使用insert直接进行哨兵位的前面插入就是尾插)void push_back(const T& x){//保存尾节点node* tail = _head->_prev;//开新节点node* newnode = new node(x);tail->_next = newnode;newnode->_prev = tail;newnode->_next = _head;_head->_prev = newnode;}//insert函数(在pos位置之前插入)iterator insert(iterator pos, const T& x){node* current = pos._node;node* prev = pos._node->_prev;node* newnode = new node(x);prev->_next = newnode;newnode->_prev = prev;newnode->_next = current;current->_prev = newnode;return newnode;}//erase函数iterator erase(iterator pos){assert(pos != end());node* prev = pos._node->_prev;node* next = pos._node->_next;prev->_next = next;next->_prev = prev;delete pos._node;pos._node = nullptr;return next;}//pop_back尾删函数void pop_back(){erase(--end());}//push_front头插函数void push_front(const T& x){insert(begin(), x);}//pop_front头删函数void pop_front(){erase(begin());}//size函数size_t size(){size_t sz = 0;iterator it = begin();while (it != end()){++it;++sz;}return sz;}//赋值重载void swap(list<T>& lt){std::swap(_head, lt->_head);}list<T>& operator=(list<T> lt1){swap(lt1);return *this;}private:node* _head;};
}

注意:const迭代器和普通迭代器使用两个模板参数就可以完成

二、对模拟实现的list进行测试

#include "list.h"
//const迭代器测试
void print(const my_list::list<int> lt)
{my_list::list<int>::const_iterator it = lt.begin();while (it != lt.end()){cout << *it << endl;++it;}
}
void test_my_list1()
{//尾插数据my_list::list<int> lt1;lt1.push_back(10);lt1.push_back(8);lt1.push_back(35);lt1.push_back(23);lt1.push_back(4346);//迭代器遍历my_list::list<int>::iterator it = lt1.begin();while (it != lt1.end()){cout << *it << " ";++it;}cout << endl;//范围forfor (auto element : lt1){cout << element << " ";}cout << endl;print(lt1);
}
struct A
{
public:A(int a = 0, int b = 0):_a(a),_b(b){ }int _a;int _b;
};
void test_my_list2()
{//尾插数据my_list::list<A> lt1;lt1.push_back(A(1,1));lt1.push_back(A(2,2));lt1.push_back(A(3,3));//测试->重载my_list::list<A>::iterator it = lt1.begin();while (it != lt1.end()){cout << (*it)._a << (*it)._b << endl;cout << it->_a << it->_b << endl;//这里按照语法严格来说是it->->_a,但是由于运算符重载要求可读性,编译器做了特殊处理,省略了一个->++it;}cout << endl;
}
void test_my_list3()
{//尾插数据my_list::list<int> lt1;lt1.push_back(10);lt1.push_back(8);lt1.push_back(35);lt1.push_back(23);lt1.push_back(4346);//头插数据lt1.push_front(1);lt1.push_front(2);lt1.push_front(3);lt1.push_front(4);//打印for (auto element : lt1){cout << element << " ";}cout << endl;//头删一个,尾删两个lt1.pop_front();lt1.pop_back();lt1.pop_back();//打印for (auto element : lt1){cout << element << " ";}cout << endl;//清除重新插入数据lt1.clear();lt1.push_front(1);lt1.push_front(2);lt1.push_front(3);lt1.push_front(4);//打印for (auto element : lt1){cout << element << " ";}cout << endl;
}
void test_my_list4()
{//拷贝构造测试my_list::list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);my_list::list<int> lt2(lt1);//打印lt2for (auto element : lt2){cout << element << " ";}cout << endl;//赋值重载测试my_list::list<int> lt3 = lt1;for (auto element : lt3){cout << element << " ";}cout << endl;
}
int main()
{test_my_list1();test_my_list2();test_my_list3();test_my_list4();return 0;
}

三、vector和list对比

http://www.dtcms.com/a/536581.html

相关文章:

  • 深圳三站合一网站建设网站建设推广怎样找客户
  • 【多所高校主办】第七届机器人、智能控制与人工智能国际学术会议(RICAI 2025)
  • 做网站有虚拟服务器什么是网络营销产生的基础
  • 高配款浮标五参数—可以及时掌握水体的生态状况
  • 《Java 实用技巧:均匀取元素算法(支持不足补齐)》
  • 【Linux】nohup命令
  • 泰州网站建设案例昆明网站seo外包
  • 【成长纪实】星光不负 码向未来|我的 HarmonyOS 学习之路与社区成长故事
  • 网站服务器租用4t多少钱一年啊提供网站建设公司有哪些
  • 如何处理系统环境变量的字符长度超过了 Windows 对话框的限制(2047 个字符)
  • 快速上手大模型:深度学习1(初识、神经网络基础)
  • Java---StringBuffer类
  • 【从零开始构建性能测试体系-10】人工智能与性能测试:如何借助AI提升测试效率
  • 网站建设人员要与客户谈什么一篇关于大学网站建设与管理的论文
  • 子洲网站建设制作网站上做网页怎么改图片
  • kafka使用-Producer
  • CUDA实现的点云MLS滤波
  • Spring Framework源码解析——TaskScheduler
  • 【从零开始开发远程桌面连接控制工具】02-服务端实现详解
  • 湖州网站设计公司WordPress博客Vieu主题
  • 国外好看的网站设计国外网站需要备案
  • 福克斯特solo4 2i2 Focusrite solo4 2i2 录制音乐 全民K歌单声道问题
  • 《信息系统项目管理师》案例分析题及解析模拟题8
  • MCU中的HSE(高速外部时钟,High-Speed External)
  • 开发中的英语积累 P9:Dispatch、Multi、Retain、Restore、Yield、Interrupt
  • ViT算法流程——从 原始像素 → 网络输出 logits 的 每一步张量形状、公式、关键代码
  • 前端与移动开发之 CSS vs QSS
  • 上那个网站找手工活做网上项目外包
  • 网站建设项目开发响应式学校网站模板下载
  • CICD之git