uq_65552722759


私信TA

用户名:uq_65552722759

访问量:133

签 名:

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

  自我简介:

TA的其他文章

解题思路:
手搓链表,首先输入a,b链表,初始化一个together链表备用。

写一个函数minid找到链表中id最小的并返回该结点地址,考察a,b两链表中id最小者,并加入together链表同时修改该id为999,循环。
注意事项:
找到了当前id最小者结点地址要加入together链表时,并不能直接链接该地址,而是要拷贝一份,并且该拷贝的next指针为NULL

参考代码:

#include<stdio.h>

#include<stdlib.h>

typedef struct student{

    int id;

    int score;

    struct student* next;

}Snode;

Snode* init(){

    Snode* temp=(Snode* )malloc(sizeof(Snode));

    temp->next=NULL;

    return temp;

}

void printnodes(Snode* x){

    Snode* ttemp=x;

    while(ttemp->next!=NULL){

        ttemp=ttemp->next;

        printf("%d %d\n",ttemp->id,ttemp->score);

    }

}

Snode* minid(Snode* x){

    int mintemp=x->next->id;

    Snode* minp=x->next;

    while(x->next!=NULL){

        x=x->next;

        if(mintemp>x->id){

            mintemp=x->id;

            minp=x;

        }

    }

    return minp;

}

int main()

{

    int N,M;

    scanf("%d %d",&N,&M);

    Snode* a=init();

    Snode* b=init();

    Snode* together=init();

    for(int i=1;i<=N;i++){

        Snode* temp=init();

        scanf("%d %d",&temp->id,&temp->score);

        Snode* ttemp=a;

        while(ttemp->next!=NULL){

            ttemp=ttemp->next;

        }

        ttemp->next=temp;

    }

    for(int i=1;i<=M;i++){

        Snode* temp=init();

        scanf("%d %d",&temp->id,&temp->score);

        Snode* ttemp=b;

        while(ttemp->next!=NULL){

            ttemp=ttemp->next;

        }

        ttemp->next=temp;

    }

    for(int i=1;i<=N+M;i++){

        Snode* temp=together;

        while(temp->next!=NULL){

            temp=temp->next;

        }

        Snode* minida=minid(a);

        Snode* minidb=minid(b);

        Snode* min=(minida->id)<(minidb->id)?minida:minidb;

        Snode* ttemp=init();

        ttemp->id=min->id;

        ttemp->score=min->score;

        temp->next=ttemp;

        min->id=999;

    }

    

    printnodes(together);

    free(a);

    free(b);

    free(together);

return 0;

}

 

0.0分

1 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区