#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 人评分