STL链表

#ifndef ZJH_LIST_H
#define ZJH_LISH_H
#include<iostream>
using namespace std;
namespace yhp
{
	template<class T>
	void Swap(T& a, T& b)
	{
		T tmp = a;
		a = b;
		b = tmp;
	}
	template<class _Ty>
	class list
	{
	protected:
		struct _Node;
		typedef struct _Node* _Nodeptr;
		struct _Node
		{
			_Nodeptr _Prev, _Next;
			_Ty _Value;
		};
		struct _Acc;
		struct _Acc
		{
			typedef struct _Node*& _Nodepref;
			typedef _Ty& _Vref;
			static _Vref _Value(_Nodeptr _P)
			{
				return (*_P)._Value;
			}
			static _Nodepref _Prev(_Nodeptr _P)
			{
				return (*_P)._Prev;
			}
			static _Nodepref _Next(_Nodeptr _P)
			{
				return (*_P)._Next;//return _P->Next;
			}
		};
	public:
		typedef _Ty  value_type;
		typedef _Ty& reference;
		typedef const _Ty& const_reference;
		typedef _Ty* pointer;
		typedef const _Ty* const_pointer;
		typedef size_t  size_type;
		typedef int  difference_type;
	public:
		class const_iterator
		{
		protected:
			_Nodeptr _Ptr;
		public:
			const_iterator(_Nodeptr _P = NULL) :_Ptr(_P) {}
			const_reference operator*()const
			{
				return _Acc::_Value(_Ptr);
			}
			const_pointer operator->()const
			{
				return &**this;
			}
			const_iterator& operator++()
			{
				_Ptr = _Acc::_Next(_Ptr);
				return *this;
			}
			const_iterator operator++(int)
			{
				const_iterator tmp = *this;
				++* this;
				return tmp;
			}
			const_iterator operator--()
			{
				_Ptr = _Acc::_Prev(_Ptr);
				return *this;
			}
			const_iterator operator--(int)
			{
				const_iterator tmp = *this;
				--* this;
				return tmp;
			}
			bool operator==(const const_iterator& _X)const
			{
				return this->_Ptr == _X._Ptr;
			}
			bool operator!=(const const_iterator& _X)const
			{
				return !(*this == _X);
			}
			_Nodeptr _Mynode()const
			{
				return _Ptr;
			}
		};



		class iterator :public const_iterator
		{
			typedef const_iterator base;
		public:
			iterator(_Nodeptr _P = NULL) :const_iterator(_P) {}
			iterator& operator++()
			{
				base::_Ptr = _Acc::_Next(base::_Ptr);
				return *this;
			}
			iterator operator++(int)
			{
				iterator tmp = *this;
				++* this;
				return tmp;
			}
			iterator& operator--()
			{
				base::_Ptr = _Acc::_Prev(base::_Ptr);
				return *this;
			}
			iterator operator--(int)
			{
				iterator tmp = *this;
				--* this;
				return tmp;
			}
			bool operator==(const iterator& _X)const
			{
				return this->_Ptr == _X._Ptr;
			}
			bool operator!=(const iterator& _X)const
			{
				return !(*this == _X);
			}
		};
	public:
		iterator begin()
		{
			return iterator(_Acc::_Next(_Head));
		}
		iterator end()
		{
			return iterator(_Head);
		}
		const_iterator begin()const
		{
			return const_iterator(_Acc::_Next(_Head));
		}
		const_iterator end()const
		{
			return const_iterator(_Head);
		}
	public:
		typedef const_iterator _It;
		list() :_Head(_Buynode()), _Size(0) {}
		list(std::initializer_list<_Ty>list) :list()
		{
			for (auto& x : list)
			{
				push_back(x);
			}
		}
		list(size_t count, const _Ty& val) :_Head(_Buynode()), _Size(0)
		{
			insert(begin(), count, val);
		}
		list(const _Ty* _F, const _Ty* _L) :_Head(_Buynode()), _Size(0)
		{
			insert(begin(), _F, _L);
		}
		list(const list& _X) :_Head(_Buynode()), _Size(0)
		{
			insert(begin(), _X.begin(), _X.end());
		}
		list& operator=(const list _X)
		{
			if (this == &_X)return *this;
			iterator _F1 = begin();
			iterator _L1 = end();
			const_iterator _F2 = _X.begin();
			const_iterator _L2 = _X.end();
			for (; _F1 != _L1 && _F2 != _L2; ++_F1, ++_F2)
			{
				*_F1 = *_F2;
			}
			earse(_F1, _L1);
			insert(_L1, _F2, _L2);
			return *this;
		}
		~list()
		{
			clear();
			_Freenode(_Head);
		}
		void push_front(const _Ty& val)
		{
			insert(begin(), val);
		}
		void push_back(const _Ty& val)
		{
			insert(end(), val);
		}
		void insert(iterator _P, const _Ty* _F, const _Ty* _L)
		{
			for (; _F != _L; ++_F)
			{
				insert(_P, *_F);
			}
		}
		void insert(iterator _P, size_t count, const _Ty& val)
		{
			while (count--)
			{
				insert(_P, val);
			}
		}
		void insert(iterator _P, _It _F, _It _L)
		{
			for (; _F != _L; ++_F)
			{
				insert(_P, *_F);
			}
		}
		void insert(iterator _P, std::initializer_list<_Ty>list)
		{
			for (auto& x : list)
			{
				insert(_P, x);
			}
		}
		iterator insert(iterator _P, const _Ty& val)
		{
			_Nodeptr _S = _P._Mynode();
			_Acc::_Prev(_S) = _Buynode(_Acc::_Prev(_S), _S);
			_S = _Acc::_Prev(_S);
			_Acc::_Next(_Acc::_Prev(_S)) = _S;
			new(&_Acc::_Value(_S))_Ty(val);
			_Size += 1;
			return iterator(_S);
		}
		void pop_front()
		{
			erase(begin());
		}
		void pop_back()
		{
			erase(--end());
		}
		void erase(iterator _F, iterator _L)
		{
			for (; _F != _L;)
			{
				erase(_F++);
			}
		}
		void clear()
		{
			erase(begin(), end());
		}
		void remove(const _Ty& val)
		{
			iterator _P = begin(), _L = end();
			for (; _P != end(); _P++)
			{
				if (*_P == val)
				{
					erase(_P);
					break;
				}
			}
		}
		void remove_all(const _Ty& val)
		{
			iterator _F = begin(), _L = end();
			while (_F != _L)
			{
				if (*_F == val)
				{
					erase(_F++);
				}
				else
				{
					++_F;
				}
			}
		}
		iterator erase(iterator _P)
		{
			_Nodeptr _S = _P._Mynode();
			//_Nodeptr _S=_P.operator++(0)._Mynode();
			// 这里是运算符重载的函数,不是单纯的++
			//_tmp  其实是临时量_tmp调动的_Mynode
			//_P
			_Acc::_Prev(_Acc::_Next(_S)) = _Acc::_Prev(_S);
			_Acc::_Next(_Acc::_Prev(_S)) = _Acc::_Next(_S);
			(&_Acc::_Value(_S))->~_Ty();
			_Freenode(_S);
			_Size -= 1;
			return _P;
			//此时返回的_P其实是一个空指针,为了解决这个问题
		}
		//void Swap(list _P)
		//{
		//	//空间收缩
		//	yhp::Swap(this->)
		//}
	private:
		_Nodeptr _Buynode(_Nodeptr _Parg = NULL, _Nodeptr _Narg = NULL)
		{
			_Nodeptr _S = (_Nodeptr)malloc(sizeof(_Node));
			_Acc::_Prev(_S) = _Parg == NULL ? _S : _Parg;
			_Acc::_Prev(_S) = _Narg == NULL ? _S : _Narg;
			return _S;
		}
		void _Freenode(_Nodeptr _P)
		{
			free(_P);
		}
		_Nodeptr _Head;
		size_type _Size;
	};
}
//自定义类型的运算符是按照函数规则来进行的,和内置类型不一样
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值