栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

list函数功能实现 超全

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

list函数功能实现 超全

namespace yy{


	template
	struct _list_node
	{
		T _val;
		_list_node* _next;
		_list_node* _prev;

		_list_node(const T& val = T())
			:_val(val)
			, _prev(nullptr)
			, _next(nullptr)
		{}
	};
	//节点


	template
	struct _list_iterartor
	{
		typedef _list_node node;
		typedef _list_iterartor self;

		node* _pnode;

		_list_iterartor(node* pnode)
			:_pnode(pnode)
		{}

		
		Ref operator*()
		{
			return _pnode->_val;
		}

		Ptr operator->()
		{
			return &_pnode->_val;
		}

		bool operator!=(const self& s) const//两个迭代器的比较
		{
			return _pnode != s._pnode;
		}

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

		// ++it -> it.operator++(&it)
		self& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		// it++ -> it.operator++(&it, 0)
		self operator++(int)
		{
			self tmp(*this);
			_pnode = _pnode->_next;
			return tmp;
		}

		self& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		self operator--(int)
		{
			self tmp(*this);
			_pnode = _pnode->_prev;
			return tmp;
		}
	};

	//迭代器







	template
	class list
	{
		typedef _list_node node;



	public:


		typedef _list_iterartor iterator;
		typedef _list_iterartor 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& lt)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			for (const auto& e : lt)
			{
				push_back(e);
			}


		}

		//copy = lt1;
		list& operator=(const list& lt)
		{
			if (this != <)
			{
				clear();
				for (const auto& e : lt)
				{
					push_back(e);
				}
			}

			return *this;
		}

		// copy = lt1;
		list& operator=(list lt)
		{
			swap(_head, lt._head);

			return *this;
		}

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


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


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

		void insert(iterator pos, const T& x)
		{
			assert(pos._pnode);

			node* cur = pos._pnode;
			node* prev = cur->_prev;
			node* newnode = new node(x);

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

		iterator erase(iterator pos)
		{
			assert(pos._pnode);
			assert(pos != end());

			node* prev = pos._pnode->_prev;
			node* next = pos._pnode->_next;

			delete pos._pnode;
			prev->_next = next;
			next->_prev = prev;

			return iterator(next);
		}

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

		size_t size()
		{
			size_t sz = 0;
			iterator it = begin();
			while (it != end())
			{
				++sz;
				++it;
			}

			return sz;
		}
	private:
		node* _head;
		//size_t _size;
	};

	void PrintList(const list& lt)
	{
		list::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			// *it += 1; // ?
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	class Date
	{
	public:
		int _year = 0;
		int _month = 1;
		int _day = 1;
	};

	void test_list1()
	{
		list lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

		list::iterator it = lt.begin();
		while (it != lt.end())
		{
			*it += 1;
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		PrintList(lt);
	}

	void test_list2()
	{
		list lt;
		lt.push_back(Date());
		lt.push_back(Date());
		lt.push_back(Date());

		list::iterator it = lt.begin();
		while (it != lt.end())
		{
			//cout << (*it)._year << " " << (*it)._month <<" " <<(*it)._day<_year << " " << it->_month << " " << it->_day << endl;

			++it;
		}
		cout << endl;

		Date d;
		Date* p = &d;

		(*p)._year = 100;
		p->_year = 100;
	}

	void test_list3()
	{
		list lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		PrintList(lt);

		list copy(lt);
		PrintList(copy);

		list lt1;
		lt1.push_back(10);
		lt1.push_back(20);
		copy = lt1;
		PrintList(copy);
		PrintList(lt1);



		lt.clear();
		PrintList(lt);
	}









}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/702848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号