循环双向链表的增删查改等基本操作
#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
struct ListNode
{
DataType _data;
ListNode* _prev;
ListNode* _next;
ListNode(const DataType& x)
:_data(x)
,_prev(NULL)
,_next(NULL)
{}
ListNode()
:_data(0)
,_prev(NULL)
,_next(NULL)
{}
};
class List
{
private:
ListNode _head;
public:
List()
{
_head._prev=&_head;
_head._next=&_head;
}
~List()
{
clear();
}
void clear()
{
assert(_head._next);
ListNode* cur=_head._next;
while(cur!=&_head)
{
ListNode* del=cur;
cur=cur->_next;
delete del;
del=NULL;
}
}
void PushBack(const DataType& x)
{
ListNode* _tail=_head._prev;
ListNode* tmp=new ListNode(x);
_tail->_next=tmp;
tmp->_prev=_tail;
tmp->_next=&_head;
_head._prev=tmp;
}
void PopBack()
{
assert(_head._next);
if(_head._next==_head._prev)
{
delete _head._next;
_head._next=&_head;
}
else
{
ListNode* _cur=_head._prev;
ListNode* _tail=_cur->_prev;
_tail->_next=&_head;
_head._prev=_tail;
delete _cur;
_cur=NULL;
}
}
void PushFront(const DataType& x)
{
ListNode* tmp=new ListNode(x);
ListNode* cur=_head._next;
_head._next=tmp;
tmp->_prev=&_head;
if(cur==&_head)
{
_head._prev=tmp;
tmp->_next=&_head;
}
else
{
cur->_prev=tmp;
tmp->_next=cur;
}
}
void PopFront()
{
assert(_head._next);
ListNode *cur=_head._next;
_head._next=cur->_next;
cur->_prev=&_head;
delete cur;
cur=NULL;
}
ListNode* Find(const DataType& x)
{
assert(_head._next);
ListNode* cur=_head._next;
while(cur!=&_head)
{
if(cur->_data!=x)
{
cur=cur->_next;
}
else
{
return cur;
}
}
cout<<"x不存在"<<endl;
return NULL;
}
void Erase(ListNode* pos)
{
assert(pos);
ListNode* prev=pos->_prev;
ListNode* next=pos->_next;
if(pos->_prev==pos->_next)
{
_head._next=&_head;
_head._prev=&_head;
}
else
{
prev->_next=next;
next->_prev=prev;
}
}
void Remove(const DataType& x)
{
assert(_head._next);
ListNode* cur=_head._next;
while(cur!=&_head)
{
if(cur->_data!=x)
{
cur=cur->_next;
}
else
{
ListNode* prev=cur->_prev;
ListNode* next=cur->_next;
if(cur->_prev==cur->_next)
{
_head._next=&_head;
_head._prev=&_head;
return;
}
else
{
prev->_next=next;
next->_prev=prev;
return;
}
}
}
cout<<"x不存在"<<endl;
return;
}
void Print()
{
assert(_head._next);
ListNode *cur=_head._next;
while(cur!=&_head)
{
cout<<cur->_data<<"->";
cur=cur->_next;
}
cout<<"Tail"<<endl;
}
};
void Test1()
{
List l;
//l.PushBack(1);
//l.PushBack(2);
//l.PushBack(3);
////l.Print();
//l.PopBack();
//l.PopBack();
//
//l.PopBack();
//l.PopBack();
//l.Print();
l.PushFront(1);
l.PushFront(2);
l.PushFront(3);
//l.Print();
l.PopFront();
l.PopFront();
l.PopFront();
l.PopFront();
l.Print();
}
void Test2()
{
List l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
//ListNode* pos=l.Find(1);
//ListNode* pos=l.Find(2);
ListNode* pos=l.Find(4);
l.Erase(pos);
l.Print();
}
void Test3()
{
List l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.Remove(1);
l.Remove(2);
l.Remove(3);
l.Remove(4);
l.Print();
}
int main()
{
//Test1();
//Test2();
Test3();
system("pause");
return 0;
}本文出自 “sunshine225” 博客,请务必保留此出处http://10707460.blog.51cto.com/10697460/1754735
原文:http://10707460.blog.51cto.com/10697460/1754735