链表的冒泡排序
#include"stdio.h"struct worker
{
int num;
int wage;
struct worker *next;
};
struct worker *head;
void sort()
{
printf("排序:\n");
struct worker *p1=head,*p2;
struct worker *pEnd=NULL;
p2=p1->next ;
do{
if(p1->num >p2->num )
{
head=p2;
p1->next =p2->next ;
p2->next =p1;
}
else
{
struct worker *temp;
while(p1->next->next !=pEnd)
{
if(p1->next->num >p2->next->num )
{
temp=p2->next->next ;
p2->next->next =p1->next ;
p1->next =p2->next ;
p2->next =temp;
}
p1=p1->next ;
p2=p1->next ;
}
pEnd=p1->next ;
}
p1=head;p2=p1->next ;
}while(p1!=pEnd);
}
void create()
{
worker *p1,*p2;
head=p2=p1=new worker;
printf("请输入信息:\n");
scanf("%d%d",&p1->num ,&p1->wage );
p1->next =NULL;
while(p1->num !=0)
{
p1=new worker;
scanf("%d%d",&p1->num ,&p1->wage );
p2->next =p1;
p2=p1;
}
p2->next =NULL;
}
void display()
{
worker *p=head;
while(p->next !=NULL)
{
printf("编号:%d 工资:%d\n",p->num ,p->wage );
p=p->next ;
}
}
void main()
{
create();
display();
sort();
display();
}
对创建的链表的成员按照序号排列,创建成功,不能排序,好像进入死循环
看了快一天了 还是找不出问题出在哪,求大神指点