用链表实现两个多项式相加的函数
#include <stdio.h>#include <stdlib.h>
#include <string.h>
typedef int ElemType
typedef struct LNode
{ ElemType data;
struct LNode *next;
} LinkList;
void CreateListR(LinkList *L,int a[],int n)
{
LinkList *s,*r;
int i;
L=(LinkList *)malloc(sizeof(LinkList));
r=L;
for(i=0;i<n;i++)
{ s=(LinkList *)malloc(sizeof(LinkList));
s->data=a[i];
r-next=s;
r=s;
}
r->next=NULL;
}
void PlusList (LinkList *&Ha,LinkList *&Hb,LinkList *&Hc)
{
LinkList *pa,*pb,*pc,*s;
pa=Ha->next;
pb=Hb->next;
Hc=(LinkList *)malloc(sizeof(LinkList));
pc=Hc;
while(pa!=NULL&&pb!=NULL)
{ S=(LinkList *)malloc(sizeof(LinkList));
s->data=pa->data+pb->data;
pc->next=s;
pc=s;
pa=pa->next;
pb=pb->next;
}
if(pa==NULL)
pc=Hb;
if(pb==NULL)
pc=Ha;
}
void DispList (LinkList *L)
{
LinkList *p=L->next;
int i;
for(i=2;i>=0;i--)
{ printf("%2d x*x",p->data);
p=p->data;
printf("%2d x",p->data;);
p=p->data;
printf("%2d ",p->data);
p=p->data;
}
printf("\n");
}
int main()
{
LinkList *Ha,*Hb,*Hc;
a[3]={2,5,7};
b[3]={6,5,4};
CreateListR(Ha,a,3);
printf("第一个多项式:\n");
DispList(Ha);
CreateListR(Hb,b,3);
printf("第二个多项式:\n");
DispList(Hb);
PlusList(Ha,Hb,Hc);
printf("相加的结果:\n");
DispList(Hc);
getchar();
}
哈哈,帮忙看看啊