《数据结构与算法分析 C语言描述》表,栈与队列部分课后习题
- //Code by Pnig0s1992
- //Date:2012,3,22
- #include <stdio.h>
- #include "Header.h"
- void MultPolynomial(LinkList Py1,LinkList Py2,LinkList LinkRc);
- void PrintfPolynomial(LinkList LinkRs);
- int main(int argc,char **argv)
- {
- LinkNode Ploy1;
- Ploy1.pNext = NULL;
- Insert(0,6,&Ploy1);
- Insert(1,5,&Ploy1);
- Insert(2,4,&Ploy1);
- Insert(3,3,&Ploy1);
- LinkNode Ploy2;
- Ploy2.pNext = NULL;
- Insert(0,10,&Ploy2);
- Insert(1,9,&Ploy2);
- Insert(2,8,&Ploy2);
- Insert(3,7,&Ploy2);
- LinkNode LinkResult;
- LinkResult.pNext = NULL;
- MultPolynomial(&Ploy1,&Ploy2,&LinkResult);
- printf("\nPoly1:");
- PrintfPolynomial(&Ploy1);
- printf("\nPloy2:");
- PrintfPolynomial(&Ploy2);
- printf("\nThe Multi result:");
- PrintfPolynomial(&LinkResult);
- system("pause");
- return 0;
- }
- //打印多项式乘法结果
- void PrintfPolynomial(LinkList LinkRs)
- {
- LinkList LinkTemp = LinkRs->pNext;
- while(LinkTemp != NULL)
- {
- printf("+%dX^%d",LinkTemp->Coefficient,LinkTemp->Exponent);
- LinkTemp = LinkTemp->pNext;
- }
- }
- void MultPolynomial(LinkList Py1,LinkList Py2,LinkList LinkRc)
- {
- int iTempExp = 0;
- int iTempCoe = 0;
- LinkList LinkPy1 = Py1->pNext;
- LinkList LinkPy2 = Py2->pNext;
- while(LinkPy2 != NULL)
- {
- LinkPy1 = Py1->pNext;//每次循环回置头结点
- while(LinkPy1 != NULL)
- {
- iTempExp = LinkPy1->Exponent+LinkPy2->Exponent;
- iTempCoe = LinkPy1->Coefficient*LinkPy2->Coefficient;
- if(isEmpty(LinkRc))
- {
- Insert(iTempExp,iTempCoe,LinkRc);
- }else
- {
- LinkList LinkResult = LinkRc->pNext;
- while(LinkResult != NULL)
- {
- if(LinkResult->Exponent == iTempExp)
- {
- LinkResult->Coefficient += iTempCoe;
- break;
- }else if((LinkResult->Exponent < iTempExp && LinkResult->pNext->Exponent > iTempExp) || isLast(LinkResult))
- {
- Insert(iTempExp,iTempCoe,LinkResult);
- break;
- }
- LinkResult = LinkResult->pNext;
- }
- }
- LinkPy1 = LinkPy1->pNext;
- }
- LinkPy2 = LinkPy2->pNext;
- }
- }
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/813245,如需转载请自行联系原作者