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);
}

 正确:

#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++;
            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)
        {
            c++;
            p=(note*)malloc(sizeof(note));
            p->data=head2->data;
            p->next=NULL;
            q->next=p;
            q=p;
            head2=head2->next;
        }
        else
        {
            c+=2;
            p=(note*)malloc(sizeof(note));
            p->data=head1->data;
            p->next=NULL;
            q->next=p;
            q=p;
            p=(note*)malloc(sizeof(note));
            p->data=head1->data;
            p->next=NULL;
            q->next=p;
            q=p;
            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+1)/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);
}

 

转载于:https://www.cnblogs.com/xyfs99/p/10350455.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值