weixin_54206748 2021-04-05 18:35 采纳率: 33.3%
浏览 29
已采纳

这个代码无法输出是哪里有问题?求教!

#include<iostream>
using namespace std;
typedef struct node{
    int coef,expn;
    struct node* next;
}node;
node* creatlist(int n)
{
    node *head,*tail,*p;
    head=new node();
    tail=head;
    for(int i=0;i<n;i++)
    {
        p=new node();
        cin>>p->coef>>p->expn;
        tail->next=p;
        tail=p;
    }
    tail->next=NULL;
    return head;
}
node* puls(node *head1,node* head2)
{
    node *head,*p1,*p2,*tail,*p;
    head=new node();
    tail=head;
    p1=head1->next;
    p2=head2->next;
    while(p1&&p2)
    {
        p=new node();
        if(p1->expn<p2->expn)
        {
            p->expn=p2->expn;
            p->coef=p2->coef;
            p2=p2->next;
        }else if(p1->expn>p2->expn)
        {
            p->expn=p1->expn;
            p->coef=p1->coef;
            p1=p1->next;
        }else if(p1->expn==p2->expn)
        {
            p->expn=p1->expn;
            p->coef=p1->coef+p2->coef;
            p1=p1->next;
            p2=p2->next;
        }
        tail->next=p;
        tail=p;
    }
    if(p1==NULL&&p2==NULL)
        return head;
    else if(p1!=NULL||p2!=NULL)
    {
        p1==NULL?tail->next=p2:tail->next=p1;
        return head;
    }
    return 0;
}
void print(node* head)
{
    node *p;
    p=head->next;
    cout<<p->expn<<' '<<p->coef;
    p=p->next;
    while(p){
        cout<<endl<<p->expn<<' '<<p->coef;
    }
}
int main()
{
    int m,n;
    cin>>m>>n;
    node *head1,*head2;
    head1=creatlist(m);
    head2=creatlist(n);
    node *head3;
    head3=puls(head1,head2);
    print(head3);
    delete(head1);delete(head2);delete(head3);
    return 0;
}

  • 写回答

2条回答 默认 最新

  • CSDN专家-cpp_learner 2021-04-06 08:33
    关注

    print函数的问题。循环p没有赋值next。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器