list的使用及其模拟实现

一.list iterator的使用

1. begin end 为正向迭代器,对迭代器执行 ++操作,迭代器向后移动
2. rbegin(end) rend(begin) 为反向迭代器,对迭代器执行 ++操作,迭代器向前移动
	int ar[] = { 1,2,3,4,5,6,7,8,9,0 };
	int n = sizeof(ar) / sizeof(ar[0]);
	//list<int> mylist(10,5);//带头结点的双向循环链表
	list<int> mylist(ar, ar + n);
	for (const auto& e : mylist)
	{
		cout << e << " ";
	}
	cout << endl;
	list<int>::iterator it = mylist.begin();
	while (it != mylist.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	list<int>::reverse_iterator rit = mylist.rbegin();
	while (rit != mylist.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;

 3.list的迭代器失效问题

迭代器类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节 点被删除了 因为 list 的底层结构为带头结点的双向循环链表 ,因此 list 中进行插入时是不会导致 list 的迭代 器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响.
mylist.erase(it++);

二.list的内置函数

list<int> mylist;

mylist.empty 检测list是否为空,是返回true,否则返回false

mylist.size    返回list中的有效节点的个数

mylist.front   返回list的第一个节点中值的引用

mylist.back   返回list的最后一个节点的值的引用 

三.list模拟实现 

namespace ljx
{
	template<class T>
	struct __list_node
	{
		__list_node<T>* _next;
		__list_node<T>* _prev;
		T _data; 
		__list_node(const T& x=T())
		    :_data(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)
		{}
		T* operator->()
		{
			return &_node->_data;
		}
		T& operator*()
		{
			return _node->_data;
		}
		Self& operator++()
		{  
			_node = _node->_next;
			return *this;
		}
		Self& operator++(int)
		{
			Self tmp(*this);
			//_node = _node->_next;
			++(*this);
			return tmp;
		}
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		Self& operator--(int)
		{
			__list_iterator<T>tmp(*this);
			--(*this);
			return tmp;
		}
		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}
		bool operator==(const Self& it)
		{
			return _node == it._node;
		}
	};
	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;
		//带头双向循环链表
		//typedef __list_iterator<T,const T&,const T * > const_iterator;
		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);
		}
		list()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		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;
			//}
			for (auto e : lt)
			{
				push_back(e);
			}
		}
		//list <T>& operator=(const list<T>& lt)
		//{
		//	if (this != &lt)
		//	{
		//		clear();
		//		for (auto e : lt)
		//		{
		//			push_back(e);
		//		}
		//	}
		//	return *this;
		//}
		list<T>& operator=(list<T> lt)
		{
			swap(_head, lt._head)
				return *this;
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			node* p = _head->_next;
			while (p != _head)
			{
				delete(p);
			}
		}
		void push_back(const T& x)
		{
			//node* tail = _head->_prev;
			//node* newnode = new node(x);
			//tail->_next = newnode;
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//_head->_prev = newnode;
			insert(end(), x);
		}
		void push_front(const T& x);
		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 erase(iterator pos)
		{
			assert(pos != end());
			{
				node* cur = pos._node;
				node* prev = cur->_prev;
				node* next = cur->_next;
				delete cur;
				prev->_next = next;
				next->_prev = prev;
			}
		}
		void pop_back()
		{
			erase(iterator(_head->_prev));
			erase(--end());
		}

	private:
		node* _head;
	};
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值