主题:[讨论]求助 链表问题
我想用链表实现两个多项式的相加操作
例: L1=2X^3+4X^5+6X^7
L2=3X^4+4X^5
求L1+L2的表达式
但是出现问题了
下面是我写的代码 各位有空帮忙看下
#include <iostream.h>
typedef struct node
{
int data;
int n;
node* next;
}node;
node* createlist(char n[])
{ int i=0;
node *head,*t,*end;
head=new node;
end=head;
t=end;
cout<<"请输入"<<n<<"的第"<<i+1<<"项的系数(输入0为结束) ";
cin>>t->data;
if(t->data!=0)
{ cout<<"请输入"<<n<<"的第"<<i+1<<"项的次幂 ";
cin>>t->n;
i++;
}
else
{
delete t;end=NULL;end->next=NULL;head=NULL;return head;
}
while(t->data!=0)
{
end=t;
t=new node;
cout<<"请输入"<<n<<"的第"<<i+1<<"项的系数(输入0为结束) ";
cin>>t->data;
if(t->data==0)
{delete t;end->next=NULL;return head;}
cout<<"请输入"<<n<<"的第"<<i+1<<"项的次幂 ";
cin>>t->n;
i++;
end->next=t;
}
delete t;
end->next=NULL;
return head;
}
void dispaly(node *L,char n[])
{ cout<<n<<": ";
if(L==NULL)
{
cout<<"链表为空!";
}
else
while(L!=NULL)
{
cout<<L->data<<"X^"<<L->n;
L=L->next;
if(L!=NULL)
cout<<"+";
}
cout<<endl;
}
node *addlist(node *p1,node *p2)
{ int i=0;
node *head,*end,*t;
head=new node;
end=head;
t=end;
while(p1!=NULL&&p2!=NULL)
{
if(p1->n<p2->n)
{
t->data=p1->data;
t->n=p1->n;
end=t;
t=new node;
end->next=t;
p1=p1->next;
}
if(p1->n>p2->n)
{
t->data=p2->data;
t->n=p2->n;
end=t;
t=new node;
end->next=t;
p2=p2->next;
}
if(p1->n==p2->n)
{
t->data=p1->data+p1->data;
t->n=p1->n;
end=t;
t=new node;
end->next=t;
p2=p2->next;
p1=p1->next;
}
}
if(p1==NULL)
{ while(p2!=NULL)
{
t->data=p2->data;
t->n=p2->n;
end=t;
t=new node;
end->next=t;
p2=p2->next;
}
}
if(p2==NULL)
{ while(p1!=NULL)
{
t->data=p1->data;
t->n=p1->n;
end=t;
t=new node;
end->next=t;
p1=p1->next;
}
}
delete t;
end->next=NULL;
return head;
}
void main()
{
node *p1=createlist("L1");
node *p2=createlist("L2");
dispaly(p1,"L1");
dispaly(p2,"L2");
node *p3=addlist(p1,p2);
dispaly(p3,"L1+L2");
}
例: L1=2X^3+4X^5+6X^7
L2=3X^4+4X^5
求L1+L2的表达式
但是出现问题了
下面是我写的代码 各位有空帮忙看下
#include <iostream.h>
typedef struct node
{
int data;
int n;
node* next;
}node;
node* createlist(char n[])
{ int i=0;
node *head,*t,*end;
head=new node;
end=head;
t=end;
cout<<"请输入"<<n<<"的第"<<i+1<<"项的系数(输入0为结束) ";
cin>>t->data;
if(t->data!=0)
{ cout<<"请输入"<<n<<"的第"<<i+1<<"项的次幂 ";
cin>>t->n;
i++;
}
else
{
delete t;end=NULL;end->next=NULL;head=NULL;return head;
}
while(t->data!=0)
{
end=t;
t=new node;
cout<<"请输入"<<n<<"的第"<<i+1<<"项的系数(输入0为结束) ";
cin>>t->data;
if(t->data==0)
{delete t;end->next=NULL;return head;}
cout<<"请输入"<<n<<"的第"<<i+1<<"项的次幂 ";
cin>>t->n;
i++;
end->next=t;
}
delete t;
end->next=NULL;
return head;
}
void dispaly(node *L,char n[])
{ cout<<n<<": ";
if(L==NULL)
{
cout<<"链表为空!";
}
else
while(L!=NULL)
{
cout<<L->data<<"X^"<<L->n;
L=L->next;
if(L!=NULL)
cout<<"+";
}
cout<<endl;
}
node *addlist(node *p1,node *p2)
{ int i=0;
node *head,*end,*t;
head=new node;
end=head;
t=end;
while(p1!=NULL&&p2!=NULL)
{
if(p1->n<p2->n)
{
t->data=p1->data;
t->n=p1->n;
end=t;
t=new node;
end->next=t;
p1=p1->next;
}
if(p1->n>p2->n)
{
t->data=p2->data;
t->n=p2->n;
end=t;
t=new node;
end->next=t;
p2=p2->next;
}
if(p1->n==p2->n)
{
t->data=p1->data+p1->data;
t->n=p1->n;
end=t;
t=new node;
end->next=t;
p2=p2->next;
p1=p1->next;
}
}
if(p1==NULL)
{ while(p2!=NULL)
{
t->data=p2->data;
t->n=p2->n;
end=t;
t=new node;
end->next=t;
p2=p2->next;
}
}
if(p2==NULL)
{ while(p1!=NULL)
{
t->data=p1->data;
t->n=p1->n;
end=t;
t=new node;
end->next=t;
p1=p1->next;
}
}
delete t;
end->next=NULL;
return head;
}
void main()
{
node *p1=createlist("L1");
node *p2=createlist("L2");
dispaly(p1,"L1");
dispaly(p2,"L2");
node *p3=addlist(p1,p2);
dispaly(p3,"L1+L2");
}