c++创建双循环链表(附加迭代器)

namespace yhp
{
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;    //*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(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()
	{
		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);
		}
	}
	iterator insert(iterator _P, const _Ty& val)   //在指定结点前插入结点
	{
		_Nodeptr _S = _P._Mynode();
		_Acc::_Prev(_S) = _Buynode(_Acc::_Prev(_S), _S);  //让传进来结点的前驱等于构造的结点
		_S = _Acc::_Prev(_S);    //_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());
	}
	iterator erase(iterator _P)
	{

	}
private:
	_Nodeptr _Buynode(_Nodeptr _Parg = NULL, _Nodeptr _Narg = NULL)  //扩容函数
	{
		_Nodeptr _S = (_Nodeptr)malloc(sizeof(_Node));
		_Acc::_Prev(_S) = _Parg == NULL ? _S : _Parg;   
		_Acc::_Next(_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、付费专栏及课程。

余额充值