#include <iostream>
#include <cstring>
#include <typeinfo>
#include <stdlib.h>
#include <assert.h>
#include <iterator>
using namespace std;
namespace my{
template<class _Ty>
_Ty* _Allocate(size_t _N, _Ty*){
if (_N < 0)
_N = 0;
return ((_Ty *)operator new((size_t)_N * sizeof (_Ty)));
}
template<class _T1, class _T2>
void _Construct(_T1* _P, const _T2& _V){
new ((void*)_P) _T1(_V);
}
template<class _Ty>
void _Destroy(_Ty* _P){
_P->~_Ty();
}
template<class _Ty>
class myallocator{
public:
typedef size_t size_type;
typedef size_t difference_type;
typedef _Ty* pointer;
typedef const _Ty* const_pointer;
typedef _Ty& reference;
typedef const _Ty& const_reference;
typedef _Ty value_type;
pointer allocate(size_type _N, const void *){
return (_Allocate((difference_type)_N, (pointer)0));
}
char* _Charalloc(size_type _N){
return (_Allocate((difference_type)_N,(char*)0));
}
void deallocate(void* _P, size_type){
operator delete(_P);
}
void construct(pointer _P, const _Ty& _V){
_Construct(_P, _V);
}
void destroy(pointer _P){
_Destroy(_P);
}
};
template<class _Ty,class _A=myallocator<_Ty>>
class list{
public:
typedef _Ty value_type;
typedef _Ty* pointer_type;
typedef _Ty& reference_value;
typedef const _Ty* const_pointer_type;
typedef const _Ty& const_reference_value;
typedef size_t size_type;
public:
struct _Node;
typedef _Node* _Nodeptr;
struct _Node{
_Ty _Value;
_Nodeptr _Next;
_Nodeptr _Prev;
};
public:
class iterator{
public:
iterator(_Nodeptr _P):_Ptr(_P){
}
bool operator!=(const iterator &it){
return !(it._Ptr==_Ptr);
}
bool operator==(const iterator &it){
return (it._Ptr==_Ptr);
}
_Ty& operator*(){
return (_Ptr->_Value);
}
iterator& operator++(){
_Ptr=_Ptr->_Next;
return *this;
}
iterator operator++(int){
iterator tmp=*this;
_Ptr=_Ptr->_Next;
return tmp;
}
_Nodeptr _Mynode(){
return _Ptr;
}
private:
_Nodeptr _Ptr;
};
public:
list():_Head(_Buynode()),_Size(0)
{}
public:
iterator begin(){
return iterator(_Head->_Next);
}
iterator end(){
return iterator(_Head);
}
public:
void push_back(const _Ty &x){
_Nodeptr _S=_Buynode(_Head,_Head->_Prev);
_S->_Value=x;
_Head->_Prev->_Next=_S;
_Head->_Prev=_S;
_Size++;
}
void push_front(const _Ty &x){
_Nodeptr _S=_Buynode(_Head->_Next,_Head);
_S->_Value=x;
_Head->_Next->_Prev=_S;
_Head->_Next=_S;
_Size++;
}
void push_front_0(const _Ty &x){
insert(begin(),x);
}
void PrintList(){
_Nodeptr p=_Head->_Next;
while(p!=_Head){
cout<<p->_Value<<"->";
p=p->_Next;
}
cout<<"Over"<<endl;
}
iterator insert(iterator _P,const _Ty& _X=_Ty()){
_Nodeptr _S=_P._Mynode();
_S->_Prev=_Buynode(_S,_S->_Prev);
_S=_S->_Prev;
_S->_Prev->_Next=_S;
allocator.construct(&(_S->_Value),_X);
++_Size;
return (iterator(_S));
}
protected:
_Nodeptr _Buynode(_Nodeptr _Narg=0,_Nodeptr _Parg=0){
_Nodeptr _S=(_Nodeptr)allocator._Charalloc(sizeof(_Node));
assert(_S!=nullptr);
_S->_Value=0;
_S->_Next=_Narg!=0?_Narg:_S;
_S->_Prev=_Parg!=0?_Parg:_S;
return _S;
}
private:
_Nodeptr _Head;
size_type _Size;
_A allocator;
};
};
int main(){
my::list<int> mylist;
mylist.push_back(1);
mylist.push_front_0(0);
auto pos=mylist.begin();
mylist.insert(pos,2);
my::list<int>::iterator it=mylist.begin();
while(it!=mylist.end()){
cout<<*it<<"->";
++it;
}
cout<<"Over."<<endl;
return 0;
}