请叫我小胡


私信TA

用户名:Walkerone2019

访问量:5822

签 名:

读万卷书,行万里路。

等  级
排  名 1476
经  验 2745
参赛次数 2
文章发表 14
年  龄 24
在职情况 学生
学  校 天津工业大学
专  业

  自我简介:

逐梦路上的前行者......

解题思路:先定义一个带指针的结构体,然后定义一个创建链表的函数,一个连接两个链表的函数,一个排序并删除重复的函数,总的来说这就是链表的创建、合并和删除.

注意事项:1.链表创建函数中,链表创建结束的结构体中的结构体指针得赋成NULL,链表结束字符。

                2.链表排序和删除时,应该free()掉重复的结构体

参考代码:

#include<stdio.h>

#include<stdlib.h>

//声明模块

typedef struct students{

     int Id;

     int score;

     struct students *next;

}Stu;

Stu *creatl(int n);

void linkS(Stu *x1, Stu *x2);

void sort(Stu *x);

//主函数模块

int main()

{

     int n, m;

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

     Stu *head_a;

     Stu *head_b;

     head_a = creatl(n);

     head_b = creatl(m);

     linkS(head_a, head_b);

     sort(head_a);

     Stu *head;

     head = head_a->next;

     while (head!=NULL)

     {

         printf("%d %d\n", head->Id, head->score);

         head = head->next;

    

     }

    free( head_a);

     free( head_b);

     free( head);

     return 0;

}

//链表创建函数

Stu *creatl(int n)

{

     Stu *head, *node, *end;

     head = (Stu *)malloc(sizeof(Stu));

     end = head;

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

     {

         node = (Stu *)malloc(sizeof(Stu));

         scanf("%d %d", &node->Id, &node->score);

         end->next = node;

         end = node;

     }

     end->next = NULL;        //链表结束指针字符

     return head;

}

//链表连接函数

void linkS(Stu *x1,Stu *x2)

{

     while (1)

     {

    

         if (x1->next == NULL)

         {

             x1->next = x2->next;

             break;

         }

         x1 = x1->next;

     }

}

//链表排序并删除重复序号函数函数

void sort(Stu *x)

{

     Stu *pt, *pr;

     int a, b;

     for (pt = x; pt != NULL; pt = pt->next)

     {

         for (pr = pt->next; pr != NULL; pr = pr->next)

         {

             if (pt->Id > pr->Id)

             {

                 a = pt->Id;

                 b = pt->score;

                 pt->Id = pr->Id;

                 pt->score = pr->score;

                 pr->Id = a;

                 pr->score = b;

            

             }

         }

     }

  

     

    

}


实测通过


 

0.0分

1 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区