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*
typedef _Ty
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;
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
}
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);
_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;
};
}