一元稀疏多项式 链表
帮忙看下表的创建和合并(相加)#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct LNode
{
int coef;
int expn;
struct LNode *next;
}LNode,*LInkList;
ListNode CreateFromTail(LinkList *L)
{
int m;
int n;
l=(LinkList)malloc(sizeof(LinkList));
l->next=NULL;
tail=l;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
while(m)
{
p=(LinkList)malloc(sizeof(LinkList));
p->coef=m;
p->expn=n;
tail->next=p;
tail=p;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
}
tail->next=NULL;
return l;
}
ListNode MergeList(LinkList *la,LinkList *lb)
{
ListNode pa,pb,temp;
int sum=0;
pa=la->next;
pb=la->next;
while(pa&&pb)
{
if(pa->expn > pb->expn)
{
pa=pa->next;
}
else if(pa->expn < pb->expn)
{
temp=pa->next;
pa->next=pb;
pa=temp;
pb=pb->next;
}
else
{
if((pa->coef+pb->coef)!=0)
{
sum=pa->coef+pb->coef;
pa->coef=sum;
pa=pa->next;
pb=pb->next;
}
else
{
pa=pa->next;
pb=pb->next;
}
}
}
if(pb)
ra->next=pb;
free(lb);
return la;
}
void Display(LinkList *L)
{
LNode p;
p=L->next;
if(p!=NULL)
{
if(p->coef<0)
printf("(%d)*x^%d",p->coef,p->expn);
else
printf("%d*x^%d",p->coef,p->expn);
p=p->next;
}
if(p!=NULL)
{
if(p->coef<0)
printf("+(%d)*x^%d",p->coef,p->expn);
else
printf("+%d*x^%d",p->coef,p->expn);
p=p->next;
}
}
int main()
{
LinkList m,n,l;
printf("the first equation:");
CreateFromTail(m);
Display(l);
printf("\n");
printf("the second equation:");
CreateFromTail(n);
Display(l);
printf("\n");
l=MergeList(m,n);
Display(l);
return 0;
}