list的介绍及其模拟实现

news/2025/2/13 4:55:29/

今天我们了解list,list在python中是列表的意思 ,但是在C++中它是一个带头双向循环链表:

list的介绍
  1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)
list的模拟实现

有了前面的string和vector的模拟实现,我们的list的模拟实现算是轻车熟路了,我们要想模拟实现list就需要了解list在库里面的源码,我们用everything查找一下
在这里插入图片描述
在这里插入图片描述
可以看到,在list的类里面成员参数只有一个,但是这个参数是此前定义的一个结构体,它包含了,next,prev和当前节点存储的data,所以我们同样需要去自定义一个结构体

我们首先把定义一个结构体,就是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){}
};

然后我们就可以在命名空间内定义list类了:
为了可读性和代码的简洁,我就用Node来作为list_node的重命名了

namespace jh
{template <class T>struct list_node{T _data;list_node* _prev;list_node* _next;list_node(const T& x = T()):_data(x), _prev(nullptr), _next(nullptr){}};template <class T>class list{typedef list_node<T> Node;private:Node* _head;size_t _size;};
}

我们首先就拿下最难啃的一块骨头:

迭代器

我们再次查看list的源码就会发现:
迭代器同样地使用了一个结构体来构造,所以这里我们也采用结构体
在这里插入图片描述
我们先整体地构造一个框架:
至于模块的地方为什么有多个参数我稍后做讲解,这是一个很重要的点
迭代器就是一个节点,我们同时定义一个拷贝构造的函数

	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){}};

++和–的重载:
迭代器最常用的点就是++和–,因为我们需要用迭代器来初始化等等,我们就首先在结构体内重载++和–:
括号后面又int的我们之前的博客也进行学习过,它是后置,编译器会自动识别的,temp就是一个匿名对象,他的生命周期只有一行,这里的->运算符我们之后也要做重载,不然不能用
这里还有一个需要注意的点:
前置是返回对象本身,所以用引用返回减少拷贝,但是后置返回的是对象temp临时变量,是一个常量,不能用引用

self& operator++()
{_node = _node->_next;return *this;
}
self& opetrator--()
{_node = _node->prev;return *this;
}
self operator++(int)
{self temp(*this);_node = _node->next;return temp;
}
self operator--(int)
{self temp(*this);_node = _node->prev;return temp;
}

*和->的重载:
*是解引用,就是返回迭代器所存储的数据,返回data就是
—>操作符前的是一个地址,所以就取地址就可以了,这里的Ref和Ptr就派上用场了

Ref operator*()
{return _node->_data;
}Ptr operator->()
{return &_node->data;
}

!=和==操作符重载:
这里用bool类型就可以了,直接返回它们之间的关系即可

bool operator!=(const self& s)
{return _node != s._node;
}bool operator==(const self& s)
{return _node == s._node;
}

迭代器就完成了:
增加Ref和Ptr的作用就是为了随时适应,例如需要const T或者const T*这种,这样就省去了const迭代器的代码,更加简洁了,这是迭代器的妙处之一!

	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){}self& operator++(){_node = _node->_next;return *this;}self& operator--(){_node = _node->prev;return *this;}self operator++(int){self temp(*this);_node = _node->next;return temp;}self operator--(int){self temp(*this);_node = _node->prev;return temp;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->data;}bool operator!=(const self& s){return _node != s._node;}bool operator==(const self& s){return _node == s._node;}};

迭代器解决后我们就可以将其应用到list类里了:
这里大家记住:
begin就是头节点head的下一个节点
end就是head节点

const_iterator begin() const
{return const_iterator(_head->_next);
}
const_iterator end() const
{return const_iterator(_head);
}
iterator begin()
{return iterator(_head->_next);
}
iterator end()
{return iterator(_head);
}
构造函数

构造函数我们必须有一个头节点head,同时我们要知道当list为空时,head的next和prev都是head本身

void empty_init()
{_head = new Node;_head->_next = _head;_head->_prev = _head;
}list()
{empty_init();
}
insert函数

insert函数要做的就是首先构造一个新的节点,然后插入,插入很简单,我们在数据结构中学过,这里不做过多的讲解:
记住最后要返回插入的那个新节点!

iterator insert(iterator pos, const T& x = T())
{Node* cur = pos._node;Node* newnode = new Node(x);Node* prev = cur->_prev;prev->_next = newnode;newnode->_next = cur;cur->_prev = newnode;newnode->_prev = prev;return iterator(newnode);
}
erase函数

erase函数同样地也是用数据结构的知识来操作,但是erase函数返回的是删除pos位置的下一个位置的迭代器:

iterator erase(iterator pos)
{Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;delete cur;prev->_next = next;next->_prev = prev;return iterator(next);
}
尾删和头删,尾插和头插

这些我们在有了解决了erase和insert之后可以直接复用了:

void push_back(const T& x)
{insert(end(), x);
}
void push_front(const T& x)
{insert(begin(), x);
}
void pop_back()
{erase(end());
}
void pop_front()
{erase(begin());
}
拷贝构造函数

拷贝构造函数我们依旧用pushback和语法糖来实现:
逐一将lt中的元素尾插进入即可

list(const list<t>T& lt)
{empty_init();for (auto e : lt){push_back(e);}
}
赋值操作符重载

赋值操作符重载我们用swap解决,直接调用std库里的swap函数即可:

void swap(list<T>& lt)
{std::swap(_head, lt._head);
}
list<T>& operator=(list<T> lt)
{swap(lt);return *this;
}
析构函数

我们先定义一个clear函数用于清理空间,然后复用,记住将head节点释放:

void clear()
{iterator it = begin();while (it != end()){it = erase(it);//erase每次返回的都是it的next,故可以这样写}
}
~list()
{clear();delete _head;_head = nullptr;
}

到这里,list的模拟实现差不多就结束了,感谢大家的支持!

完整代码如下:

using namespace std;
namespace jh
{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){}self& operator++(){_node = _node->_next;return *this;}self& operator--(){_node = _node->_prev;return *this;}self operator++(int){self temp(*this);_node = _node->_next;return temp;}self operator--(int){self temp(*this);_node = _node->_prev;return temp;}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}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, const T&, const T*> iterator;typedef __list_iterator<T, const T&, const T*> const_iterator;const_iterator begin() const{return const_iterator(_head->_next);}const_iterator end() const{return const_iterator(_head);}iterator begin(){return iterator(_head->_next);}iterator end(){return iterator(_head);}void empty_init(){_head = new Node;_head->_next = _head;_head->_prev = _head;}list(){empty_init();}iterator insert(iterator pos, const T& x = T()){Node* cur = pos._node;Node* newnode = new Node(x);Node* prev = cur->_prev;prev->_next = newnode;newnode->_next = cur;cur->_prev = newnode;newnode->_prev = prev;return iterator(newnode);}iterator erase(iterator pos){Node* cur = pos._node;Node* prev = cur->_prev;Node* next = cur->_next;delete cur;prev->_next = next;next->_prev = prev;return iterator(next);}void push_back(const T& x){insert(end(), x);}void push_front(const T& x){insert(begin(), x);}void pop_back(){erase(end());}void pop_front(){erase(begin());}list(const list<T>& lt){empty_init();for (auto e : lt){push_back(e);}}void swap(list<T>& lt){std::swap(_head, lt._head);}list<int>& operator=(list<int> lt){swap(lt);return *this;}void clear(){iterator it = begin();while (it != end()){it = erase(it);}}~list(){clear();delete _head;_head = nullptr;}private:Node* _head;size_t _size;};
}

http://www.ppmy.cn/news/1332613.html

相关文章

第一篇【传奇开心果短博文系列】Python的库OpenCV技术点案例示例:cv2常用功能和方法

传奇开心果短博文系列 短博文系列目录Python的库OpenCV技术点案例示例系列 短博文目录一、前言二、常用功能和方法示例三、归纳总结 短博文系列目录 Python的库OpenCV技术点案例示例系列 短博文目录 一、前言 cv2是Python中常用的第三方库&#xff0c;也称为OpenCV库&#…

深入浅出 diffusion(2):pytorch 实现 diffusion 加噪过程

我在上篇博客深入浅出 diffusion&#xff08;1&#xff09;&#xff1a;白话 diffusion 原理&#xff08;无公式&#xff09;中介绍了 diffusion 的一些基本原理&#xff0c;其中谈到了 diffusion 的加噪过程&#xff0c;本文用pytorch 实现下到底是怎么加噪的。 import torch…

Shell中的测试及语句

目录 一、测试 &#xff08;一&#xff09;简单条件测试 &#xff08;二&#xff09;逻辑测试 1.符号&& 2.符号|| &#xff08;三&#xff09;整数比较 &#xff08;四&#xff09;字符串比较 1.相等比较 2.不同比较 3.字符串长度检查 4.双中括号 [[ ]] …

[GXYCTF2019]BabySQli1

单引号闭合&#xff0c;列数为三列&#xff0c;但是没有期待的1 2 3回显&#xff0c;而是显示wrong pass。 尝试报错注入时发现过滤了圆括号&#xff0c;网上搜索似乎也没找到能绕过使用圆括号的方法&#xff0c;那么按以往爆库爆表爆字段的方法似乎无法使用了 在响应报文找到一…

视频尺寸魔方:分层遮掩3D扩散模型在视频尺寸延展的应用

▐ 摘要 视频延展(Video Outpainting)是对视频的边界进行扩展的任务。与图像延展不同&#xff0c;视频延展需要考虑到填充区域的时序一致性&#xff0c;这使得问题更具挑战性。在本文中&#xff0c;我们介绍了一个新颖的基于扩散模型的视频尺寸延展方法——分层遮掩3D扩散模型(…

【C#】基础巩固

最近写代码的时候各种灵感勃发&#xff0c;有了灵感&#xff0c;就该实现了&#xff0c;可是&#xff0c;实现起来有些不流畅&#xff0c;总是有这样&#xff0c;那样的卡壳&#xff0c;总结下来发现了几个问题。 1、C#基础内容不是特别牢靠&#xff0c;理解的不到位&#xff…

图像RGB/YUV原理

一、RGB/YUV原理 RGB 和 YUV 是两种常见的图像颜色编码格式&#xff0c;它们在数字图像处理和视频编码中都有广泛的应用。 1.1 RGB&#xff08;红绿蓝&#xff09; RGB 是指红色&#xff08;Red&#xff09;、绿色&#xff08;Green&#xff09;、蓝色&#xff08;Blue&…

虚拟机打开之后,无法响应

文章目录 前言一、虚拟机无法响应的前因后果二、解决办法1.找到安装的虚拟机的位置2.将上面的带.lck 的文件删除3. 重新启动虚拟机 总结 前言 虚拟机一直用的好好的&#xff0c;突然打开后无法响应&#xff0c;在此记录下解决的过程。 一、虚拟机无法响应的前因后果 1、虚拟机…