链表排序

来源:百度知道 编辑:UC知道 时间:2024/05/28 04:29:55
struct student *Sort(struct student *,char *order)
{
struct student *p,*q;
p=q=(struct student *)malloc(LEN);
p=head;
if(head!=NULL)
switch(*order)
{
case 'name': do
{ if(strcmp(p->name,p->next->name)>0) {q=p;p=p->next;p->next=q;} p=p->next;
}while(p->next==NULL);
break;
case 'num': do { if(p->num>p->next->num) {q=p;p=p->next;p->next=q;} p=p->next;
}while(p->next==NULL);
break;
case 'age': do { if(p->age>p->next->age) {q=p;p=p->next;p->next=q;} p=p->next;
}while(p->next==NULL);break;
}
if(p->next==NULL)
switch(*order)
{
case 'name': if(strcmp(p->n

帅哥,你的写法很别致,语法上
case 'name':
case 'num':
case 'age':
是没有问题的,但是你有没有想过这些字符到底是什么?还有swtich一定要有default!这是代码健壮性问题!

其他代码我没有太仔细看,这里很有意思~~