解题思路:
注意事项:
参考代码:
#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语言代码)浏览:1116 |
简单的a+b (C语言代码)浏览:672 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:594 |
C语言训练-计算1977!* (C++代码)浏览:825 |
C语言训练-计算t=1+1/2+1/3+...+1/n (C语言代码)浏览:833 |
【亲和数】 (C语言代码)浏览:522 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:383 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:721 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:642 |
WU-蓝桥杯算法提高VIP-勾股数 (C++代码)浏览:1538 |