《一元多项式相加C语言代码.docx》由会员分享,可在线阅读,更多相关《一元多项式相加C语言代码.docx(4页珍藏版)》请在三一办公上搜索。
1、一元多项式相加C语言代码数据结构作业 一元多项式相加C语言代码 #include #include typedef struct node int exp,coef; struct node *link; PolyNode,*Polylinklist; Polylinklist Creat(int n) Polylinklist p,r=NULL,list=NULL; int coef,exp,i; for(i=1;icoef=coef; p-exp=exp; p-link=NULL; if(list=NULL) list=p; else r-link=p; r=p; return(list)
2、; Polylinklist ATTACH(int coef,int exp,Polylinklist r) Polylinklist w; w=(Polylinklist)malloc(sizeof(PolyNode); w-exp=exp; w-coef=coef; r-link=w; return w; Polylinklist PADD(Polylinklist a,Polylinklist b) Polylinklist c; Polylinklist r,p=a,q=b; int x; c=(Polylinklist)malloc(sizeof(PolyNode); r=c; wh
3、ile(p!=NULL&q!=NULL) if(p-exp=q-exp) x=p-coef+q-coef; if(x!=0) r=ATTACH(x,q-exp,r); p=p-link; q=q-link; else if(p-expexp) r=ATTACH(q-coef,q-exp,r); q=q-link; else r=ATTACH(p-coef,p-exp,r); p=p-link; while(p!=NULL) r=ATTACH(p-coef,p-exp,r); p=p-link; while(q!=NULL) r=ATTACH(q-coef,q-exp,r); q=q-link;
4、 r-link=NULL; p=c; c=c-link; free(p); return c; void Result(Polylinklist w) Polylinklist m; m=w; while(w=NULL) printf(0); break; while(w!=NULL) if(w-exp=0) printf(%d,w-coef); else printf(%d*x%d,w-coef,w-exp); w=w-link; while(w!=NULL) if(w-coef0) if(w-exp=0) printf(+%d,w-coef); else printf(+%d*x%d,w-
5、coef,w-exp); else if(w-exp!=0) printf(%d*x%d,w-coef,w-exp); else printf(%d,w-coef); w=w-link; void main Polylinklist c=NULL; PolyNode *Lengtha; PolyNode *Lengthb; int a1,b1; printf(Please input as Length:); scanf(%d,&a1); Lengtha=Creat(a1); printf( a=); Result(Lengtha); printf(n); printf(Please input bs Length:); scanf(%d,&b1); Lengthb=Creat(b1); printf( b=); Result(Lengthb); printf(n); c=PADD(Lengtha,Lengthb); printf(n); printf( c=); Result(c); printf(n);