C++---list模拟实现

1. list模拟实现注意点

1.1 list介绍

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

1.2 list的迭代器失效

迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。为了避免这种情况,对于insert来说,需要返回新插入结点的迭代器,对于earse来说,需要返回删除结点位置的下一个结点的迭代器,即可解决。

2. list模拟实现的完整代码

#pragma once
#include<assert.h>
namespace wzy
{
	template<class T>
	struct __list_node
	{
		T _date;
		__list_node<T>* _next;
		__list_node<T>* _prev;

		//这个节点的构造函数     T()表示一个匿名对象
		__list_node(const T& x = T())
			:_date(x)
			, _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)
		{}
		//拷贝构造 赋值重载  析构函数等都不需要去写,默认生成的就行

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

		bool operator==(const self& s)
		{
			return !(*this != s);
		}

		Ref operator*()const
		{
			return _node->_date;
		}

		//前置++
		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;
		}

		Ptr operator->()const
		{
			return &_node->_date;
		}
	};

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


		
		//__list_node<T>是他的类型,但是构造函数是与类名相同
		list()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}


		~list()
		{
			clear();
			delete _head;
			_head = nullptr; //其实这个对象的声明周期都到了,没有人可以拿到,所以这句代码在析构函数中可有可无
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = earse(it); //返回的删除位置的下一个
			}
		}

		//lt2(lt1)
		list(const list<T>& lt)
		{
			//_head = new node;
			//_head->_next = _head;
			//_head->_prev = _head;


			//const_iterator it = lt.begin();
			//while (it != lt.end())
			//{
			//	push_back(*it);
			//	++it;
			//}

			//现代写法
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;

			list<T> tmp(lt.begin(), lt.end());
			std::swap(_head, tmp._head); 
		}


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


		//拿迭代器去写构造函数
		template<class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;

			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}


		//引用可以很好解决传参过程中的深拷贝问题
		void push_back(const T& x)
		{
			//node* newnode = new node(x);
			//node* tail = _head->_prev;
			_head  tail  newnode
			//tail->_next = newnode;
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//_head->_prev = newnode;

			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			earse(--end());
		}

		void pop_front()
		{
			earse(begin());
		}

		//你在任意的一个位置插入都是前一个和后一个
		//插入可以在任意的位置插入  
		//对于迭代器来说,前后的数据改变了,严格意义上来说,其实也算迭代器失效了
		iterator insert(iterator pos, const T& x)
		{
			node* newnode = new node(x);
			node* cur = pos._node;
			node* prev = cur->_prev;

			//prev newnode cur
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			
			//返回的是新插入那个结点的迭代器
			return iterator(newnode);
		}

		//删除可不敢删除_head,因为删掉大结构直接就会出现问题
		iterator earse(iterator pos)
		{
			assert(pos != end()); //因为迭代器是一个左闭右开的结构,begin()就是在第一个结点的位置,end()就是在_head的位置
			//只需要找到它的前一个和后一个
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete cur; //这个位置都被earse了,你还去++,就是野指针的访问了
			return iterator(next);
		}
	private:
		//双向带头循环链表
		node* _head;
	};
}

3. list与vector的对比

vector与list都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及应用场景不同,其主要不同如下:
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值