weirdo


私信TA

用户名:weirdo917

访问量:901

签 名:

等  级
排  名 2175
经  验 2351
参赛次数 0
文章发表 2
年  龄 0
在职情况 学生
学  校 山东大学
专  业

  自我简介:

TA的其他文章

解题思路:话不多说看注释就行

注意事项:看注释咯

参考代码:

#include<stdio.h>

#include<stdlib.h> 

typedef struct _node{

        int id;

        int grade;//前两个是结点所存的数据 

        struct _node *next;//指向下一个结点的指针 

}Node;//这里用了typedef所以Node是类型而不是变量 

int main()

{

        int N,M;

        int i;

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

        Node *head1=NULL;//定义一个链表的头,它指向第一个结点 

        for(i=0;i<N;i++){//这里有明确的结点数量,所以用for循环 

                Node *p=(Node*)malloc(sizeof(Node));

                scanf("%d %d",&p->id,&p->grade);

            p->next=NULL;

            Node *last=head1;       //将链表的结点连接起来,需要遍历链表 

            if(last){

                 while(last->next){

                  last=last->next;

                 }

                 last->next=p;

             }else{

                     head1=p;

             }

        }

         Node *head2=NULL;

         for(i=0;i<M;i++){

                Node *p=(Node*)malloc(sizeof(Node));

                scanf("%d %d",&p->id,&p->grade);

                p->next=NULL;

                Node *last=head2;

                if(last){

                      while(last->next){

                              last=last->next;

                        }

                     last->next=p;

                 }else{

                      head2=p;

                  }

          }

         Node *last=head1;//注意这里需要再次定义last,因为上次定义在大括号中 

         while(last->next){//连接两个链表,先找到第一个链表的末端NULL,让它等于head2,实现连接 

                  last=last->next;//需要注意这里结束的条件是last->next而不是last 

         }

         last->next=head2;

         //下面开始按照学号排序,采用选择排序 

         int t;

         Node *min=head1;

         for(min=head1;min;min=min->next){

                  for(last=min->next;last;last=last->next){//注意这里遍历链表结束的条件是last而不是last->next 

                           if(min->id>last->id){//且注意这里每轮last的位置应该随min改变而改变 

                                   t=min->id;min->id=last->id;last->id=t;

                                   t=min->grade;min->grade=last->grade;last->grade=t; 

                             }

                  }

         }

         last=head1;

         while(last){

                  printf("%d %d\n",last->id,last->grade);

                  free(last);

                  last=last->next;

         }

         return 0;

}


 

0.0分

3 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区