多项式ADT的数组实现
/*删除表的正确方法*/ /*assume header*/ void DeleteList(List L) { Position p,Tmp; p=L->Next; while(p != NULL) { Tmp=p->Next; free(p); p = Tmp; } }//先free掉,然后再判断这个结点的Next是不是NULL,是就不做了 /*多项式ADT*/ /*多项式数组实现的类型声明*/ /*一个结构就可以表示。一个多项式*/ typedef struct{ CoeffArray[MaxDegree+1]; int HighPower;//表示这个数组的最高次 } * Polynomial; /*将多项式初始化为0*/ void ZeroPolynomial(Polynomial Poly) { for(i=0;i <= MaxDegree ;i++) { Poly->CoeffArray[i]=0; } Poly->HighPower = 0; } /*两个多项式相加的过程*/ void AddPolynomial(Polynomial Poly1,Polynomial Poly2,Polynomial PolySum) { ZeroPolynomial(PolySum); MaxPower=Max2(Poly1->HighPower,Poly2->HighPower); for(i=0;i<=MaxPower;i++) { PolySum->CoeffArray[i] = Poly1->CoeffArray[i] + Poly2->CoeffAyyay[i]; } PolySum->HighPower=MaxPower; } /*两个多项式相乘的过程*/ void MultPolynomial(Polynomial Poly1,Polynomial Poly2,Polynomial PolyProd) { ZeroPolynomial(PolyProd); PolyProd->HighPower = Poly1->HighPower * Poly2->HighPower; for(int i=0; i <= Poly1->HighPower; i++) { for(int j=0; j <= Poly2->HighPower; j++) { PolyProd->CoeffArray[i+j] += Poly1->CoeffArray[i] * Poly2->CoeffArray[j]; } } }