原题链接:[编程入门]链表合并
#include<stdio.h>
#include<stdlib.h>
struct link
{
int id;
int score;
struct link *next;
};
void display(struct link *head)
{
struct link* p=head;
if(head==NULL) printf("head=NULL\n");
while(p!=NULL)
{
printf("%d %d\n",p->id,p->score);
p=p->next;
}
}
void deletenode(struct link *head)
{
struct link *p1=head,*p2=NULL;
if(p1!=NULL)
{
p2=p1;
p1=p1->next;
free(p2);
}
}
/*
int search(struct link *head,int ID)
{
int p1=head;
if(head==NULL) return -1;
while(p1!=NULL)
{
if(p1->id==ID) return score;
p1=p1->next;
}
return -1;
}
*/
struct link *appendnode(struct link *head,int student_id,int student_score)
{
struct link *p1=NULL;
struct link *p2=head;
p1=(struct link*)malloc(sizeof(struct link));
if(p1==NULL) exit(0);
if(head==NULL) head=p1;
else
{
while(p2->next!=NULL) p2=p2->next;
p2->next=p1;
}
p2=p1;
p2->id=student_id;
p2->score=student_score;
p2->next=NULL;
return head;
}
int main(void)
{
int m,n,i,j,t;
int ID[100],SCORE[100];
int student_id,student_score;
struct link *head1=NULL,*head2=NULL,*head=NULL;
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
scanf("%d%d",&student_id,&student_score);
ID[i]=student_id;
SCORE[i]=student_score;
head1=appendnode(head1, student_id,student_score);
}
for(j=0;j<n;j++)
{
scanf("%d%d",&student_id,&student_score);
ID[i+j]=student_id;
SCORE[i+j]=student_score;
head2=appendnode(head2, student_id, student_score);
}
for(i=0;i<n+m-1;i++)
for(j=i+1;j<m+n;j++)
{
if(ID[i]>ID[j])
t=ID[i],ID[i]=ID[j],ID[j]=t,
t=SCORE[i],SCORE[i]=SCORE[j],SCORE[j]=t;
}
for(i=0;i<n+m;i++)
head=appendnode(head,ID[i],SCORE[i]);
display(head);
deletenode(head);
deletenode(head1);
deletenode(head2);
return 0;
}解题思路:
注意事项:
参考代码:
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复