哪位大神能指教一下这代码中的bug是啥呀

题目是这样的: 应用单链表实现一元多项式及其相加。 例如,一元多项式 1:x2+5x+3;一元多项式 2:2x2+5;加法运算结果:3x2+5x+8。
代码:
#include using namespace std;struct node{ int xishu; //系数 int zhishu; //指数 node* next;};void creatnode(node * phead) //创建链表{ cout<<"前一个数表示系数,后一个数表示指数。输入两个 0 表示结束输入"<>x; cin>>y; pnode->xishu=x; pnode->zhishu=y; pnode->next=nullptr; p->next=pnode; p=pnode; } if(x==0) { cout<<"输入完毕!"<next; node q=p; bool flag; flag=true; while(flag) //冒泡排序 { flag=false; p=phead->next; while((p->next)!=nullptr) { if((p->zhishu)<(p->next->zhishu)) { int temp,temp2; temp=p->xishu; temp2=p->zhishu; p->xishu=p->next->xishu; p->zhishu=p->next->zhishu; p->next->xishu=temp; p->next->zhishu=temp2; p=p->next; flag=true; } p=p->next; } }}void print(node*phead1,node * phead2,node * phead3) //输出两多项式相加后的多项式{ node *p=phead1; p=phead1->next; node * q=phead2; q=phead2->next; node store=phead3; while((p->next!=nullptr)&&(q->next!=nullptr)) //不考虑链表中最后的0,所以不考虑最后的结点 { node * temp=new node; if(p->zhishu==q->zhishu) { temp->xishu=p->xishu+q->xishu; temp->zhishu=p->zhishu; temp->next=nullptr; store->next=temp; store=temp; p=p->next; q=q->next; } if((p->zhishu)>(q->zhishu)) { temp->xishu=p->xishu; temp->zhishu=p->zhishu; temp->next=nullptr; store->next=temp; store=temp; p=p->next; } if((p->zhishu)<(q->zhishu)) { temp->xishu=q->xishu; temp->zhishu=q->zhishu; temp->next=nullptr; store->next=temp; store=temp; q=q->next; p=p; } } //接下来处理当两个链表中某个链表全部遍历之后,将令一个链表中的值赋给目标链表 while(p->next!=nullptr&&q->next==nullptr) { node * temp=new node; temp->xishu=p->xishu; temp->zhishu=p->zhishu; temp->next=nullptr; store->next=temp; store=temp; p=p->next; q=q; } while(q->next!=nullptr&&p->next==nullptr) { node * temp=new node; temp->xishu=q->xishu; temp->zhishu=q->zhishu; temp->next=nullptr; store->next=temp; store=temp; q=q->next; } //输出最终结果 cout<<"输出之前的遍历:"; travel(phead3); node * r=phead3; r=phead3->next; while(r->next!=nullptr) { if(r->zhishu!=0) { cout<xishu<<"x^"<zhishu<<"+"; } if(r->zhishu==0) { cout<xishu<<"+"; } r=r->next; } if(r->zhishu!=0) { cout<xishu<<"x^"<zhishu<<"."; } if(r->zhishu==0) { cout<xishu<<"."; }}int main(){ node* head1=new node; head1->next=nullptr; cout<<"请输入第一个一元多项式,"; creatnode(head1); node* head2=new node; head2->next=nullptr; cout<<"请输入第二个一元多项式,"; creatnode(head2); node * head3=new node; //创建目标链表的哨位结点 head3->next=nullptr; lsort(head1); lsort(head2); print(head1,head2,head3); return 0;}

当输入题目中的样例时,输出的只有5x^1+8. 少了3x^2这一项。

#include

using namespace std;

struct node
{
int xishu; //系数
int zhishu; //指数
node* next;
};

void creatnode(node * phead) //创建链表
{
cout<<"前一个数表示系数,后一个数表示指数。输入两个 0 表示结束输入"< node* p=phead;
int x=1,y=1;
while(x!=0)
{
node* pnode=new node;
cin>>x;
cin>>y;
pnode->xishu=x;
pnode->zhishu=y;
pnode->next=nullptr;
p->next=pnode;
p=pnode;
}
if(x==0)
{
cout<<"输入完毕!"<<endl;
cout<<endl;
}
}

void lsort(node * phead) //对链表按照指数由小到大排序
{
node * p=phead->next;
node *q=p;
bool flag;
flag=true;
while(flag) //冒泡排序
{
flag=false;
p=phead->next;
while((p->next)!=nullptr)
{
if((p->zhishu)<(p->next->zhishu))
{
int temp,temp2;
temp=p->xishu;
temp2=p->zhishu;
p->xishu=p->next->xishu;
p->zhishu=p->next->zhishu;
p->next->xishu=temp;
p->next->zhishu=temp2;
p=p->next;
flag=true;
}
p=p->next;
}

}
travel(phead);

}

void print(node*phead1,node * phead2,node * phead3)
{
node p=phead1;
p=phead1->next;
node * q=phead2;
q=phead2->next;
node
store=phead3;
while((p->next!=nullptr)&&(q->next!=nullptr)) //不考虑最后的0,所以不考虑最后的结点
{
node * temp=new node;
if(p->zhishu==q->zhishu) //若指数相同,则将head1中所指向的值存到目标链表中
{
temp->xishu=p->xishu+q->xishu;
temp->zhishu=p->zhishu;
temp->next=nullptr;
store->next=temp;
store=temp;
p=p->next;
q=q->next;
}
if((p->zhishu)>(q->zhishu))
{
temp->xishu=p->xishu;
temp->zhishu=p->zhishu;
temp->next=nullptr;
store->next=temp;
store=temp;
p=p->next;
}
if((p->zhishu)<(q->zhishu))
{
temp->xishu=q->xishu;
temp->zhishu=q->zhishu;
temp->next=nullptr;
store->next=temp;
store=temp;
q=q->next;
p=p;
}
}
//接下来处理某一链表遍历完后的情况
while(p->next!=nullptr&&q->next==nullptr)
{
node * temp=new node;
temp->xishu=p->xishu;
temp->zhishu=p->zhishu;
temp->next=nullptr;
store->next=temp;
store=temp;
p=p->next;
q=q;
}
while(q->next!=nullptr&&p->next==nullptr)
{
node * temp=new node;
temp->xishu=q->xishu;
temp->zhishu=q->zhishu;
temp->next=nullptr;
store->next=temp;
store=temp;
q=q->next;
}
node * r=phead3; //r为指向目标链表的指针
r=phead3->next;
while(r->next!=nullptr)
{
if(r->zhishu!=0)
{
cout<xishu<<"x^"<zhishu<<"+";
}
if(r->zhishu==0)
{
cout<xishu<<"+";
}
r=r->next;
}
if(r->zhishu!=0)
{
cout<xishu<<"x^"<zhishu<<".";
}
if(r->zhishu==0)
{
cout<xishu<<".";
}
}
int main()
{ node * head1=new node;
head1->next=nullptr;
cout<<"请输入第一个一元多项式,";
creatnode(head1);
node* head2=new node;
head2->next=nullptr;
cout<<"请输入第二个一元多项式,";
creatnode(head2);
node * head3=new node; //创建目标链表的哨位结点
head3->next=nullptr;
lsort(head1);
lsort(head2);
print(head1,head2,head3);
return 0;
}

代码中的travel是没有的,谢谢

问题蛮多的,第一行你#include后面什么头文件都没有,连IOSTREAM都没得,就这一个,后面基本不用看了

你这代码错误很多,看得出来,你和我差不多也是初学者,不过我比你早一点这个功能我也做过,给你看看我的吧。
数据结构作业
一元多项式相加C语言代码
#include
#include
typedef struct node
{
int exp,coef;
struct node *link;
}
PolyNode,*Polylinklist;
Polylinklist Creat(int n)
{
Polylinklist p,r=NULL,list=NULL;
int coef,exp,i;
for(i=1;i<=n;i++)
{
scanf("%d %d",&coef,&exp);
p=(Polylinklist)malloc(sizeof(PolyNode));
p->coef=coef;
p->exp=exp;
p->link=NULL;
if(list==NULL)
list=p;
else

r->link=p;
r=p;
}
return(list);
}
Polylinklist ATTACH(int coef,int exp,Polylinklist r)
{
Polylinklist w;
w=(Polylinklist)malloc(sizeof(PolyNode));
w->exp=exp;
w->coef=coef;
r->link=w;
return w;
}

Polylinklist PADD(Polylinklist a,Polylinklist b)
{
Polylinklist c;
Polylinklist r,p=a,q=b;
int x;
c=(Polylinklist)malloc(sizeof(PolyNode));
r=c;
while(p!=NULL&&q!=NULL)
if(p->exp==q->exp)
{
x=p->coef+q->coef;
if(x!=0)
r=ATTACH(x,q->exp,r);
p=p->link;
q=q->link;
}
else if(p->expexp)
{
r=ATTACH(q->coef,q->exp,r);
q=q->link;
}
else {r=ATTACH(p->coef,p->exp,r);
p=p->link;
}
while(p!=NULL)
{
r=ATTACH(p->coef,p->exp,r);
p=p->link;
}
while(q!=NULL)
{
r=ATTACH(q->coef,q->exp,r);
q=q->link;
}
r->link=NULL;
p=c;
c=c->link;
free(p);
return c;
}

void Result(Polylinklist w)
{
Polylinklist m;
m=w;

while(w==NULL)
    {
       printf("0");
       break;       
    }                                                                                              
while(w!=NULL)
{
   if(w->exp==0)
      printf("%d",w->coef);
   else 
      printf("%d*x^%d",w->coef,w->exp);

   w=w->link;

      while(w!=NULL)
      {
          if(w->coef>0)
          {
             if(w->exp==0)
             printf("+%d",w->coef);
             else 
             printf("+%d*x^%d",w->coef,w->exp);
          }
          else
          {
             if(w->exp!=0)
             printf("%d*x^%d",w->coef,w->exp);
             else
             printf("%d",w->coef);
          }

    w=w->link;
      }
}

}
void main()
{
Polylinklist c=NULL;
PolyNode *Lengtha;
PolyNode *Lengthb;
int a1,b1;
printf("Please input a's Length:");
scanf("%d",&a1);
Lengtha=Creat(a1);
printf(" a=");
Result(Lengtha);
printf("\n");
printf("Please input b's Length:");
scanf("%d",&b1);
Lengthb=Creat(b1);
printf(" b=");
Result(Lengthb);
printf("\n");
c=PADD(Lengtha,Lengthb);
printf("\n");
printf(" c=");
Result(c);
printf("\n");
}

哪里看不懂我可以解释