youarethe


私信TA

用户名:wuenda

访问量:2041

签 名:

等  级
排  名 4367
经  验 1319
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 北极
专  业

  自我简介:

解题思路:





注意事项:





参考代码:

#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,&current1Ptr);

        if(current1Ptr==NULL){ 

           previous1Ptr->next=left2Ptr; 

           head2Ptr=NULL;

        }

else{

          find(head2Ptr,current1Ptr->data,&previous2Ptr,&current2Ptr); 

          

           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 人评分

  评论区