关于单链表的一些问题
大家看看,这是我写的一个程序,为什么总是有问题?我改了又改,但是还是有问题,谁能帮我解决下子
#include "stdafx.h"
#include<iostream.h>
struct Node
{
float ceof;
int exp;
Node *next;
};
Node *CreateList(Node *head,int n)
{
for(int i=0;i<n;i++)
{
cout<<"请输入系数和项数:";
Node *p=new Node;
cin>>p->ceof;
cin>>p->exp;
p->next=NULL;
p->next=head->next;
head->next=p;
}
return head;
}
Node *AddPoly(Node *a,Node *b)
{
Node *head=new Node;
Node *tail=new Node;
head=tail;
a=a->next;
b=b->next;
while(a!=NULL&&b!=NULL)
{
if(a->exp<b->exp)
{
tail->next=a;
tail=a;
a=a->next;
}
else if(a->exp>b->exp)
{
tail->next=b;
tail=b;
b=b->next;
}
else
{
float x=a->ceof+b->ceof;
if(x==0)
{
a=a->next;
b=b->next;
}
else
{
a->ceof=x;
tail->next=a;
tail=a;
a=a->next;
b=b->next;
}
}
}
return head;
}
void PrintList(Node *head)
{
head=head->next;
while(head!=NULL)
{
cout<<head->ceof<<"x^"<<head->exp<<"+";
head=head->next;
}
}
int main(int argc, char* argv[])
{
Node *p=new Node;
Node *q=new Node;
cout<<"请输入二项式的项数:";
int n;
cin>>n;
p=CreateList(p,n);
cout<<"请输入二项式的项数:";
int m;
cin>>m;
q=CreateList(q,m);
Node *sum;
sum=AddPoly(p,q);
PrintList(sum);
return 0;
}