解题思路:1.分别建立链表1和2;

              2.连接链表1、2;

              3.连接后的总链表为链表3;

              4.用选择排序法对链表3进行排序。

注意事项:    注意多个头指针及next指针的指向(此处为难点)

参考代码:

#include<stdio.h>

#include<stdlib.h>

 

typedef struct node

{

        int id;

        int sc;

        struct node *next;

 

}Node;

 

Node *input1(int m)

{

    Node *p1,*p2,*head1;                                                                                                                                                                                                                                                                                 

    int i;

    for(i=0;i<m;i++)

    {

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

        if(i==0)

        {

            scanf("%d %d",&p1->id,&p1->sc);

            head1=p1;

            p2=p1;

        }

        else

        {

            scanf("%d %d",&p1->id,&p1->sc);

            p2->next=p1;

            p2=p1;

        }

    }

    p2->next=NULL;

    return head1;

}

 

Node *input2(int n)

{

    Node *p1,*p2,*head2;

    int i;

    for(i=0;i<n;i++)

    {

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

        if(i==0)

        {

            scanf("%d %d",&p1->id,&p1->sc);

            head2=p1;

            p2=p1;

        }

        else

        {

            scanf("%d %d",&p1->id,&p1->sc);

            p2->next=p1;

            p2=p1;

        }

    }

    p2->next=NULL;

    return head2;

}

 

Node *connect(Node *head1,Node *head2)

{

    Node *head3=head1;

    while(head1->next!=NULL)

    {

        head1=head1->next;

    }

    head1->next=head2;

    return head3;  

}

 

void paixu(Node *head3)         //选择排序法

{

    Node *min,*last;

    int a,b;

     

    for(min=head3;min;min=min->next)

    {

        for(last=min->next;last;last=last->next)

        {

            if(min->id >last->id )

            {

                a=min->id ;min->id =last->id ;last->id =a;

                b=min->sc ;min->sc =last->sc ;last->sc =b;

            }

        }

    }

}

 

void print(Node *head3)

{

    while(head3)

    {

        printf("%d %d\n",head3->id,head3->sc);

        head3=head3->next; 

    }

}

 

int main()

{

    int m,n;

    scanf("%d %d",&m,&n);

    Node *head1=input1(m);

     Node *head2=input2(n);

    Node *head3=connect(head1,head2);

    paixu(head3);

    print(head3);

    return 0;

}


点赞(0)
 

0.0分

3 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 1 条评论

九九 11月前 回复TA