#include<iostream>
#include<iterator>
#include<stdlib.h>
using namespace std;
template<class _Ty, class _A = allocator<_Ty> >
class list
{
struct _Node;
friend struct _Node;
typedef _POINTER_X(_Node, _A) _Nodeptr;
struct _Node {
_Nodeptr _Next, _Prev;
_Ty _Value;
};
struct _Acc;
friend struct _Acc;
struct _Acc {
typedef _REFERENCE_X(_Nodeptr, _A) _Nodepref;
typedef _A::reference _Vref;
static _Nodepref _Next(_Nodeptr _P)
{return ((_Nodepref)(*_P)._Next); }
static _Nodepref _Prev(_Nodeptr _P)
{return ((_Nodepref)(*_P)._Prev); }
static _Vref _Value(_Nodeptr _P)
{return ((_Vref)(*_P)._Value); }
};
public:
typedef _A::size_type size_type;
public:
explicit list(const _A& _Al = _A())
: allocator(_Al),
_Head(_Buynode()), _Size(0) {}
void push_back(const _Ty &V)
{
_Nodeptr _S = _Buynode(_Head,_Head->_Prev);
_Head->_Prev->_Next = _S;
_Head->_Prev = _S;
allocator.construct(&_S->_Value,V);
_Size++;
}
void push_front(const _Ty &V)
{
_Nodeptr _S = _Buynode(_Head->_Next,_Head);
_Head->_Next->_Prev = _S;
_Head->_Next = _S;
allocator.construct(&_S->_Value,V);
_Size++;
}
void insert(const int i,const _Ty &V) //在i后面插入V
{
_Nodeptr _P = Find(i);
_Nodeptr _S = _Buynode(_P->_Next,_P);
_P->_Next = _S;
_P->_Next->_Prev = _S;
allocator.construct(&_S->_Value,V);
_Size++;
}
int Find(const _Ty _V) //返回V的位置
{
int i = 0;
_Nodeptr _S = _Head->_Next;
while(_S != _Head->_Prev)
{
if(_S->_Value == _V)
return i;
_S = _S->_Next;
i++;
}
if(_S->_Value == _V)
return i;
return -1;
}
_Nodeptr Find(const _Ty &_V)//返回V
{
_Nodeptr _S = _Head->_Next;
while(_S != _Head->_Prev)
{
if(_S->_Value != _V)
_S = _S->_Next;
else
return _S;
}
if(_S->_Value != _V)
return 0;
return _S;
}
void pop_back()
{
_Nodeptr _P = _Head->_Prev;
_P->_Prev->_Next = _Head;
_P->_Next->_Prev = _P->_Prev;
allocator.destroy(&_P->_Value);
allocator.deallocate(_P,_Size);
_Size--;
}
void pop_front()
{
_Nodeptr _P = _Head->_Next;
_P->_Prev->_Next = _P->_Next;
_P->_Next->_Prev = _Head;
allocator.destroy(&_P->_Value);
//allocator.deallocate(_P,1);
_Size--;
}
_Ty find(const _Ty &V)
{
_Nodeptr _P = _Head->_Next;
while(_P != _Head->_Prev)
{
if(_P->_Value != V)
_P = _P->_Next;
else
{
cout<<"Have find!"<<endl;
return V;
}
}
if(_P->_Value != V)
{
cout<<"Not Have the V!"<<endl;
return 0;
}
cout<<"Have find~~~"<<endl;
return V;
}
void remove(const _Ty &V)//将数值为V的所有元素删除
{
_Nodeptr _P = _Head->_Next;
while(_P != _Head->_Prev)
{
if(_P->_Value != V)
_P = _P->_Next;
else
{
erase(_P);
_P = _P->_Next;
}
}
if(_P->_Value == V)
erase(_P);
return;
}
void erase(const _Nodeptr _Q)
{
_Q->_Next->_Prev = _Q->_Prev;
_Q->_Prev->_Next = _Q->_Next;
allocator.destroy(&_Q->_Value);
// allocator.deallocate(_Q,_Size-i);
_Size--;
}
void unique()//移除数值连续相同的元素
{
_Nodeptr _P = _Head->_Next;
while(_P != _Head->_Prev)
{
if(_P->_Value != _P->_Next->_Value)
_P = _P->_Next;
else
{
erase(_P);
_P = _P->_Next;
}
}
}
void clear()
{
_Nodeptr _P = _Head->_Next;
while(_P != _Head)
{
erase(_P);
_P = _P->_Next;
//_P++;
}
_Size = 0;
}
void swap(_Nodeptr _P,_Nodeptr _Q)
{
_Ty _T;
_T = _P->_Value;
_P->_Value = _Q->_Value;
_Q->_Value = _T;
}
void sort() //降序
{
_Nodeptr _S = _Head->_Next;
while(_S != _Head->_Prev)
{
_Nodeptr _P = _S->_Next;
while(_P != _Head->_Next)
{
if(_S->_Value < _P->_Value)
{
swap(_S,_P);
_P = _P->_Next;
}
else
_P = _P->_Next;
}
_S = _S->_Next;
}
}
_Ty count()const
{
cout<<_Size<<endl;
return _Size;
}
void show()
{
_Nodeptr _P = _Head->_Next;
while(_P != _Head)
{
cout<<_P->_Value<<" ";
_P = _P->_Next;
}
cout<<endl;
}
protected:
_Nodeptr _Buynode(_Nodeptr _Narg = 0, _Nodeptr _Parg = 0)
{
_Nodeptr _S = (_Nodeptr)allocator._Charalloc(
1 * sizeof (_Node));
//_Nodeptr _S = (_Nodeptr)allocator.allocate(sizeof(_Node),NULL);
_Acc::_Next(_S) = _Narg != 0 ? _Narg : _S;
_Acc::_Prev(_S) = _Parg != 0 ? _Parg : _S;
return (_S);
}
void _Freenode(_Nodeptr _S)
{allocator.deallocate(_S,0); }
_A allocator;
_Nodeptr _Head;
size_type _Size;
};
void main()
{
list<int> youlist;
// youlist.push_back(rand()%10+1);
youlist.push_front(10);
youlist.push_front(10);
youlist.push_back(rand()%100+1);
youlist.push_front(20);
youlist.push_front(20);
youlist.push_front(70);
youlist.push_front(1);
youlist.push_front(2);
youlist.push_back(2);
// youlist.insert(2,100);
// youlist.pop_front();
youlist.remove(10);
youlist.show();
youlist.count();
//youlist.clear();
youlist.show();
youlist.find(70);
int i = youlist.Find(70);
cout<<i<<endl;
// youlist.clear();
// youlist.sort();
// youlist.show();
// youlist.find(10);
// youlist.Find(10);
// youlist.unique();
// youlist.pop_back();
// youlist.pop_front();
// youlist.sort();
}
list的实现
最新推荐文章于 2024-06-26 11:48:25 发布