解题思路:先构建两个链表,然后按照链表内的学号进行升序排序,这里我的思路是每次插入前找到比链表2待插入的节点学号大的节点,然后进行插入

注意事项:

参考代码:

#include<stdio.h>

#include<malloc.h>


//节点

typedef struct student

{

int num;

int score;

struct student* next;

}Node;


//函数声明

Node* InitList(int len);

void print(Node* head);

Node* Insert(Node* head1, Node* head2);



int main()

{

int len1, len2;

//输入链表的长度

scanf("%d%d", &len1, &len2);

//初始化链表

Node* head1 = InitList(len1);

Node* head2 = InitList(len2);

//进行链表的插入

head1 = Insert(head1, head2);

//打印链表内容

print(head1);


return 0;

}


//初始化链表

Node* InitList(int len)

{

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

head->next = NULL;

Node* cur = head;

//根据链表的长度来利用循环来链接节点

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

{

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

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

cur->next = temp;

temp->next = NULL;


cur =temp;

}

cur->next = NULL;

return head;

}


//打印链表内容

void print(Node* head)

{

Node* cur = head->next;

while (cur != NULL)

{

printf("%d %d\n", cur->num, cur->score);

cur = cur->next;

}

}


//链表按照升序插入

Node* Insert(Node* head1, Node* head2)

{

Node* cur1 = head1->next;

Node* cur2 = head2->next;

Node* cur = head1;

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


while (cur2 != NULL)

{

while (cur2->num >= cur1->num)

{

cur = cur1;

cur1 = cur1->next;

}

//链表2当前节点的数值不是链表1中的最大值

if(cur!=NULL)

{

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

temp->num = cur2->num;

temp->score = cur2->score;


temp->next = cur1;

cur->next = temp;


cur = head1;

cur1 = cur->next;

cur2 = cur2->next;

}

//如果链表2当前节点的数值比链表1数值都大,那么只要进行尾插就行

if (cur1 == NULL) {


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

temp->num = cur2->num;

temp->score = cur2->score;

cur1->next = temp;

temp->next = NULL;


cur = head1;

cur1 = cur->next;

cur2 = cur2->next;

}

}


return head1;

}


点赞(0)
 

0.0分

1 人评分

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

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

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

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

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

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

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

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

评论列表 共有 0 条评论

暂无评论