yolo0616 2021-11-10 17:43 采纳率: 100%
浏览 53
已结题

链表学生成绩排序从大到小 有什么问题


# include<stdio.h>
# include<string.h>
# include<malloc.h>
# include<stdlib.h>
# define M 3
typedef
```c



``` struct student
{
    int score;
    char name[20];
    struct student *next;
}N,*P;
P create(void)
{
     P head=(P)malloc(sizeof(N));
     if(head==NULL)
     {
         printf("off");
         exit(-1);
     }
     head->next=NULL;
     return head;
}
void input(P head,char num[],int n)
{
    P last;
    last=(P)malloc(sizeof(N));
     if(last==NULL)
     {
         printf("off");
         exit(-1);
     }
     strcpy(last->name,num);
     last->score=n;
     last->next=NULL;
     while(head->next!=NULL)
         head=head->next;
     head->next=last;
}
void sort(P head)
{
    P tall;
    P math;
    tall=head;
    math=tall->next;
    while(math->next)
    {
        P a=tall;
        P b=math;
        while(b->next)
        {
        if(b->score<b->next->score)
        {
            a->next=b->next;
            b->next=b->next->next;
            a->next->next=b;
        }
        a=a->next;
        b=a->next;
        }
        head=head->next;
        math=head->next;
    }
}
void show(P head)
{
  while(head->next)
  {
     head=head->next;
     printf("%s %d\n",head->name,head->score);
  }
}
int main(void)
{
   int i;
   N a[M];
   P head=create();
   printf("input message:\n");
   for(i=0;i<M;i++)
   {
     scanf("%s%d",a[i].name,&a[i].score);
     input(head,a[i].name,a[i].score);
   }
    sort(head);
    show(head);
     return 0;


}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月18日
    • 创建了问题 11月10日

    悬赏问题

    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 有没有帮写代码做实验仿真的
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥30 vmware exsi重置后登不上
    • ¥15 c++头文件不能识别CDialog
    • ¥15 Excel发现不可读取的内容
    • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
    • ¥20 yolov5自定义Prune报错,如何解决?
    • ¥15 电磁场的matlab仿真