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

ops/2024/10/17 23:21:32/

目录

文章目录

前言

一、代码

总结



前言

本文主要展示了模拟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/ops/123881.html

相关文章

每日一题:单例模式

每日一题&#xff1a;单例模式 ❝ 单例模式是确保一个类只有一个实例&#xff0c;并提供一个全局访问点 1.饿汉式&#xff08;静态常量&#xff09; 特点&#xff1a;在类加载时就创建了实例。优点&#xff1a;简单易懂&#xff0c;线程安全。缺点&#xff1a;无论是否使用&…

JavaScript网页设计案例:构建动态交互的在线图书管理系统

JavaScript网页设计案例&#xff1a;构建动态交互的在线图书管理系统 在当今的数字化时代&#xff0c;网页设计不仅仅是关于美观和布局&#xff0c;更重要的是用户体验和互动性。JavaScript&#xff0c;作为一种强大的编程语言&#xff0c;在网页开发中扮演着至关重要的角色&a…

基于单片机的穿戴式泳池遇险紧急呼救系统的设计

本计基于单片机的穿戴式泳池遇险紧急呼救系统装置。该装置采用STC12C5A60S2单片机与心率检测模块MAX30102的一体化脉冲血氧分析仪和心率监测器&#xff0c;对人体的心跳进行了实时检测。该装置由发送端和接收端两部分组成&#xff0c;中间由LORA无线通信模块进行数据传输&#…

Django学习笔记十三:优秀案例学习

Django CMS 是一个基于 Django 框架的开源内容管理系统&#xff0c;它允许开发者轻松地创建和管理网站内容。Django CMS 提供了一个易于使用的界面来实现动态网站的快速开发&#xff0c;并且具有丰富的内容管理功能和多种插件扩展。以下是 Django CMS 的一些核心特性和如何开始…

<Project-8 pdf2tx-MM> Python Flask应用:在浏览器中翻译PDF文件 NLTK OCR 多线程 指定翻译器 改进后的P6

项目概述 名字解释 缩写&#xff1a; pdf2tx-MM pdf file transfer to text content with Multi-threads and Multi-translators pdf2tx-MM 是一个基于 Flask 的 Web 应用程序&#xff0c;提供将 PDF 文件中的内容提取、翻译并展示。使用者上传 PDF 文件&#xff0c;应用程序…

前端基础(四十):拖放功能的实现

效果 源码 <div class"draggable-wrap"><div class"draggable-box" draggable"true" data-json{"name": "Lee"}><h1>Lee</h1><div class"drop-box" data-json{"name": &qu…

数据结构与算法JavaScript描述练习------第3章列表

1. 增加一个向列表中插入元素的方法&#xff0c;该方法只在待插元素大于列表中的所有元素时才执 行插入操作。这里的大于有多重含义&#xff0c;对于数字&#xff0c;它是指数值上的大小&#xff1b;对于字母&#xff0c;它 是指在字母表中出现的先后顺序。 function isGreate…

安宝特方案 | AR技术在轨交行业的应用优势

随着轨道交通行业不断向智能化和数字化转型&#xff0c;传统巡检方式的局限性日益凸显。而安宝特AR眼镜以其独特的佩戴方式和轻便设计&#xff0c;为轨道交通巡检领域注入了创新活力&#xff0c;提供了全新的解决方案。 01 多样化佩戴方法&#xff0c;完美适应户外环境 安宝特…