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

C++之LIST模拟实现(代码纯享版)

目录

文章目录

前言

一、代码

总结



前言

本文主要展示了模拟List的代码实现

一、代码

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace zlh
{template<class T>struct list_node{T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T& data=T()):_data(data),_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){}ref operator* (){return _node->_data;}ptr operator-> (){return &_node->_data;}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& s)const{return _node != s._node;}bool operator==(const self& s)const{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;iterator begin(){return _head->_next;}iterator end(){return _head;}const_iterator begin() const{return _head->_next;}const_iterator end() const{return _head;}void empty_init(){_head = new node;_head->_next = _head;_head->_prev = _head;_size = 0;}list(){empty_init();}list(initializer_list<T> li){empty_init();for (auto e : li){push_back(e);}}list(const list<T>& x){empty_init();for (auto& e : x){push_back(e);//插入e不是x}}~list(){clear();delete _head;_head = nullptr;}void clear(){auto it = begin();while (it != end()){it = erase(it);}}void push_back(const T& x){//node* newnode = new node(x);//node* tail = _head->_prev;//tail->_next = newnode;//newnode->_next = _head;//newnode->_prev = tail;//_head->_prev = newnode;//++_size;		insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(--end());}void pop_front(){erase(begin());}void swap(list<T>& lt){std::swap(_head, lt._head);std::swap(_size, lt._size);}size_t size() const{return _size;}bool empty()const{return _size == 0;}iterator insert(iterator pos, const T& x){node* cur = pos._node;node* prev = cur->_prev;node* newnode = new node(x);// prev newnode curnewnode->_next = cur;cur->_prev = newnode;newnode->_prev = prev;prev->_next = newnode;++_size;return newnode;}iterator erase(iterator pos){assert(pos!=end());node* next = pos._node->_next;node* prev = pos._node->_prev;prev->_next = next;next->_prev = prev;delete pos._node;--_size;return next;}private:node* _head;size_t _size;};void test_list1(){list<int> l1;l1.push_back(1);l1.push_back(2);l1.push_back(3);l1.push_back(4);l1.push_back(5);list<int>::iterator is= l1.begin();is=l1.insert(is,20);//l1.erase(is);while (is != l1.end()){cout << *is << " ";++is;}cout << endl;//for (auto e : l1)//{//	cout << e << " ";//}list<int>l2(l1);list<int>::iterator it = l2.begin();for (auto e : l2){cout << e << " ";}}
}


总结

通过模拟实现List类,可以加深我们对list的印象,了解它的底层逻辑,可以使我们更好地去使用它。

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

相关文章:

  • 华为OD机试 - 括号匹配 - 栈(Python/JS/C/C++ 2024 E卷 100分)
  • 打破欧美10年芯片垄断,杨振宁教授关门弟子,仅用三年创造奇迹
  • OpenCV视频I/O(20)视频写入类VideoWriter之用于将图像帧写入视频文件函数write()的使用
  • 音视频入门基础:FLV专题(14)——FFmpeg源码中,解码Script Tag的实现
  • 小猿口算APP脚本(协议版)
  • 【长文梳理webserver核心】核心类篇
  • [实用工具]Docker安装nextcloud实现私有云服务和onlyoffice
  • 基于STM32设计的生猪健康检测管理系统(NBIOT+OneNet)(240)
  • springboot kafka多数据源,通过配置动态加载发送者和消费者
  • 【华为】基于华为交换机的VLAN配置与不同VLAN间通信实现
  • 力扣题11~20
  • 更美观的HTTP性能监测工具:httpstat
  • 在2024 VDC,听一曲“蓝心智能”的江河协奏
  • Python编写的数字光刻仿真程序,使用了Hopkins光刻模型和粒子群优化(PSO)算法来优化掩模设计
  • 【AD那些事 11】绘制PCB板时“隔离” 的那些事(笔记摘抄)
  • sublime配置(竞赛向)
  • 双向数据库迁移工具:轻松实现 MySQL 与 SQLite 数据互导
  • oracle查询表空间信息
  • 使用Python编写你的第一个算法交易程序
  • 点进HTML初步了解
  • 幸运的沈抖,进击的百度智能云
  • android广播实现PIN码设置
  • Mac 需要杀毒软件?
  • Java | Leetcode Java题解之第472题连接词
  • CUDA Graphs学习与实验
  • 【自注意力与Transformer架构在自然语言处理中的演变与应用】
  • LabVIEW交直流接触器动态检测系统
  • Unity3D中基于四叉树的范围检测算法详解
  • k8s网络通信
  • 07 欢乐的跳