解题思路:
注意事项:
参考代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct number{
int data;
int score;
struct number *next;
};
struct number *Creat_Link(int num)
{
struct number *head=NULL,*tail=NULL,*newptr=NULL;
int number1;
int number2;
int i;
i=1;
while(i<=num){
scanf("%d",&number1);
scanf("%d",&number2);
newptr=malloc(sizeof(struct number));
newptr->data=number1;
newptr->score=number2;
if(newptr==NULL){
return NULL;
}
else{
if(head==NULL){
head=newptr;
tail=newptr;
}
else{
tail->next = newptr;
tail = newptr;
}
}
i++;
}
tail->next=NULL;
return head;
}
void find(struct number * headPtr,int value,struct number ** previousPtr,struct number ** currentPtr)//找到相应的结点
{
struct number * currentPtrTemp,*previousPtrTemp;
currentPtrTemp=headPtr;
previousPtrTemp=NULL;
while (currentPtrTemp!=NULL &&
currentPtrTemp->data<=value){
previousPtrTemp=currentPtrTemp;
currentPtrTemp=currentPtrTemp->next;
}
*previousPtr=previousPtrTemp;
*currentPtr=currentPtrTemp;
}
struct number * mergeSortList(struct number * head1Ptr,struct number * head2Ptr)
{
struct number *left2Ptr=NULL,*previous1Ptr=NULL,*current1Ptr=NULL,*previous2Ptr=NULL,*current2Ptr=NULL;
while(head2Ptr!=NULL){
previous1Ptr=NULL;
current1Ptr=NULL;
previous2Ptr=NULL;
current2Ptr=NULL;
left2Ptr=head2Ptr;
find(head1Ptr,left2Ptr->data,&previous1Ptr,¤t1Ptr);
if(current1Ptr==NULL){
previous1Ptr->next=left2Ptr;
head2Ptr=NULL;
}
else{
find(head2Ptr,current1Ptr->data,&previous2Ptr,¤t2Ptr);
if(current1Ptr==head1Ptr){
previous2Ptr->next=head1Ptr;
head1Ptr=left2Ptr;
}
else{
previous1Ptr->next=left2Ptr;
previous2Ptr->next=current1Ptr;
}
head2Ptr=current2Ptr;
}
}
return head1Ptr;
}
void reversePrintList(struct number * headPtr)
{
if(headPtr!=NULL){
printf("%d %d\n",headPtr->data,headPtr->score);
reversePrintList (headPtr->next);
}
}
void destory(struct number *headptr)
{
struct number *temptr=NULL;
while(headptr!=NULL){
temptr=headptr;
headptr=headptr->next;
free(temptr);
}
}
main()
{
struct number *head1ptr=NULL,*head2ptr=NULL,*main_headptr=NULL;
// printf("the link 1:\n");
int n,m;
scanf("%d %d",&n,&m);
head1ptr=Creat_Link(n);
// printf("\nthe link 2:\n");
head2ptr=Creat_Link(m);
main_headptr=mergeSortList(head1ptr,head2ptr);
// printf("\nthe merge link:\n");
reversePrintList(main_headptr);
destory(main_headptr);
destory(head1ptr);
destory(head2ptr);
head1ptr=NULL;
head2ptr=NULL;
main_headptr=NULL;
return 0;
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复