希望各位大佬帮我看看我这个一元多项式的加法哪出问题了
#include <iostream>
using namespace std;
typedef struct Node* ProtoNode;
typedef ProtoNode List;
typedef ProtoNode Position;
struct Node
{
float coef;//系数
int expn;//指数
List next;
};
void CreatePolynomial(List L)
{
List p=L;
float coe;
int exp;
while(cin>>coe>>exp)
{
if(coe>0&&exp>0)
{
List pnewnode=new Node;
pnewnode->coef=coe;
pnewnode->expn=exp;
pnewnode->next=nullptr;
p->next=pnewnode;
p=pnewnode;
}
else break;
}
}
int IsEmpty(List L)
{
if(L->next==NULL)
return 1;
else return 0;
}
int IsLast(Position p,List L)
{
return p->next==NULL;
}
void PrintPolynomial(List L)
{
List p=L->next;
cout<<"这是你的多项式:";
while(p!=nullptr)
{
cout<<"系数"<<p->coef<<" "<<"指数:"<<p->expn<<" ";
p=p->next;
}
cout<<endl;
}
Position FindPrevious (int x,ProtoNode phead)
{
Position p;
p=phead;
while(p->next!=NULL && p->next->expn!=x)
p=p->next;
return p;
}
void Delete(int x,ProtoNode phead)
{
Position p,TmpCell;
p=FindPrevious(x,phead);
if(!IsLast(p,phead))
{
TmpCell=p->next;
p->next=TmpCell->next;
free(TmpCell);
}
}
ProtoNode Add(List L1,List L2)
{
//cout<<1;
List P1=L1->next;
//cout<<2;
List P2=L2->next;
//cout<<3;
while(P1)
{
// cout<<7<<endl;
if(P1->expn==P2->expn)
{
cout<<8<<endl;
P1->coef+=P2->coef;
Delete(P2->expn,L2);
PrintPolynomial(L2);
P1=P1->next;
P2=L2;
}
else P2=P2->next;
if(P2==nullptr)
{
P1=P1->next;
P2=L2;
}
}
P2=L2->next;
P1->next=P2;
cout<<4<<endl;
}
int main()
{
List head1=nullptr;
head1=new Node;
head1->coef=0;
head1->expn=0;
head1->next=nullptr;
cout<<"请输入你的第一个多项式:"<<endl;
CreatePolynomial(head1);
PrintPolynomial(head1);
List head2=nullptr;
head2=new Node;
head2->coef=0;
head2->expn=0;
head2->next=nullptr;
cout<<"请输入你的第二个多项式:"<<endl;
CreatePolynomial(head2);
PrintPolynomial(head2);
Add(head1,head2);
cout<<"相加后等于:"<<endl;
PrintPolynomial(head1);
}
在我相加函数那,经过我调试,好像把L2的结点接到L1的时候出了问题,但是我想不清楚为什么会出问题,直接指针指过去不久可以了嘛