Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中搜索链接列表_C_List - Fatal编程技术网

在C中搜索链接列表

在C中搜索链接列表,c,list,C,List,我试图用c语言搜索一个链接列表,我可以让它匹配我的搜索字符串和第一个节点,但不匹配下一个节点,我不知道为什么。这是我的密码: void fnSearchList(struct listnode *ptrH, char strName[50]) { struct listnode *ptrTemp = ptrH; int nCount = 0; // nRet = strcmp(strName, ptrH->arcFirstName); // printf("%i", n

我试图用c语言搜索一个链接列表,我可以让它匹配我的搜索字符串和第一个节点,但不匹配下一个节点,我不知道为什么。这是我的密码:

void fnSearchList(struct listnode *ptrH, char strName[50])
{
    struct listnode *ptrTemp = ptrH;
    int nCount = 0;
//  nRet = strcmp(strName, ptrH->arcFirstName);
//  printf("%i", nRet);
    if(!ptrH)
    {
        /* Empty List */
        printf("\n\nEmpty List \n\n");
    }
    else
    {
        while(ptrTemp->ptrNext)
        {
            nRet = strcmp(strName, ptrTemp->arcFirstName);
            if(nRet == 0)
            {
                printf("The value %s has been located\n", ptrTemp->arcFirstName);
                nCount++;
            }
            ptrTemp = ptrTemp->ptrNext;
        }

        if(!nCount)
            printf("\t\tValue not found within the list\n");
        else
            printf("\t\tA total of %d were found\n", nCount);
    }   
    printf("The list totals %d\n", fnTotalList(ptrH));
}

在测试strcmp时,我已经标记了一些东西,以确定strcmp是否正常工作。

我认为您的while循环应该是:

while (ptrTemp)

否则它将不会成为列表中的最后一个元素

我认为您的while循环应该是:

while (ptrTemp)

否则它将不会在列表中的最后一个元素中出现检查
while
循环的条件应该是
while(ptrTemp)
,而不是
while(ptrTemp->ptrNext)
。这是因为您已经通过执行以下操作将
ptrTemp
更改为指向列表中的下一个节点

ptrTemp = ptrTemp->ptrNext;
因此,您的代码跳过列表中的最后一个节点,因为
lastNode->ptrNext==NULL
true
。 另外,请注意,函数
fnSearchList
strName
参数是指向
char
类型的指针,而不是50
char
的数组。你可以这样写:

void fnSearchList(struct listnode *ptrH, char *strName) {
    // stuff
}

它们完全相同。

检查
while
循环的条件应该是
while(ptrTemp)
,而不是
while(ptrTemp->ptrNext)
。这是因为您已经通过执行以下操作将
ptrTemp
更改为指向列表中的下一个节点

ptrTemp = ptrTemp->ptrNext;
因此,您的代码跳过列表中的最后一个节点,因为
lastNode->ptrNext==NULL
true
。 另外,请注意,函数
fnSearchList
strName
参数是指向
char
类型的指针,而不是50
char
的数组。你可以这样写:

void fnSearchList(struct listnode *ptrH, char *strName) {
    // stuff
}
它们完全一样。

变化

while(ptrTemp->ptrNext)  
while(ptrTemp)

while(ptrTemp->ptrNext)  
while(ptrTemp)
更改

while(ptrTemp->ptrNext)  
while(ptrTemp)

while(ptrTemp->ptrNext)  
while(ptrTemp)

一件小事就能让代码正常工作,这不是很疯狂吗?我看到妻子甚至说要试着删除它,但我从来没有这么做过。她应该是那个做编程的人哈哈哈,一件小事就能让代码正常工作,这不是很疯狂吗?我在看,妻子甚至说要试着删除它,但我从来没有这样做过呵呵。她应该是那个做节目的人哈哈哈