数据结构链表问题
我有个很棘手的问题啊,请各位帮帮忙!#include<iostream>
using namespace std;
class intsllnode
{
public:
int c,e;
intsllnode *next;
public:
intsllnode(int a,int b,intsllnode*ptr)
{
c=a;
e=b;
next=ptr;
}
intsllnode(){}
};
class intsllist
{
private:
intsllnode*head,*tail,*f1,*f2,*f3;//f1,f2代表的是两个要相加的多项式,f3代表的是结果函数
//intsllist*s;
void paixu(intsllnode*ptr);//根据x的指数从小到大排序
public:
intsllist(){}
void creat();
void intserttof1(int a,int b);//插入数到f1;
void intserttof2(int a,int b);//插入数到f2;
void addtof3();//进行相加得到f3;
void show();
};
void intsllist::paixu(intsllnode*ptr)//ptr只的是某个函数如f1,f2
{
intsllnode*n,*m;
n=ptr->next;
m=n->next;
while(n!=NULL)
{
while(n->e>m->e)
{
intsllnode t;
t=*n;
*n=*m;
*m=t;
m=m->next;
}
n=n->next;
}
}
void intsllist::creat()
{
intsllist *s;
s=new intsllist();
}
void intsllist::intserttof1(int a,int b)
{
intsllnode*j;//*p,
//p=new intsllnode();
f1=new intsllnode();
//s=new intsllist();
//s->f1=p;//p设为链表的头节点
j=new intsllnode();
j->c=a;
j->e=b;
if(head=tail=0)
head=tail=j;
else
{
f1->next=j;
//p->next=j;
j->next=head;
head=j;
}
}
void intsllist::intserttof2(int a,int b)
{
//s=new intsllist();
intsllnode*j;//*q,
f2=new intsllnode();
//q=new intsllnode();
//s->f2=q;//p设为链表的头节点
j=new intsllnode();
j->c=a;
j->e=b;
//q->next=j;
f2->next=j;
j->next=head;
head=j;
}
void intsllist::addtof3()
{
intsllnode*x,*y;//*l;
int a;
//s=new intsllist();
//l=new intsllnode();
//s->f3=l;
f3=new intsllnode();
x=f1->next;
y=f2->next;
while(x!=NULL&&y!=NULL)
{
if(x->e==y->e)
f3->c=x->c+y->c;
else if(x->e>y->e)
{
while(x->e!=y->e&&x->e>y->e)
{
y=y->next;
f3->c=y->c;
f3=f3->next;
}
if(x->e==y->e)
f3->c=x->c+y->c;
else
f3->c=x->c;
}
else if(x->e<y->e)
{
while(x->e!=y->e&&x->e<y->e)
{
x=x->next;
f3->c=x->c;
f3=f3->next;
}
if(x->e==y->e)
f3->c=x->c+y->c;
else
f3->c=y->c;
}
x=x->next;
y=y->next;
}
}
void intsllist::show()
{
intsllnode*i;
i=f3;
cout<<"所有结果为";
while(i!=NULL)
{
if(i->c==0)
delete i;
else
cout<<i->c<<i->e;
i=i->next;
}
}
int main()
{
intsllist *k;int a,b;
k->creat();
cout<<"输入f1的x的系数和指数"<<endl;
cin>>a>>b;
cout<<"ok"<<endl;
k->intserttof1(a,b);
k->show();
cout<<"输入f2的x的系数和指数"<<endl;
cin>>a>>b;
k->intserttof2(a,b);
k->show();
k->addtof3();
k->show();
system("pause");
return 0;
}
总是会有问题
[解决办法]
if(head=tail=0)
这个是什么?????
有这么写法的么