我正在努力如何传递指针,以便能够根据主函数中的头部搜索链表。它意味着是包含多项式的系数和指数的结构。在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 :(得分:0)
我对此并不是很有信心,但无论如何都要尝试。
PolyTerm *findTerm(int iCoeff, int iExp, PolyTerm **pHead)
{
//process here ...
}
然后就这样称呼它:
pFind = findTerm(iCoeff, iExp, ppHead);
您必须再次向pPolyAHead
传递引用,以便您仍然可以访问并比较其值。
希望这会有所帮助。
答案 1 :(得分: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