我在如何傳遞指針以便能夠根據主函數中的頭來搜索鏈表。它意味着是一個包含多項式的係數和指數的結構。在allocateTerm函數中,我試圖將雙指針傳遞給另一個函數來搜索並查看我即將創建的術語是否已在多項式中具有共同項。代碼如下:將雙指針傳遞給另一個函數
typedef struct PolyTerm{
int iCoeff;
int iExp;
struct PolyTerm *pNext;
}PolyTerm;
PolyTerm *allocateTerm(int iCoeff, int iExp, PolyTerm **ppHead, PolyTerm *pCurr);
PolyTerm *findTerm(int iCoeff, int iExp, PolyTerm ***ppHead);
void main(int argc, char *argv[])
{
PolyTerm *pPolyAHead = NULL;
PolyTerm *pCurr = NULL;
allocateTerm(2,3,&pPolyAHead, pCurr);
pCurr = allocateTerm(3,4,&pPolyAHead, pCurr);
pCurr = allocateTerm(4,5,&pPolyAHead, pCurr);
pCurr = allocateTerm(5,6,&pPolyAHead, pCurr);
printf("%p\n", pPolyAHead);
for(pCurr = pPolyAHead; pCurr != NULL; pCurr=pCurr->pNext)
printf("coeff: %d exp: %d address: %p\n", pCurr->iCoeff, pCurr->iExp, pCurr);
PolyTerm *pPolyBHead = NULL;
allocateTerm(7,8,&pPolyBHead, pCurr);
pCurr = allocateTerm(9,10,&pPolyBHead, pCurr);
pCurr = allocateTerm(11,12,&pPolyBHead, pCurr);
pCurr = allocateTerm(13,14,&pPolyBHead, pCurr);
for(pCurr = pPolyBHead; pCurr != NULL; pCurr=pCurr->pNext)
printf("coeff: %d exp: %d address: %p\n", pCurr->iCoeff, pCurr->iExp, pCurr);
}
PolyTerm *allocateTerm(int iCoeff, int iExp, PolyTerm **ppHead, PolyTerm *pCurr)
{
PolyTerm *pFind = NULL;
if(*ppHead == NULL)
{
*ppHead = (PolyTerm *)malloc(sizeof(PolyTerm));
if(*ppHead == NULL)
{
printf("Memory allocation error with ppHead\n");
}
(*ppHead)->iCoeff = iCoeff;
(*ppHead)->iExp = iExp;
(*ppHead)->pNext = NULL;
}
else
{
// Search to see if that exponent already exists
printf("%p\n", *ppHead);
pFind = findTerm(iCoeff, iExp, /*????? (some form of **pHead)*/);
if(pFind == NULL)
{
PolyTerm *pNew = NULL;
pNew = (PolyTerm *)malloc(sizeof(PolyTerm));
if(pNew == NULL)
printf("Memory allocation error with pNew\n");
if((*ppHead)->pNext == NULL)
{
(*ppHead)->pNext = pNew;
}
else
{
pCurr->pNext = pNew;
}
pNew->iCoeff = iCoeff;
pNew->iExp = iExp;
pNew->pNext = NULL;
}
else
{
pFind->iCoeff += iCoeff;
}
}
}
PolyTerm *findTerm(int iCoeff, int iExp, PolyTerm ?????/*????? (some form of **pHead)*/)
{
//printf("%p", &head);
/*
PolyTerm *pFind = NULL;
for(pFind = (*ppHead); pFind != NULL; pFind = (*ppHead)->pNext)
{
if(pFind->iExp == iExp)
return pFind;
}
return NULL;*/
}
所以你想,如果再次檢查'PolyTerm'使用'findTerm'功能正確已經存在在你的'pPolyAHead'名單? – JanLeeYu
是的。我只是不想說一個是3x^2,另一個是2x^2。但我不知道如何將** ppHead傳遞給搜索函數以檢查等效指數。 –
相關,有什麼可能的意義是發送'pCurr'到'allocateTerm'應該是完成? – WhozCraig