迭代器实现

#ifndef XING_LIST_H
#define XING_LIST_H

namespace xing
{
template <class _Ty>
class list
{
private:
	struct _Node;
	typedef struct _Node* _Nodeptr;
	struct _Node
	{
		_Nodeptr _Prev, _Next;
		_Ty _Value;
	};
	
	struct _Acc;
	struct _Acc
	{
		typedef  _Ty& _Vref;
		typedef struct _Node*& _Nodepref;
		static _Vref _Value(_Nodeptr _P)
		{
			return (*_P)._Value;
		}
		static _Nodepref _Prev(_Nodeptr _P)
		{
			return (*_P)._Prev;
		}
		static _Nodepref _Next(_Nodeptr _P)
		{
			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;

class iterator;
class const_iterator;

class const_iterator
{
protected:
	_Nodeptr _Ptr;
public:
	const_iterator(_Nodeptr _P) :_Ptr(_P) {}
	const_reference operator *()const
	{
		return _Acc::_Value(_Ptr);
	}
	const_pointer operator->()const
	{
		return &**this;
	}
	const_iterator& operator++()
	{
		return _Acc::_Next(_Ptr);
	}
	const_iterator operator++(int)
	{
		iterator old = *this;
		++* this;
		return old;
	}
	const_iterator& operator--()const
	{
		return _Acc::_Prev(_Ptr);
	}
	const_iterator operator--(int)
	{
		iterator old = *this;
		--* this;
		return old;
	}
	bool operator == (const const_iterator& _X)const
	{
		return _Ptr == _X._Ptr;
	}
	bool operator != (const const_iterator& _X)const
	{
		return !(*this == _X);
	}
    _Nodeptr _Mynode()const
	{
		return _Ptr;
	}
};
class iterator :public const_iterator
{
public:
	iterator(_Nodeptr _P = NULL) :const_iterator(_P) {};
	typedef const_iterator Base;
	reference operator *()const
	{
		return _Acc::_Value(Base::_Ptr);
	}
	pointer operator->()const
	{
		return &**this;
	}
	iterator& operator++()
	{
		Base::_Ptr = _Acc::_Next(Base::_Ptr);
		return *this;
	}
	iterator operator++(int)
	{
		iterator old = *this;
		++* this;
		return old;
	}
	iterator& operator--()
	{
		Base::_Ptr = _Acc::_Prev(Base::_Ptr);
		return *this;
	}
	iterator operator--(int)
	{
		iterator old = *this;
		--* this;
		return old;
	}
	bool operator == (const iterator& _X)const
	{
		return Base::_Ptr == _X.Base::_Ptr;
	}
	bool operator != (const iterator& _X)const
	{
		return !(*this == _X);
	}
};
	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:
	list() :_Head(_BuyNode()), _Size(0) {};
	~list()
	{
		clear();
		_Freenode(_Head);
	}
	void push_front(const _Ty& val)
	{
		insert(begin(), val);
	}
	void push_back(const _Ty& val)
	{
		insert(end(), val);
	}
	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;
		//_Acc::_Value(_S) = val;
		new(&_Acc::_Value(_S))_Ty(val);
		_Size += 1;
		return _S;
	}
	iterator erase(iterator _P)
	{
		_Nodeptr _S = _P++._Mynode();
		_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;
	}
	void clear()
	{
		while (begin() != end())
		{
			erase(begin()++);
		}
	}
	void remove()
	{

	}
private:
	_Nodeptr _Head;
	size_type _Size;
private:
	_Nodeptr _BuyNode(_Nodeptr Narg = NULL, _Nodeptr Parg = NULL)
	{
		_Nodeptr _S = (_Nodeptr)malloc(sizeof(_Node));
		_S->_Prev = Narg == NULL ? _S : Narg;
		_S->_Next = Parg == NULL ? _S : Parg;
		return _S;
	}
	void _Freenode(_Nodeptr _P)
	{
		free(_P);
	}
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值