C语言单链表实现多项式相加
本文实例为大家分享了C语言单链表实现多项式相加的具体代码,供大家参考,具体内容如下
//多项式的相加和相乘 #include#include #pragmawarning(disable:4996)//兼容scanf typedefstructnode{ intcoef; intexpon; structnode*link; }Polynode,*Polynomial; PolynomialInsertPolyLinklist(Polynomialin,PolynomialPread){ Pread->link=in; Pread=in; in->link=NULL; returnPread; } PolynomialReadPoly(void){ PolynomialPread=(Polynomial)malloc(sizeof(Polynode)); Pread->link=NULL; PolynomialH=Pread; intN; scanf("%d",&N); while(N--){ Polynomialp=(Polynomial)malloc(sizeof(Polynode)); scanf("%d%d",&p->coef,&p->expon); Pread=InsertPolyLinklist(p,Pread); } PolynomialF; F=H->link; free(H); returnF; } voidPrintPoly(PolynomialF){ while(F!=NULL){ printf("%d%d",F->coef,F->expon); F=F->link; } printf("\n"); } PolynomialAdd(Polynomialp1,Polynomialp2){ Polynomialt1=p1,t2=p2; Polynomialp=(Polynomial)malloc(sizeof(Polynode)); p->link=NULL; Polynomialq=p; Polynomialread; while(t1&&t2){ if(t1->expon==t2->expon){ if(t1->coef+t2->coef){ t1->coef=t1->coef+t2->coef; t1->expon=t1->expon; read=t1; q->link=read; q=read; t1=t1->link; t2=t2->link; } } else{ if(t1->expon>t2->expon){ read=t1; q->link=read; q=read; t1=t1->link; } else{ if(t1->expon expon){ read=t2; q->link=read; q=read; t2=t2->link; } } } } if(t1){ q->link=t1; } if(t2){ q->link=t2; } PolynomialF=p->link; free(p); returnF; } intmain(void){ Polynomialp1,p2,pp,ps; p1=ReadPoly(); PrintPoly(p1); p2=ReadPoly(); PrintPoly(p2); pp=Add(p1,p2); PrintPoly(pp); //ps=Mult(p1,p2); //PrintPoly(ps); return0; }
参考
MOOC浙大数据结构
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。