Jayden


私信TA

用户名:dotcpp0731360

访问量:255

签 名:

等  级
排  名 2400
经  验 2235
参赛次数 0
文章发表 12
年  龄 0
在职情况 学生
学  校
专  业 数据科学与大数据技术

  自我简介:

解题思路:
    自定义函数实现功能模块化:

        1、创捷链表createList

        2、创建节点creatNode

        3、插入节点insertNodeByHead(这里用的是头插入)

        4、链表合并mergeList

        5、链表排序sortList(选择排序法)

        6、链表打印printList


注意事项: 看代码注释就够啦哈哈哈


参考代码:

#include <stdio.h>
#include <stdlib.h>

struct student{
    int id;    //学号
    int score; //成绩
};

struct Node{
    struct student data;    //数据域
    struct Node* next;     //指针域
};

//创捷链表
struct Node* createList()
{
    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));    //动态申请内存
    headNode -> next = NULL;
    return headNode;
}

//创建节点
struct Node* createNode(struct student data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));    //动态申请内存
    newNode -> data = data;    //初始化
    newNode -> next = NULL;
    return newNode;
}

//插入节点
void insertNodeByHead(struct Node* headNode, struct student data)        //头插法:每次插入节点都是插入在headNode和headNode->next之间
{
    struct Node* newNode = createNode(data);            //调用创建节点函数
    newNode -> next = headNode -> next;        //新节点指向的下一个是原头节点的下一个
    headNode -> next = newNode;             //头节点指向的下一个节点是新节点
}

//链表连接
void mergeList(struct Node* headNode1, struct Node* headNode2)
{
    struct Node* pMove = headNode1;        //移动指针变量找到list1的尾节点,然后与list2连接
    while(pMove -> next != NULL)            //while寻找尾节点
    {
        pMove = pMove -> next;        //若不是空,则说明不是尾节点,pMove不断后移,当pMove为空(NULL)时,说明此时pMove所在节点就是尾节点
    }
    headNode2 = headNode2 -> next;        //list2的头节点是没有数据的,所以连接的是list2->next.
    pMove -> next = headNode2;            //实现链表的连接功能
}

//链表排序
void sortList(struct Node* headNode, struct student data)
{
    struct Node* turn = headNode -> next;            //turn用于外循环,遍历每个节点
    struct Node* move = headNode -> next -> next;  //move用于内循环,遍历turn后面未排序的所以节点
    struct Node* min = headNode -> next;            // min用于定位内循环中当前的最小的学号的节点
    while(turn -> next != NULL)    //外循环
    {
        min = turn;        //每一次内循环遍历前都要初始化,假使turn就是当前最小的学号的节点
        while(move != NULL)
        {
            if(move -> data.id < min -> data.id)    //判断move里的学号与min里的学号的大小
            {
                min = move;
            }
            move = move -> next;        //move不断后移遍历
        }
        if(min != turn)
        {
            struct student temp = min -> data;  //数据交换
            min -> data = turn -> data;
            turn -> data = temp;
        }
        turn = turn -> next;    //每次内循环结束后,turn都要向后移动一个节点,开始排序第二个数据…………
        move = turn -> next;    //move指向的节点永远是turn指向的节点的下一个
    }
}

//链表打印
void printList(struct Node* headNode, struct student data)
{
    struct Node* pMove = headNode -> next;
    while(pMove != NULL)
    {
        printf("%d\t%d\n", pMove -> data.id, pMove -> data.score);
        pMove = pMove -> next;
    }
}

int main()
{
    struct student information;         //定义结构体变量information用于暂时存放数据
    struct Node* list1 = createList();    //调用创建链表函数
    struct Node* list2 = createList();
    int N, M;
    scanf("%d %d", &N, &M);
    for(int i = 0; i < N; i ++)        //获取list1的数据
    {
        scanf("%d %d", &information.id, &information.score);
        insertNodeByHead(list1, information);    //调用插入节点函数
    }
    for(int j = 0; j < M; j ++)        //获取list2的数据
    {
        scanf("%d %d", &information.id, &information.score);
        insertNodeByHead(list2, information);
    }
    mergeList(list1, list2);       //调用链表连接函数
    sortList(list1, information);    //调用排序函数
    printList(list1, information);    //打印验证
    return 0;
}


 

0.0分

2 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区