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

这个代码无法输出是哪里有问题?求教!
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-cpp_learner 2021-04-06 08:33关注
print函数的问题。循环p没有赋值next。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用