c语句 链表冒泡排序
鹤影荷塘 2019-05-19 11:28:07 struct student *paixu (struct student *head){ struct student *endpt; struct student *p; struct student *p1,*p2; int i; printf("请输入1 或2(1=num 2=score)"); scanf("%d",&i); p1=(struct student *)malloc(LEN); p1->next=head; head=p1; for(endpt=NULL;endpt!=head;endpt=p) { for(p=p1=head;p1->next->next!=endpt;p1=p1->next) { if(i==1) { if(p1->next->num>p1->next->next->num) { p2=p1->next->next; p1->next->next=p2->next; p2->next=p1->next; p1->next=p2; p=p1->next->next; } } else if(i==2) { if((p1->next->score)<(p1->next->next->score)) { p2=p1->next->next; p1->next->next=p2->next; p2->next=p1->next; p1->next=p2; p=p1->next->next; } } else { printf("error"); return head; } } } p1=head; head=head->next; free(p1); p1=NULL; return head;};
这个排序似乎不能正确输出,比如第一项是3,之后的排序结果,是以3为第一项的排序,只有大于3的项,没有小于3的排在前面。
谢谢大家