7-53 两个有序序列的中位数

错误:

#include <stdio.h>
#include <stdlib.h>
typedef struct note
{
    int data;
    struct note *next;
}note;
note* CreatList(int n)
{
    note *head,*q,*p;
    head=(note*)malloc(sizeof(note));
    head->next=NULL;
    q=head;
    int a;
    while(n--)
    {
        scanf("%d",&a);
        p=(note*)malloc(sizeof(note));
        p->data=a;
        p->next=NULL;
        q->next=p;
        q=p;
    }
    return head;
}
int c=0;
note *Different(note *head1,note *head2)
{
    head1=head1->next;head2=head2->next;
    note *head,*q,*p;
    head=(note*)malloc(sizeof(note));
    head->next=NULL;
    q=head;
    while(head1!=NULL&&head2!=NULL)
    {
        if(head1->data<head2->data)
        {
            c++;
            //printf("%d\n",head1->data);
            p=(note*)malloc(sizeof(note));
            p->data=head1->data;
            p->next=NULL;
            q->next=p;
            q=p;
            head1=head1->next;
        }
        else if(head1->data>head2->data)
        {
            //printf("%d\n",head2->data);
            c++;
            p=(note*)malloc(sizeof(note));
            p->data=head2->data;
            p->next=NULL;
            q->next=p;
            q=p;
            head2=head2->next;
        }
        else
        {
            c++;
            p=(note*)malloc(sizeof(note));
            p->data=head1->data;
            p->next=NULL;
            q->next=p;
            q=p;
            //printf("%d\n",head1->data);
            head1=head1->next;
            head2=head2->next;
        }
    }
    if(head1==NULL)
    {
        while(head2!=NULL)
        {
            c++;
            q->next=head2;
            q=head2;
            head2=head2->next;
        }
    }
    if(head2==NULL)
    {
        q->next=head1;
        while(head1!=NULL)
        {
            c++;
            q->next=head1;
            q=head1;
            head1=head1->next;
        }
    }
    return head;
}
void Print(note *head)
{
    head=head->next;
    c=c/2;
    int k=0;
    while(1)
    {
        k++;
        if(k==c)
        {
            printf("%d\n",head->data);
            break;
        }
        head=head->next;
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    note *p1=CreatList(n);
    note *p2=CreatList(n);
    note *p=Different(p1,p2);
    Print(p);
}

猜你喜欢

转载自www.cnblogs.com/xyfs99/p/10350455.html
今日推荐