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

news/2024/10/11 8:47:57/

目录

文章目录

前言

一、代码

总结



前言

本文主要展示了模拟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.ppmy.cn/news/1537362.html

相关文章

Chromium 中chrome.downloads扩展接口c++

一、前端chrome.downloads 使用 chrome.downloads API 以编程方式启动、监控、操作和搜索下载内容。 权限 downloads 您必须在扩展程序清单中声明 "downloads" 权限&#xff0c;才能使用此 API。 {"name": "My extension",..."permiss…

目录工具类 - C#小函数类推荐

此文记录的是目录工具类。 /***目录工具类Austin Liu 刘恒辉Project Manager and Software DesignerE-Mail: lzhdim163.comBlog: http://lzhdim.cnblogs.comDate: 2024-01-15 15:18:00***/namespace Lzhdim.LPF.Utility {using System.IO;/// <summary>/// The Objec…

【Docker从入门到进阶】06.常见问题与解决方案 07.总结与资源

6. 常见问题与解决方案 在使用Docker进行开发和部署过程中&#xff0c;可能会遇到各种问题。以下是一些常见问题及其解决方案&#xff1a; 容器启动失败和调试 在使用 Docker 时&#xff0c;容器启动失败或立即退出可能会导致一定的困扰&#xff0c;以下是进一步深入解决该问…

Visual Studio 2022安装(含重生版)

前言&#xff1a; 昨天调试代码的时候发现程序怎么都运行不了&#xff0c;错误显示无法找到文件啊啊啊&#xff0c;能力有限&#xff0c;找不出错误源&#xff0c;然后就狠心删掉所有相关文件来“重新开始”&#xff01; 正文&#xff1a; 1.官网下载&#xff08;内定中文版…

鸿蒙next开发者第一课02.DevEcoStudio的使用-习题

【习题】DevEco Studio的使用 通过/及格分80/ 满分100 判断题 1. 如果代码中涉及到一些网络、数据库、传感器等功能的开发&#xff0c;均可使用预览器进行预览。F 正确(True)错误(False) 预览器不能进行传感器等特殊功能的开发,需要使用真机开发 2. module.json5文件中的…

实用Linux脚本

MySQL备份 #!/bin/bashset -eUSER"backup" PASSWORD"backup" # 数据库数据目录 # DATA_DIR"/data/mysql" BIN_INDEX$DATA_DIR"/mysql-bin.index" # 备份目录 # BACKUP_DIR"/data/backup/mysql" BACKUP_LOG"/var/log/m…

微软最新 Office 办公软件2025下载 – Microsoft 365 正版优惠订阅

​ 以前 Office 365 是微软打造的「办公软件订阅」服务。订阅后&#xff0c;用户可以在多个平台使用Word、Excel、PowerPoint、OneDrive云存储网盘等正版办公应用。 微软希望这种订阅方式能够推广到更多的产品和用户&#xff0c;于是决定将 Office 365 升级为全新的「Microsoft…

PFC和LLC的本质和为什么要用PFC和LLC电路原因

我们可以用电感和电容的特性,以及电压和电流之间的不同步原理来解释PFC(功率因数校正)和LLC(谐振变换器)。 电感和电容的基本概念 电感(Inductor): 电感是一种储存电能的组件。它的电流变化比较慢,电流在电感中延迟,而电压变化得比较快。可以把电感想象成一个“滞后…