解题思路:
注意事项:
参考代码:
#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语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:624 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:582 |
杨辉三角 (C语言代码)浏览:505 |
演讲大赛评分 (C语言代码)浏览:1696 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:672 |
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:592 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:611 |
字符删除 (C语言代码)浏览:767 |
汽水瓶 (C语言代码)浏览:579 |
1199题解浏览:707 |