原题链接:[编程入门]链表合并
解题思路:
链式排序不是那么好弄。。所以用了交换
注意事项:
参考代码:
/*lb*/
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int num;
int score;
struct student *next;
}stu;
stu *create(stu *L,int n)
{
int i;
stu *p,*q;
L = (stu *)malloc(sizeof(stu));
q = L;
L->next = NULL;
for(i = 0; i < n; i++)//尾插
{
p = (stu *)malloc(sizeof(stu));
p->next = NULL;
scanf("%d%d",&p->num,&p->score);
q->next = p;
q = p;
}
return L;
}
stu *addandsort(stu *L1,stu *L2)
{
stu *p,*q,*min;
int t;
p = L1;
while(p->next!=NULL)
p = p->next;
p->next = L2->next;
p = L1->next;
while(p)
{
min = p;
q = p->next;
while(q)
{
if(min->num>q->num)
min = q;
q = q->next;
}
t = p->num;p->num = min->num;min->num = t;
t = p->score;p->score = min->score;min->score = t;
p = p->next;
}
return L1;
}
void print(stu *L)
{
stu *p = L->next;
while(p)
printf("%d %d\n",p->num,p->score),p = p->next;
}
int main()
{
stu *L1,*L2,*L3;
int n,m;
scanf("%d%d",&n,&m);
L1 = create(L1,n);
L2 = create(L2,m);
L3 = addandsort(L1,L2);
print(L3);
return 0;
}0.0分
2 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复