STL_list模拟实现

list介绍:

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

代码:

代码实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;

namespace ZYY
{
	//定义node节点
	template<typename T>
	struct _list_node
	{
		_list_node<T>* _prev;
		_list_node<T>* _next;
		T _data;
		_list_node(const T& x = T()):_prev(nullptr),_next(nullptr),_data(x)
		{}
	};


	//封装list的迭代器
	template<typename T, typename Ref, typename Ptr> // Ref是数据类型 Ptr是数据的指针类型
	class _list_iterator
	{
		typedef _list_node<T> node;
		typedef _list_iterator<T, Ref, Ptr> Self;
	public:
		_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)
		{
			_list_iterator<T,Ref,Ptr> tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		//前置--
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		//后置--
		Self operator--(int)
		{
			__list_iterator<T> tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		
		bool operator!=(const Self& it)
		{
			return it._node != _node;
		}

		bool operator==(const Self& it)
		{
			return it._node == _node;
		}
		node* _node;
	};

	template<class T>
	class list
	{
	public:
		typedef _list_node<T> node;
		typedef _list_iterator<T, T&, T*> iterator;
		typedef _list_iterator<T, const T&, const T*> const_iterator;

		list()
		{
			_head = new node(T());
			_head->_next = _head;
			_head->_prev = _head;
		}

		list(const list<T>& l)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			auto it = l.begin();
			while(it != l.end())
			{
				push_back(*it);
				it++;
			}
		}

		list<T>& operator=(const list<T>& l)
		{
			if(this != &l)
			{
				clear();
				auto it = l.begin();
				while(it != l.end())
				{
					push_back(*it);
					it++;
				}
			}
			return *this;
		}


		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		//获得第一个元素
		T& front()
		{
			return _head->_next->_data;
		}

		const T& front()const
		{
			return _head->_next->_data;
		}

		//获得最后一个元素
		T& back()
		{
			return _head->_prev->_data;
		}
		const T& back() const
		{
			return _head->_prev->_data;
		}

		//交换两个list
		void Swap(list<T>& l)
		{
			swap(_head,l._head);
		}

		//在指定位置插入元素
		void insert(iterator pos,const T& x)
		{
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* newnode = new node(x);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
		}


		//删除指定迭代器位置的元素
		iterator erase(iterator pos)
		{
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			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()); //刚开始写成了erase(end()) 程序崩溃
		}

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

		
		size_t size()
		{
			size_t count = 0;
			auto it = begin();
			while(it != end())
			{
				count++;
				it++;
			}
			return count;
		}


		bool empty()
		{
			return begin()==end();
		}


		void resize(size_t newsize,T x = T())
		{
			size_t oldsize = size();
			if(oldsize > newsize)
			{
				for(size_t i = newsize ; i < oldsize; i++)
				{
					pop_back();
				}
			}
			else
			{
				for(size_t i = oldsize ; i < newsize; i++)
				{
					push_back(x);
				}
			}
		}

		//清空list中除了_head的节点
		void clear()
		{
			auto it = begin();
			while(it != end())
			{
				it = erase(it);
			}
		}

		//打印出list的元素 ---- 在STL中的list里没有封装这个函数,只是为了测试,自己加进去的
		void print_list()
		{
			for(auto it:*this)
			{
				cout<<it<<" ";
			}
			cout<<endl;
		}

		iterator begin()
		{
			return (iterator)_head->_next;
		}

		const_iterator begin() const
		{
			return (const_iterator)_head->_next;
		}

		iterator end() 
		{
			return (iterator)_head;
		}

		const_iterator end() const
		{
			return (const_iterator)_head;
		}
	private:
		node* _head;
	};
}

代码测试:

int main()
{
	cout<<"查看list1中元素个数:"<<endl;
	ZYY::list<int> list1;
	cout<<list1.size()<<endl;
	ZYY::list<int> list2;

	cout<<"向list1中插入元素:"<<endl;
	for(int i = 0 ; i < 10;i++){
		list1.push_back(i);
	}
	list1.print_list();

	cout<<"向list2中插入元素:"<<endl;
	for(int i = 10 ; i < 20;i++){
		list2.push_back(i);
	}
	list2.print_list();

	cout<<"交换list1与list2"<<endl;
	list1.Swap(list2);
	list1.print_list();
	list2.print_list();

	cout<<"将list2赋值给list1"<<endl;
	list1 = list2;
	list1.print_list();

	cout<<"将list2清空"<<endl;
	list2.clear();
	list2.print_list();

	cout<<"查看list2的元素个数"<<endl;
	cout<<list2.size()<<endl;

	cout<<"删除list1中的第二个元素"<<endl;
	list1.erase(++list1.begin());
	list1.print_list();

	cout<<"将list1的元素个数变成20,扩充元素为99"<<endl;
	list1.resize(20,99);
	list1.print_list();

	cout<<"将list1的元素个数变成10"<<endl;
	list1.resize(10);
	list1.print_list();
}

测试结果:

在这里插入图片描述

注意事项:

  1. list的迭代器并不是一个原生指针,而是进行封装过的
  2. list迭代器的失效只会发生在删除时(vector容器迭代器的失效是发生在插入删除与扩容时)并且失效的迭代器只是指向删除节点位置的迭代器失效
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值