單鏈表實現多項式相乘,有這樣的一個思路可以參考:
實現多項式相乘,最關鍵的是系數和指數的兩個數據,這里命名為coef和HighPower。
最簡便的辦法是使用兩個嵌套循環例如(3x^2+4x^1)(x^2+2x^4)用3x^2遍歷另外一個括號內的數據,同時實現本身括號內的遍歷。
這個想法的核心程序可歸納為以下:
while(pList!=NULL){ while(pList2!=NULL){ pNew=(PNODE)malloc(sizeof(NODE)); pNew->pNext=NULL; pNew->coef=(pList->coef)*(pList2->coef); pNew->HighPower=(pList->HighPower)+(pList2->HighPower); pReslut=addNewItem(pReslut,pNew); pList2=pList2->pNext; } pList2=pHead2; pList=pList->pNext; }
這樣即可實現將所有數據,輸出到一個新的鏈表里面。
但同時這也帶來了不少問題,怎樣才能合並新的鏈表里面相同系數的參數呢?
這里可以使用這樣的方法:
創捷一個新表New,新表New2,將原有的表拆分循環復制在NEW2,然后將NEW2復制給NEW,如果在NEW中有相同項,則直接相加或者相乘,如果沒有,則在鏈表最后添加新的節點。這樣即可實現合並單鏈表中相同系數的參數。
源碼如下:
#include<stdio.h> #include<stdlib.h> typedef struct node{ int coef; int HighPower; struct node *pNext; }NODE,*PNODE; PNODE crateList(); void traverseLinkList(PNODE); PNODE caculate(PNODE,PNODE); PNODE addNewItem(PNODE ,PNODE ); PNODE total(PNODE); PNODE ListFind(PNODE,PNODE); int main(){ PNODE pList,pList2,pNew; pList=crateList(); getchar(); pList2=crateList(); pNew=caculate(pList,pList2); pNew=total(pNew); traverseLinkList(pNew); return(0); } PNODE crateList(){ int a,b,c; PNODE pCurrent,pPre,pHead=NULL; pCurrent=NULL;pPre=NULL; b=0,c=0; while(1){ if(scanf("%d %d",&b,&c)!=2) break; pCurrent=(PNODE)malloc(sizeof(NODE)); if(pHead==NULL) pHead=pCurrent; else pPre->pNext=pCurrent; pCurrent->pNext=NULL; pCurrent->coef=b; pCurrent->HighPower=c; pPre=pCurrent; } puts("input end,now next step"); return(pHead); } void traverseLinkList(PNODE pList){ PNODE pCurrent; pCurrent=pList; if(pCurrent==NULL) puts("No Data Record"); else while(pCurrent!=NULL){ printf("the Coef is %d,and HighPower is %d\n",pCurrent->coef,pCurrent->HighPower); pCurrent=pCurrent->pNext; } } PNODE caculate(PNODE pList,PNODE pList2){ PNODE pNew,pCurrent,pPre,pHead2=pList2,pReslut=NULL; if(!pList||!pList2){ puts("wrong!"); return(0); } while(pList!=NULL){ while(pList2!=NULL){ pNew=(PNODE)malloc(sizeof(NODE)); pNew->pNext=NULL; pNew->coef=(pList->coef)*(pList2->coef); pNew->HighPower=(pList->HighPower)+(pList2->HighPower); pReslut=addNewItem(pReslut,pNew); pList2=pList2->pNext; } pList2=pHead2; pList=pList->pNext; } return(pReslut); } PNODE addNewItem(PNODE pReslut,PNODE pAdd){ PNODE pHead=NULL,pCurrent; if(pReslut==NULL){ return(pAdd); } pHead=pReslut; pCurrent=pReslut; while(pCurrent->pNext!=NULL) pCurrent= pCurrent->pNext; pCurrent->pNext=pAdd; pAdd->pNext=NULL; return(pHead); } PNODE total(PNODE pNew){ PNODE pNew3,pCurrent,pPre,pHead,pNew2=NULL; pCurrent=pNew; if(!pNew) return(0); pNew3=(PNODE)malloc(sizeof(NODE)); pNew2=(PNODE)malloc(sizeof(NODE)); pNew3->pNext=NULL; while(pCurrent!=NULL){ pNew3->coef=pCurrent->coef; pNew3->HighPower=pCurrent->HighPower; pNew2=ListFind(pNew2,pNew3); if(!pHead) pHead=pNew; pCurrent=pCurrent->pNext; } return(pHead); } PNODE ListFind(PNODE pNew,PNODE pList){ PNODE pCurrent,pPre,pHead,pCurrent2; while(pCurrent!=NULL){ if(pCurrent->HighPower==pList->HighPower){ pCurrent->coef=(pCurrent->coef)*(pList->coef); pCurrent->HighPower=pCurrent->HighPower+pList->HighPower; return(pHead); } pCurrent=pCurrent->pNext; } pCurrent=(PNODE)malloc(sizeof(NODE)); if(!pHead) pHead=pCurrent; else pPre->pNext=pCurrent; puts("ok"); pCurrent->pNext=NULL; pCurrent->coef=pList->coef; pCurrent->HighPower=pList->HighPower; pPre=pCurrent; return(pHead); }