模拟实现STL_list

1、模拟实现 list 之前应该注意到的问题
  • 1.1 list的迭代器

    list 的迭代器不是一个原生指针,list 的迭代器实现是 将原生态指针进行封装,list 迭代器的如何实现可以看下面的代码。

  • 1.2 list的迭代器失效(erase)

    在实现 erase 的时候会发生迭代器失效,因为 list 的底层结构为带头结点的双向循环链表,因此在 list 中进行插入时是不会导致 list 的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

  • 1.3 要实现对 list 里面存放自定义类型的访问,比如 list < Date >,这样子就需要实现迭代器当中的 operator->(),详细可以看下面的代码。

2、模拟实现 list
define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
namespace L
{
	template<class T>
	struct _list_node
	{
		_list_node<T>* _next;
		_list_node<T>* _prev;
		T _data;

		_list_node(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};

	template<class T, class Ref, class 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;
		}

		//it1 !=  it2
		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}

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


	template<class T>
	class list
	{
		typedef _list_node<T> node;
		typedef _list_iterator<T, T&, T*> iterator;
		typedef _list_iterator<T, const T&, const T*> const_iterator;
	public:
		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)
		{
			clear();//不要忘记清理之前的
			auto it = l.begin();
			while (it != l.end())
			{
				push_back(*it);
				++it;
			}
			return *this;


		}

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

		void push_back(const T& x)//尾插
		{
			node* newnode = new node(x);
			node* tail = _head->_prev;//先找出尾元素

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;

			//insert(end(),x);
		}

		void pop_back()//尾删
		{
	    	node* tail = _head->_prev->_prev;
			tail->_next = _head;
			_head->_prev = tail;
			//erase(--end());
		}

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

		void pop_front()//头删
		{
			erase(begin());
		}

		iterator erase(iterator pos)
		{
			node* cur = pos._node;
			assert(cur != _head);

			node* prev = cur->_prev;
			node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;

			delete cur;
			return iterator(next);
		}

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

		void print_list()
		{
			auto it = begin();
			while (it != end())
			{
				cout << *it << " ";
				++it;
			}
			cout << endl;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

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

		iterator end()
		{ 
			return iterator(_head);//list的 end 就是 _head
		}

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

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

	class Date
	{
	public:
		Date(int year = 1900, int month = 1, int day = 1)
			:_year(year)
			, _month(month)
			, _day(day)
		{}

		int _year;
		int _month;
		int _day;
	};
}
3、对模拟实现 list 进行测试
void test1()
{
	L::list<int> l1;
	L::list<int> l2;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);

	l1.print_list();
	l1.pop_back();
	l1.push_front(25);
	l1.pop_front();
	l1.print_list();

	l2 = l1;
	l2.print_list();
}

void test2()
{
	L::list<L::Date> l3;
	l3.push_back(L::Date());
	l3.push_back(L::Date());

	auto it = l3.begin();
	while (it != l3.end())
	{
		cout << it->_year << " " << it->_month << " " << it->_day << endl;
		++it;
	}
}
int main()
{
	test1();
	test2();
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值