2016-03-28 107 views
0

我在如何傳遞指針以便能夠根據主函數中的頭來搜索鏈表。它意味着是一個包含多項式的係數和指數的結構。在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;*/ 
} 
+0

所以你想,如果再次檢查'PolyTerm'使用'findTerm'功能正確已經存在在你的'pPolyAHead'名單? – JanLeeYu

+0

是的。我只是不想說一個是3x^2,另一個是2x^2。但我不知道如何將** ppHead傳遞給搜索函數以檢查等效指數。 –

+0

相關,有什麼可能的意義是發送'pCurr'到'allocateTerm'應該是完成? – WhozCraig

回答

0

我對你的函數做了一些修改,我不確定究竟是哪一個,但大部分原始語法都是完整的。我相信下面的代碼正在實現你的目標。如果缺少某些東西,請留下評論。

#include<stdlib.h> 
#include<stdio.h> 

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[]) 
{ 
    setbuf(stdout, NULL); 

    PolyTerm *pPolyAHead = NULL; 
    PolyTerm *pCurr = NULL; 

    pCurr = allocateTerm(2,3,&pPolyAHead, pCurr); 
    //printf("%d %d %p %p\n",pCurr->iCoeff,pCurr->iExp,pPolyAHead,pCurr->pNext); 
    pCurr = allocateTerm(3,4,&pPolyAHead, pCurr); 
    //printf("%d %d %p\n",pCurr->iCoeff,pCurr->iExp,pCurr->pNext); 
    pCurr = allocateTerm(4,5,&pPolyAHead, pCurr); 
    pCurr = allocateTerm(5,6,&pPolyAHead, pCurr); 

    //printf("main: %p\n", pPolyAHead); 

    for(pCurr = pPolyAHead; pCurr != NULL; pCurr=pCurr->pNext) 
     printf("coeff: %d exp: %d address: %p\n", pCurr->iCoeff, pCurr->iExp, pCurr); 

    //exit(1); 

    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) { 
     // Occurs only on first entry. 
     *ppHead = (PolyTerm *)malloc(sizeof(PolyTerm)); 
     if(*ppHead == NULL) 
      printf("Memory allocation error with ppHead\n"); 

     (*ppHead)->iCoeff = iCoeff; 
     (*ppHead)->iExp = iExp; 
     (*ppHead)->pNext = NULL; 
     return *ppHead; 
    } else { 
     // Search to see if that exponent already exists 
     //printf("allocate: %p\n", *ppHead); 
     pFind = findTerm(iCoeff, iExp, ppHead); 
     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 **ppHead) 
{ 
    //printf("find: %p\n", *ppHead); 

    PolyTerm *pFind; 
    for(pFind = *ppHead; pFind->pNext != NULL; pFind = pFind->pNext) { 
     //printf("find: iExp %d %d\n",pFind->iExp, iExp); 
     if(pFind->iExp == iExp) { 
      //printf("find: returned\n"); 
      return pFind; 
     } 
    } 
    //printf("find: not found\n"); 
    return NULL; 
} 

輸出:

coeff: 2 exp: 3 address: 001B1C28 
coeff: 3 exp: 4 address: 001B1C40 
coeff: 4 exp: 5 address: 001B1C58 
coeff: 5 exp: 6 address: 001B1C70 
coeff: 7 exp: 8 address: 001B1668 
coeff: 9 exp: 10 address: 001B1680 
coeff: 11 exp: 12 address: 001B1698 
coeff: 13 exp: 14 address: 001B16B0 
+0

我第一次運行findTerm時仍然遇到了分段錯誤。你能在這裏運行代碼嗎? –

+0

是的,我在代碼下面發佈了輸出。 – PZwan

0

我對此沒有真正的信心,但只是試試這個。

PolyTerm *findTerm(int iCoeff, int iExp, PolyTerm **pHead) 
{ 
    //process here ... 
} 

然後就這樣稱呼它:

pFind = findTerm(iCoeff, iExp, ppHead); 

你有一個參考再次傳遞給您的pPolyAHead,這樣你仍然可以訪問,並比較其價值。 希望這有助於。

+0

剛剛嘗試過,它仍然存在分段錯誤。我相信這只是傳遞ppHead時導致內存錯誤的語法錯誤,因爲如果我將findTerm的調用註釋掉,一切正常。 –