我不李姐


私信TA

用户名:uq_19162528603

访问量:1419

签 名:

没被人肯定过

等  级
排  名 3042
经  验 1976
参赛次数 1
文章发表 10
年  龄 19
在职情况 学生
学  校 南京理工大学紫金学院
专  业 物联网

  自我简介:

没被人肯定过

解题思路:先构建两个链表,然后按照链表内的学号进行升序排序,这里我的思路是每次插入前找到比链表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分

1 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区