c - 在 C 中搜索链表

标签 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", 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 是否正常工作时,我已经标记了一些东西。

最佳答案

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

while (ptrTemp)

否则它不会看起来是列表中的最后一个元素

关于c - 在 C 中搜索链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21918945/

相关文章:

c - 为什么Linux内核中没有wait_event_..._irqsave()函数或宏?

c++ - 将指针转换为其他指针

python - 如何在Python中向索引添加特定数字?

c# - 可以使用相同的列表,但仍然是空的

python - 如何让 Python 区分相等的整数

python - 我应该使用 list() 还是用括号括起来?

c# - 将数据集转换为列表

c - 我无法理解 MIPS 问题中的部分代码

c - 用 C 语言编写打印方法

ios - 在 Objective-C 中从 NSarray 中获取字符