搞了一周的一道链表题,还是在运行的时候出岔子
弄了一周的一道链表题,还是在运行的时候出岔子
题目是求多项式的和,运行时输入链表没问题,目测问题就出在相加上,但是怎么都没查出来。
------解决方案--------------------
if(c->link=NULL)cout<<"+";
}
cout<<endl;
}
改为
if(c->link==NULL)cout<<"+";
}
------解决方案--------------------
if(a->exp=b->exp) 也改为 if(a->exp==b->exp)
------解决方案--------------------
闲着也是闲着,帮你改了一些,注意你的代码排版。
题目是求多项式的和,运行时输入链表没问题,目测问题就出在相加上,但是怎么都没查出来。
#include<iostream>
using namespace std;
#define MZX 100;
struct NODE;
struct NODE
{
int coef;
int exp;
struct NODE * link;
};
struct NODE * createlink()
{
struct NODE *head,*p;
p=head=new struct NODE;
cout<<"input coef,exp"<<endl;
cin>>p->coef>>p->exp;
while(p->coef!=0)
{
p->link=new struct NODE;
p=p->link;
cin>>p->coef>>p->exp;
}
p->link=NULL;
return head;
}
struct NODE * addlist(struct NODE *a,struct NODE *b)
{
struct NODE *c,*head;
head=c=new struct NODE;
while(a->link!=NULL&&b->link!=NULL)
{
if(a->exp>b->exp)
{c=a;a=a->link;}
if(a->exp<b->exp)
{c=b;b=b->link;}
if(a->exp=b->exp)
{
c->coef=a->coef+b->coef;
c->exp=a->exp;
c=c->link;
a=a->link;
b=b->link;
}
}
if(a->link!=NULL)
{
while(a->link!=NULL)
{c=a;a=a->link;}
}
if(b->link!=NULL)
{
while(b!=NULL)
{c=b;b=b->link;}
}
c->link=NULL;
return head;
}
void printlist(struct NODE *c)
{
cout<<"Poly=";
while(c->coef!=0)
{
cout<<c->coef<<"x^"<<c->exp;
if(c->link=NULL)cout<<"+";
}
cout<<endl;
}
void main()
{
struct NODE *a,*b,*c;
a=createlink();
b=createlink();
c=addlist(a,b);
}
struct
------解决方案--------------------
if(c->link=NULL)cout<<"+";
}
cout<<endl;
}
改为
if(c->link==NULL)cout<<"+";
}
------解决方案--------------------
if(a->exp=b->exp) 也改为 if(a->exp==b->exp)
------解决方案--------------------
闲着也是闲着,帮你改了一些,注意你的代码排版。
#include<iostream>
using namespace std;
#define MZX 100;
struct NODE
{
int coef;
int exp;
NODE * link;
};
NODE * createlink()
{
NODE *head,*p;
p = head = new NODE();
cout<<"input coef,exp"<<endl;
cin >> p->coef >> p->exp;
while(p->coef)
{
p->link = new NODE();
p = p->link;
cin>> p->coef >> p->exp;
}
p->link = NULL;
return head;
}
NODE* merge(NODE *a,NODE *b)
{
NODE *c = 0 ,*head = 0, *t;
while(a && b)
{
if(a->exp > b->exp)
{
if(!head)
{
head = c = a;