什么都说 直接上代码
#define DataType int
struct Node
{
Node(const DataType& data = 0)
{}
Node* _pNext;
Node* _pPre; //双向链表
DataType _data;
};
class List
{
public:
List()
:_pHead(NULL)
,_pTail(NULL)
,_size(0)
{}
List(DataType arr[], size_t size)
:_pHead(NULL)
,_pTail(NULL)
,_size(size)
{
size_t i = 0;
for(i = 0; i<size; i++)
{
PushBack(arr[i]);
}
}
//_BuyNode(arr);
//_pHead = new Node;
//_pHead ->_data = arr;
//_pTail = _pHead;
//_size = size;
List(const List& l)
{
Node* ptemp_h = new Node;
Node* ptemp_t = new Node;
ptemp_h = l._pHead;
ptemp_t = l._pTail;
delete _pHead;
delete _pTail;
//_pHead = new Node;//(l._pHead);
//_pTail = new Node;//(l._pTail);
_pHead = ptemp_h;
_pTail = ptemp_t;
}
List& operator=(const List& l)
{
if(this != &l)
{
Node* ptemp_h = new Node;
Node* ptemp_t = new Node;
ptemp_h = l._pHead;
ptemp_t = l._pTail;
delete _pHead;
delete _pTail;
//_pHead = new Node;//(l._pHead);
//_pTail = new Node;//(l._pTail);
_pHead = ptemp_h;
_pTail = ptemp_t;
_size = l._size;
return *this;
}
return *this;
}
~List()
{
if((NULL != _pHead)&&(NULL != _pTail))
{
delete _pHead;
delete _pTail;
_pHead = NULL;
_pTail = NULL;
--_size;
}
}
void PushBack(const DataType& data)
{
if(/*(NULL == _pHead)&&*/(NULL == _pTail))
{
_pTail = _BuyNode(data) ;
_pHead = _pTail;
_pHead->_pPre = NULL;
_pTail->_pNext = NULL;
}
else
{
Node* pnewnode = _BuyNode(data);
_pTail->_pNext = pnewnode;
pnewnode->_pPre = _pTail;
pnewnode->_pNext = NULL;
_pTail = pnewnode;
}
++_size;
}
void PopBack()
{
//if(NULL == _pTail)
//{
// return ;
//}
if(Empty())
{
assert(false);
return ;
}// if 和 assert 的区别, 如何判断。
else if(_pHead == _pTail)
{
delete[] _pHead;
_pHead = NULL;
_pTail = NULL;
--_size;
}
else
{
delete[] _pTail;
//--_pTail->_pNext = NULL;
//_pTail->_pPre = --_pTail;
//_pTail->_pNext = ++_pTail;
_pTail = _pTail->_pPre;
delete _pTail->_pNext; //???????????????????????????????
_pTail->_pNext = NULL;
--_size;
}
}
void PushFront(const DataType& data)
{
if(Empty())
{
_pHead = _BuyNode(data);
_pTail = _pHead;
++_size;
}
else
{
Node* pnewnode = _BuyNode(data);
_pHead->_pPre = pnewnode;
pnewnode->_pNext = _pHead;
pnewnode->_pPre =NULL;
_pHead = pnewnode;
++_size;
}
}
void PopFront()
{
if(Empty())
{
assert(false);
return ;
}
else if(_pHead == _pTail)
{
delete _pHead;
_pHead = NULL;
_pTail = NULL;
--_size;
}
else
{
_pHead = _pHead->_pNext;
delete _pHead->_pPre;
_pHead->_pPre = NULL;
--_size;
}
}
bool Empty()const
{
return (0 == _size);
}
Node* Find(const DataType& data)const
{
Node* pcur = _pHead;
while(pcur)
{
if(data == pcur->_data)
{
return pcur;
}
else
{
pcur = pcur->_pNext;
}
return pcur;
}
}
void Insert(Node* pos, const DataType& data)
{
if(Empty())
{
PushFront(data);
//++_size;
}
else if(NULL != (_pHead == _pTail))
{
PushBack(data);
//++_size;
}
else
{
Node* pnewnode = _BuyNode(data);
pnewnode->_pNext = pos->_pNext;
pnewnode->_pPre = pos;
pos->_pNext = pnewnode;
pnewnode->_pNext->_pPre=pnewnode;
++_size;
}
}
void Erase(Node* pos)
{
if(Empty())
{
assert(false);
return ;
}
else if(pos->_pNext == _pHead->_pNext)
{
PopBack();
}
else
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
delete pos;
pos->_pNext =NULL;
pos->_pPre = NULL;
}
}
void Remove(const DataType& data)
{
Node* ptemp = _pHead;
while(_pHead)
{
if(data == _pHead->_data)
{
Erase(_pHead);
}
_pHead = _pHead->_pNext;
}
_pHead = ptemp;
}
size_t Size()const
{
return _size;
}
void Clear()
{
if(0==_size)
{
return ;
}
else
{
Node* pdel = _pHead;
Node* pcur = _pHead->_pNext;
while(pcur->_pNext)
{
pcur = pcur->_pNext;
pdel = pcur->_pPre;
delete pdel;
}
delete pcur;
_pTail = NULL;
pdel =_pHead;
delete pdel;
_pHead = NULL;
_size = 0;
}
}
Node& Front()
{
return *_pHead;
}
const Node& Front()const
{
return *_pHead;
}
Node& Back()
{
return *_pTail;
}
Node& Back()const
{
return *_pTail;
}
// ostream& operator <<(ostream& _cout,const List& l)
//{
// Node* pcur = l._pHead;
// while(pcur)
// {
// _cout << pcur->_data;
// pcur = pcur->_pNext;
// }
//return _cout;
//}
private:
Node* _BuyNode(const DataType& data)
{
//return new Node(data); //注意初始化
Node *newnode = new Node;
newnode->_data = data;
return newnode;
}
friend ostream& operator<<(ostream& _cout, const List& l);
private:
Node* _pHead;
Node* _pTail;
size_t _size;
};
ostream& operator <<(ostream& _cout,const List& l)
{
Node* pcur = l._pHead;
while(pcur)
{
_cout << pcur->_data;
pcur = pcur->_pNext;
}
return _cout;
}
int main()
{
int arr[] ={1,2,3};
List l1(arr,3);
l1.PushBack(4);
l1.PushBack(5);
cout<<l1<<endl;
l1.Clear();
cout<<l1<<endl;
return 0;
}