朱扬宇


私信TA

用户名:uq_98682061445

访问量:186

签 名:

3212052051354

等  级
排  名 4264
经  验 1658
参赛次数 0
文章发表 9
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

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

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区