node* next;
while (pcur !=NULL)
{
if(!pcur->next)
{
pcur->next=prev;
//最后一个节点的下一个节点指向前一个节点
break;
}
next = pcur->next;
//下一个节点
pcur->next=prev;
//修改当前节点的下一个节点
pcur->prev=next;
//修改当前及诶单的上一个节点
prev=pcur;
//将当前节点设为上一个节点
pcur=next;
//将下一个节点设为当前节点
}
cur=head->next;
//末节点指向头节点
head->next=pcur;
//头指针指向当前节点,也就是指向翻转之前的末节点
}
//排序
//节点之间直接交换数据,而没有用修改指针指向
void sort()
{
if(empty())
{
return;
}
node *p,*t;
p=head->next;
T d;
while(p!=NULL)
{
t=p;
while(t!=NULL)
{
if(p->data>t->data)
{
d=p->data;
p->data=t->data;
t->data=d;
}
t=t->next;
}
p=p->next;
}
}
//链表元素个数
int size()
{
return len;
}
void resize(int count)
{
resize(count,0);
}
//重新设置元素
void resize(int count,T t)
{
if(count<0)
{
throw new exception("元素的个数不能小于0!");
}
clear(); //内存是必须释放的
while(count--)
{
push_front(t);
}
}
//清空集合
void clear()
{
if(empty())
{
return;
}
node *tmp=head;
node *t=tmp->next;
while(t!=NULL)
{
tmp=t;
t=t->next;
delete tmp;
}
tmp=NULL;
t=NULL;
cur=head;//当前节点指向头指针
}
//链表是否为空
bool empty() const
{
return len==0;
}
//判断链表是否为空
void checkEmpty()
{
if(empty())
{
throw new exception("集合中没有元素!");
}
}
private : typedef struct node1
{
node1 *prev,*next;
T data;
node1(T t):data(t),prev(NULL),next(NULL){}
}
node;
node* head;//头节点
node* cur; //当前节点,指向末节点
int len; //节点个数
void initialize()
{
cur=head=new node(-1);
len=0;
}};