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

【C++篇】list模拟实现

实现接口:

  1. list的无参构造、n个val构造、拷贝构造

  2. operator=重载

  3. 实现迭代器

  4. push_back()

  5. push_front()

  6. erase()

  7. insert()

  8. 头尾删

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;namespace liu
{//定义list节点template<class T>struct list_node{T  _data;list_node<T>* _prev;list_node<T>* _next;list_node(const T& x=T()):_data(x),_prev(nullptr),_next(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--(){_node = _node->_prev;return *this;}bool operator!=(const Self& s){return _node != s._node;}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;void empty_init(){_head = new Node();_head->_prev = _head;_head->_next = _head;}list()//无参构造{empty_init();}list(size_t n,const T& val=T())//n个val{empty_init();for (int i=0;i<n;i++){push_back(val);}}list(const list<T>& i1){empty_init();for(auto& x:i1){push_back(x);}}~list(){clear();delete _head;_head = nullptr;}void clear(){auto it = begin();while (it!=end()){it=erase(it);}}iterator begin(){return iterator(_head->_next);}const_iterator begin()const{return const_iterator(_head->_next);}iterator end(){return iterator(_head);}const_iterator end()const{return const_iterator(_head);}iterator insert(iterator pos,const T&val){Node* new_node = new Node(val);Node* cur = pos._node;Node* prev = cur->_prev;prev->_next = new_node;new_node->_prev = prev;cur->_prev = new_node;new_node->_next = cur;return iterator(new_node);}iterator erase(iterator pos){assert(pos!=end());Node* del = pos._node;Node* prev = del->_prev;Node* next = del->_next;prev->_next = next;next->_prev = prev;delete del;return iterator(next);}void pop_front(){erase(begin());}void pop_back(){erase(--end());}void swap(list<T>& i1){std::swap(_head,i1._head);}list<T>& operator=(list<T> il){swap(il);return *this;}void push_back(const T& x){/*Node* new_node = new Node(x);Node* tail = _head->_prev;tail->_next = new_node;new_node->_prev = tail;new_node->_next = _head;_head->_prev = new_node;*/insert(end(),x);}void push_front(const T& x){/*Node* new_node = new Node(x);Node* prev = _head->_next;_head->_next = new_node;new_node->_prev = _head;prev->_prev = new_node;new_node->_next = prev;*/insert(begin(), x);}private:Node* _head;//双端链表,哨兵位};template<class T>void swap(T&a,T& b){T c(a);a = b;b = c;}template<class T>void swap(list<T>& a, list<T>& b){a.swap(b);}void test1(){/*liu::list<int>i1;i1.push_back(1);i1.push_back(2);i1.push_back(3);i1.push_back(4);i1.push_front(5);i1.push_front(6);i1.push_front(7);liu::list<int>::iterator it1 = i1.begin();while (it1!=i1.end()){cout << *it1 << " ";++it1;}cout << endl;liu::list<int>::iterator it2 = i1.begin();it2=i1.erase(it2);while (it2 != i1.end()){cout << *it2 << " ";++it2;}*/liu::list<int>i1(10,1);liu::list<int>i2(10,2);liu::swap(i1, i2);for (auto x : i2){cout << x << " ";}cout << endl;for (auto x : i1){cout << x << " ";}cout << endl;}
}

http://www.xdnf.cn/news/654643.html

相关文章:

  • Qt qml Network error问题
  • 「读书报告」内网安全攻防
  • 每日算法-250526
  • GitLab 18.0 正式发布,15.0 将不再受技术支持,须升级【三】
  • 消防营区管理升级:豪越科技智能仓储与装备管理的力量
  • 【Java项目测试报告】:在线音乐平台(Online-Music)
  • 开发过的一个Coding项目
  • top查看 CPU使用情况
  • 【Java学习笔记】单例设计模式
  • C++23 std::start_lifetime_as:用于隐式生存期类型的显式生存期管理函数 (P2590R2)
  • Java网络编程中的I/O操作:从字节流到对象序列化
  • DJI上云API官方demo学习
  • JavaSE核心知识点04工具04-01(JDK21)
  • 【opencv】vs2019中配置opencv
  • 同一个核磁共振(MRI)检查中,不同序列的图像之间空间坐标定位如何实现
  • Redis | 缓存技术对后端的重要性
  • STM32之SPI——外部FLASH和RFID
  • 宫格导航--纯血鸿蒙组件库AUI
  • 树莓派超全系列教程文档--(47)如何使用内核补丁
  • QT中常用的类
  • Cesium 实战 26 - 自定义纹理材质 - 实际应用之飞线(抛物线)
  • 并发的产生及对应的解决方案之服务架构说明
  • 第1章第1节:安全运维基础思维与体系建设-安全运维的定义与核心目标
  • Ext系列文件系统
  • 分布式缓存:证明分布式系统的 CAP 理论
  • [闲谈]C语言的面向对象
  • 易境通WMS系统:赋能快消品海外仓高效管理
  • 完美解决Docker镜像无法拉取问题(转载)
  • 服务器的IP是什么东西?
  • uniapp-商城-69-shop(2-商品列表,点击商品展示,商品的详情, vuex的使用,rich-text使用)