STL:list实现

list是和vector类似的顺序型容器,也是是比vector更为复杂的容器。list是双向带头链表,初始有一个不存数据的头节点,并通过节点内指针将后续节点依次连接起来 。

相较于vector,list特点如下:

   (1)list可以按需申请,释放不需要扩容操作,减少内存碎片。

 (2)任意位置插入删除的效率是O(1)。

 (3)不支持下标随机访问。

   (4)cache缓存命中率低。

list结构较为复杂,就list节点来说,list自己本身和list节点不是一样的结构,list包含list节点,因此需要分开设计。

#include<iostream>
using namespace std;namespace YHY
{// List的节点类template<class T>struct ListNode{ListNode(const T& val = T()){_val = val;}ListNode<T>* _pPre = nullptr;ListNode<T>* _pNext = nullptr;T _val;};//List的迭代器类template<class T, class Ref, class Ptr>class ListIterator{typedef ListNode<T>* PNode;typedef ListIterator<T, Ref, Ptr> Self;public:ListIterator(PNode pNode = nullptr):_pNode(pNode){}ListIterator(const Self& l){_pNode = l._pNode;}Ref operator*(){return _pNode->_val;}T* operator->(){return &(operator*());}Self& operator++(){_pNode = _pNode->_pNext;return *this;}Self operator++(int){Self old = *this;_pNode = _pNode->_pNext;return old;}Self& operator--(){_pNode = _pNode->_pPre;return _pNode;}Self& operator--(int){Self old = *this;_pNode = _pNode->_pPre;return old;}bool operator!=(const Self& l){return _pNode != l._pNode;}bool operator==(const Self& l) //比较的是节点地址而不是节点内数据,因为比较节点内数据没意义,只有比较地址可以进行遍历的操作{return _pNode == l._pNode;}PNode _pNode;};//list类template<class T>class list{typedef ListNode<T> Node;typedef Node* PNode;public:typedef ListIterator<T, T&, T*> iterator;typedef ListIterator<T, const T&, const T*> const_iterator;public:// List的构造list(){CreateHead();}list(int n, const T& value = T()){CreateHead();for (int i = 0; i < n; i++){push_back(value);}}template <class Iterator>list(Iterator first, Iterator last){CreateHead();while (first != last){push_back(*first);first++;}}list(const list<T>& l){CreateHead();for (auto e : l){push_back(e);}}list<T>& operator=(const list<T> l){CreateHead();list(l.begin(), l.end());return *this;}~list(){clear();delete _pHead;_pHead = nullptr;}// List Iteratorconst_iterator begin() const{ return const_iterator(_pHead->_pNext);}const_iterator end() const{ return const_iterator(_pHead); }iterator begin() { return iterator(_pHead->_pNext); }iterator end() { return iterator(_pHead); }size_t size() const{size_t sum = 0;const_iterator it = begin();while (it != end()){it++;sum++;}return sum;    }bool empty()const{return _pHead->_pNext == _pHead->_pPre;}T& front(){return  _pHead->_pNext;}const T& front()const{return _pHead->_pNext;}T& back(){return  _pHead->_pPre;}const T& back()const{return _pHead->_pPre;}// List Modifyvoid push_back(const T& val){ insert(end(), val);}void pop_back(){      erase(end());}void push_front(const T& val) { insert(begin(), val); }void pop_front() {erase(begin());}// 在pos位置前插入值为val的节点iterator insert(iterator pos, const T& val){PNode newnode = new Node(val);PNode next = pos._pNode;PNode pre = next->_pPre;pre->_pNext = newnode;newnode->_pPre = pre;next->_pPre = newnode;newnode->_pNext = next;return iterator(newnode);}// 删除pos位置的节点,返回该节点的下一个位置iterator erase(iterator pos){PNode cur = pos._pNode->_pNext;PNode pre = pos._pNode->_pPre;cur->_pNext = pre;pre->_pPre = cur;pos._pNode->_pNext = nullptr;pos._pNode->_pPre = nullptr;delete pos._pNode;return iterator(cur);}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}void swap(list<T>& l){std::swap(_pHead, l._pHead);}private:void CreateHead(){PNode newhead = new Node;_pHead = newhead;_pHead->_pNext = _pHead;_pHead->_pPre = _pHead;}PNode _pHead;};
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/656027.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用Docker安装Jenkins,并能够在该Jenkins中使用Docker

1. 构建Dockerfile 试错1 参考https://medium.com/manav503/how-to-build-docker-images-inside-a-jenkins-container-d59944102f30 按照文章里所介绍的&#xff0c;实现在Jenkins容器环境中依然能够调用Docker&#xff0c;需要做到以下几步 下载Jenkins镜像将环境中的docke…

CGAL 网格剖分算法

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 CGAL为我们提供了一种网格剖分算法,它与之前的网格平面分割算法有些类似,只不过它输出的是平面与网格的相交线段,CGAL也为我们提供了一种加速策略(AABB树)。 二、实现代码 #include <iostream> #includ…

java面试算法要刷吗?学算法只是为了面试吗?

在Java的世界里&#xff0c;算法往往被看作是面试的一个重要组成部分。有些人可能会问&#xff0c;学Java不是应该更多关注框架、数据库、微服务之类的吗&#xff1f;为什么还要费那么大劲去研究算法呢&#xff1f;好吧&#xff0c;让我来给你讲讲这背后的逻辑。 1、算法在Jav…

ES6 Reflect详解

文章目录 概述静态方法Reflect.get(target, name, receiver)Reflect.set(target, name, value, receiver)Reflect.has(obj, name)Reflect.deleteProperty(obj, name)Reflect.construct(target, args)Reflect.getPrototypeOf(obj)Reflect.setPrototypeOf(obj, newProto)Reflect.…

项目解决方案:高清视频监控联网设计方案

目 录 一、客户需求 二、网络拓扑图 三、方案描述 四、服务器配置 五、方案优势 1. 多级控制 2. 平台可堆叠使用 3. 支持主流接入协议 4. 多种终端显示 5. 视频质量诊断 6. 客户端功能强大 7. 一机一档 一、客户需求 客户现场存在两个网络环境&#xff0c…

自动化测试CSS元素定位

1.1 CSS定位 1.1.1 绝对路径定位 目标 查找第一个文本为“猜猜看”的a标签 实现 CSS表达式 html>body>div>a[.”猜猜看”] python表达式 driver.find_element_by_css_selector(‘html>body>div>a[.”猜猜看”]’) 1.1.2 相对路径定位 目标 查找第…

【Tomcat与网络1】史前时代—没有Spring该如何写Web服务

在前面我们介绍了网络与Java相关的问题&#xff0c; 最近在调研的时候发现这块内容其实非常复杂&#xff0c;涉及的内容多而且零碎&#xff0c;想短时间梳理出整个体系是不太可能的&#xff0c;所以我们还是继续看Tomcat的问题&#xff0c;后面有网络的内容继续补充吧。 目录 …

【美团】Java高级开发工程师(用户增长方向)

更新时间&#xff1a;2024/01/29 | 工作地点&#xff1a;上海市 | 事业群&#xff1a;点评事业部 | 工作经验&#xff1a;3年 部门介绍 大众点评作为国内重要的本地生活消费决策参考平台&#xff0c;多年来深耕本地生活消费领域&#xff0c;深受广大用户的信任和喜爱。我们的业…

MongoDB安装以及卸载

查询id&#xff1a; docker ps [rootlocalhost ~]# docker stop c7a8c4ac9346 c7a8c4ac9346 [rootlocalhost ~]# docker rm c7a8c4ac9346 c7a8c4ac9346 [rootlocalhost ~]# docker rmi mongo sudo docker pull mongo:4.4 sudo docker images 卸载旧的 sudo docker stop mong…

分享几个好用的前端网站

1.echart图表demo集 https://www.isqqw.com/ 2.json地图在线预览 https://geojson.io/ 3. 全国地图json数据 https://www.poi86.com/ 4.jquery插件库 https://www.jq22.com/ 5.jquery操作手册 https://www.bejson.com/apidoc/jquery/index.html 6.丰富的表格插件 ht…

【基础算法】1、快速排序快速选择

快速排序思想&#xff1a; 1、找一个分界点。 2、在分界点两边开始调整范围。 3、递归两边&#xff0c;重复。 例题&#xff1a; 给定你一个长度为 n的整数数列。 请你使用快速排序对这个数列按照从小到大进行排序。 并将排好序的数列按顺序输出。 输入格式 输入共两行&#xf…

NoSQL 数据库管理系统和模型的比较

前些天发现了一个人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;最重要的屌图甚多&#xff0c;忍不住分享一下给大家。点击跳转到网站。 NoSQL 数据库管理系统和模型的比较 介绍 当大多数人想到数据库时&#xff0c;他们通常会想到传统的关系数据库…

【TCP】流量控制和拥塞控制

前言 TCP&#xff08;传输控制协议&#xff09;是互联网协议&#xff08;IP&#xff09;网络传输层协议&#xff0c;负责控制数据包的顺序和流量控制&#xff0c;以防止网络拥塞和数据丢失。TCP流量控制和拥塞控制是确保网络有效通信的重要机制。具体分析如下&#xff1a; 流…

微服务-微服务Alibaba-Nacos 源码分析(上)

Nacos&Ribbon&Feign核心微服务架构图 架构原理 1、微服务系统在启动时将自己注册到服务注册中心&#xff0c;同时外发布 Http 接口供其它系统调用(一般都是基于Spring MVC) 2、服务消费者基于 Feign 调用服务提供者对外发布的接口&#xff0c;先对调用的本地接口加上…

利用qrcode.vue库生成二维码

利用qrcode.vue库生成二维码 安装 在vue2中 npm install --save qrcode.vue1 # yarn add qrcode.vue在vue3中 npm install --save qrcode.vue3 # yarn add qrcode.vue使用 普通使用&#xff1a; import { createApp } from vue import QrcodeVue from qrcode.vuecreateAp…

【Java程序设计】【C00168】基于SSM的旅游网管理系统(论文+PPT)

基于SSM的旅游网管理系统&#xff08;论文PPT&#xff09; 项目简介项目获取开发环境项目技术运行截图 项目简介 这是一个基于ssm的旅游网系统 本系统分为用户前台功能模块和管理员功能模块2个功能模块。 用户前台功能模块&#xff1a;当游客打开系统的网址后&#xff0c;首先…

【Python笔记-设计模式】单例模式

一、说明 单例是一种创建型设计模式&#xff0c;能够保证一个类只有一个实例&#xff0c; 并提供一个访问该实例的全局节点。 (一) 解决问题 维护共享资源&#xff08;数据库或文件&#xff09;的访问权限&#xff0c;避免多个实例覆盖同一变量&#xff0c;引发程序崩溃。 …

React 基础学习02

以下是React18版本的基础学习资源 点击我获取更多学习资源 1. …模板扩展符 import logo from ./logo.svg; import ./App.css; function App() {const imgData {className: small,style: {wdith: 200px,height: 200px,backgroundColor: grey}}return (<div className&quo…

JavaWeb中的Filter(过滤器)和 Listener(监听器)

提示&#xff1a;这两个东西听起来似乎很难&#xff0c;实际上是非常简单的&#xff0c;按照要求写就行了&#xff0c;一定不要被新名词给吓到了。 JavaWeb中的Filter&#xff08;过滤器&#xff09; 一、Filter&#xff08;过滤器&#xff09;1.如何编写 Filter2.Filter 中的细…

最小二乘圆柱拟合(高斯牛顿法)

欢迎关注更多精彩 关注我&#xff0c;学习常用算法与数据结构&#xff0c;一题多解&#xff0c;降维打击。 本期话题&#xff1a;最小二乘圆柱拟合 相关背景资料 点击前往 圆柱拟合输入和输出要求 输入 8到50个点&#xff0c;全部采样自圆柱上。每个点3个坐标&#xff0c;坐…