#include<stdio.h> #include<stdlib.h> typedef struct student{ int id; int score; struct student *next; }Stu; Stu *create_stu(int n){ Stu *head=(Stu *)malloc(sizeof(Stu)); if(head==NULL){ return NULL; } head->id=-1; head->score=-1; head->next=NULL; Stu *tmp=head; for(int i=0;i<n;i++){ Stu *newNode=(Stu* ) malloc (sizeof(Stu)); if(newNode==NULL){ return NULL; } scanf("%d %d",&newNode->id,&newNode->score); newNode->next=NULL; tmp->next=newNode; tmp=newNode; } return head; } Stu *merge_stu(Stu *s1,Stu *s2){ if(s1==NULL||s2==NULL){ return NULL; } Stu *tmp1=s1; Stu *tmp2=s2; while(tmp1->next!=NULL){ tmp1=tmp1->next; } tmp1->next=tmp2->next; free(tmp2); return s1; } void sort_Stu(Stu *students){ if(students==NULL){ return; } Stu *pre=NULL; Stu *cur=NULL; Stu tmp; for(pre=students->next;pre->next!=NULL;pre=pre->next){ for(cur=pre->next;cur!=NULL;cur=cur->next){ if(pre->id>cur->id){ tmp=*pre; *pre=*cur; *cur=tmp; tmp.next=pre->next; pre->next=cur->next; cur->next=tmp.next; } } } } void print_stu(Stu *students){ if(students==NULL){ printf("invalid list!\n"); return; } Stu *cur=students->next; while(cur!=NULL){ printf("%d %d\n",cur->id,cur->score); cur=cur->next; } } void destroy_stu(Stu *students){ if(students==NULL){ return; } Stu *s=students; Stu *tmp; while(s!=NULL){ tmp=s->next; free(s); s=tmp; } } int main() { int N,M; scanf("%d %d",&N,&M); Stu *s1=create_stu(N); Stu *s2=create_stu(M); Stu *students=merge_stu(s1,s2); sort_Stu(students); print_stu(students); destroy_stu(students); return 0; }
0.0分
0 人评分
简单的a+b (C语言代码)浏览:585 |
C语言训练-计算t=1+1/2+1/3+...+1/n (C语言代码)浏览:539 |
C二级辅导-等差数列 (C语言代码)浏览:628 |
时间转换 (Java代码)浏览:617 |
分糖果 (C++代码)浏览:1537 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:690 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:1015 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1482 |
DNA (C语言描述,蓝桥杯)浏览:1653 |
DNA (C语言代码)浏览:837 |