全文预览

数据结构-利用链表计算一元多项式-课程设计-实验报告

上传者:hnxzy51 |  格式:doc  |  页数:13 |  大小:0KB

文档介绍
malloc(sizeof(struct Polynomial));//建立头结点Р hc->next=NULL;Р headc=hc;Р while(qa||qb){Р qc=(Polyn)malloc(sizeof(struct Polynomial));Р pare(qa,qb)){Р case 1:Р {Р qc->coef=qa->coef;Р qc->expn=qa->expn;Р qa=qa->next;Р break;Р }Р case 0:Р { Р qc->coef=qa->coef+qb->coef;Р qc->expn=qa->expn;Р qa=qa->next;Р qb=qb->next;Р break;Р }Р case -1:Р {Р qc->coef=qb->coef;Р qc->expn=qb->expn;Р qb=qb->next;Р break;Р } Р }//switchР if(qc->coef!=0){Р qc->next=hc->next;Р hc->next=qc;Р hc=qc;Р }Р else free(qc);//当相加系数为0时,释放该结点Р }//whileР return headc;Р}//AddPolynРPolyn SubtractPolyn(Polyn pa,Polyn pb){//求解并建立多项式a+b,返回其头指针Р Polyn h=pb;Р Polyn p=pb->next;Р Polyn pd;Р while(p){ //将pb的系数取反Р p->coef*=-1;Р p=p->next;Р }Р pd=AddPolyn(pa,h);Р for(p=h->next;p;p=p->next) //恢复pb的系数Р p->coef*=-1;Р return pd;Р}//SubtractPolyn

收藏

分享

举报
下载此文档