解题思路:
注意事项:
参考代码:
#include <stdio.h>
#include <string.h>
typedef struct Stu
{
int id;
int score;
struct Stu *pnext;
}Stu;
Stu *create_Stu(int n)
{
Stu *head=(Stu*)malloc(sizeof(Stu));
if(head==NULL)
return NULL;
head->id=-1;
head->score=-1;
head->pnext=NULL;
Stu *tmp=head;
int i;
for(i=0;i<n;i++)
{
Stu *new_node=(Stu*)malloc(sizeof(Stu));
if(new_node==NULL)
return NULL;
scanf("%d",&new_node->id);
scanf("%d",&new_node->score);
new_node->pnext=NULL;
tmp->pnext=new_node;
tmp=new_node;
}
return head;
}
Stu *merge_Stu(Stu *students1,Stu *students2)
{
if(students1==NULL||students2==NULL)
return NULL;
Stu *tmp1=students1;
while(tmp1->pnext!=NULL)
{
tmp1=tmp1->pnext;
}
Stu *tmp2=students2;
tmp1->pnext=tmp2->pnext;
free(tmp2);
return students1;
}
void sort_Stu(Stu *students)
{
if (students == NULL)
return;
Stu *pre = NULL;
Stu *cur = NULL;
Stu tmp;
for (pre = students->pnext; pre->pnext != NULL; pre = pre->pnext)
{
for (cur = pre->pnext; cur != NULL; cur = cur->pnext)
{
if (pre->id > cur->id)
{
tmp = *pre;
*pre = *cur;
*cur = tmp;
tmp.pnext = pre->pnext;
pre->pnext = cur->pnext;
cur->pnext = tmp.pnext;
}
}
}
}
void print_Stu(Stu *students)
{
if (students == NULL || students->pnext == NULL)
{
printf("invalid list!\n");
return;
}
Stu *cur = students->pnext;
while (cur != NULL)
{
printf("%d %d\n", cur->id, cur->score);
cur = cur->pnext;
}
}
void destory_Stu(Stu *students)
{
if (students == NULL)
return;
Stu *s = students;
Stu *tmp = NULL;
while (s != NULL)
{
tmp = s->pnext;
free(s);
s = tmp;
}
}
int main()
{
int N,M;
scanf("%d %d",&N,&M);
Stu *students1=create_Stu(N);
Stu *students2=create_Stu(M);
Stu *students=merge_Stu(students1,students2);
sort_Stu(students);
print_Stu(students);
destory_Stu(students);
return 0;
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复