为什么 `search` 功能没有 运行?

Why does the `search` function doesn't run?

我写了一个函数来搜索单向链表中的节点。当我 运行 它时,它在打印节点存在或不存在时卡住了。代码中没有任何警告,所以我想知道我的函数调用概念是否有问题。

我知道curr->CharArr[9]不应该在函数调用中,但是如果我不用它应该用什么替换呢?还是真的可以接受?

这是搜索节点的函数调用。

int search(struct LLNode *curr, char* find)
{
    while (curr != NULL)
    {
        if (curr->CharArr[9] == find)
        {
            return 1;
            curr = curr->next;
        }


        else if (curr->CharArr[9] != find)
        {
            return 0;
        }
    }
}

这是 调用它的代码:

printf("\nEnter fruit name to search in the linked list: ");
scanf("%s", find);

int result = search(curr,&find);

if (result == 1)
{
    printf("%s found in the list.\n", find);
}
else if (result == 0)
{
    printf("%s not found in the list.\n", find);
}

这是整个函数:

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

struct LLNode
{
    char *CharArr[10];
    struct LLNode *next;
};

struct LLNode * createNode (char val[])
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    temp-> CharArr[9] = val;
    temp-> next = NULL;
    return (temp) ;
};

int search(struct LLNode *curr, char* find)
{
    while (curr != NULL)
    {
        if (curr->CharArr[9] == find)
        {
            return 1;
            curr = curr->next;
        }


        else if (curr->CharArr[9] != find)
        {
            return 0;
        }
    }
}


int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;
    char find;

    printf("The nodes are:\n");

    head = curr = createNode ("Apple") ;
    printf ("%s\n", curr->CharArr[9]) ;

    curr = curr->next;
    curr = createNode("Orange");

    printf ("%s\n", curr->CharArr[9]) ;

    printf("\nEnter fruit name to search in the linked list: ");
    scanf("%s", find);

    int result = search(curr,&find);

    if (result == 1)
    {
        printf("%s found in the list.\n", find);
    }
    else if (result == 0)
    {
        printf("%s not found in the list.\n", find);
    }

}

我不太确定,但我认为,首先,我在这里看到了无法实现的代码

...   
 if (curr->CharArr[9] == find)
            {
                return 1;
                curr = curr->next;  // right here, return statement before this line
            }
...

如果我没记错的话,在下面的代码中你想比较两个字符数组:

...
if (curr->CharArr[9] == find)
// some code
curr->CharArr[9] != find
...

但它不起作用,因为您在此处使用数组下标运算符 curr->CharArr[9] 并且它只占用 char 数组的第十个符号。如果您需要比较两个字符串,您需要将一个字符串的每个字符与另一个字符串的相应字符进行比较,或者只使用 strcmp() 函数。

首先,结构应该是这样的。

struct LLNode
{
    char *CharArr;
    struct LLNode *next;
};

search 函数需要使用 strcmp 来匹配字符串,当找到匹配时你应该只 return。

int search(struct LLNode *curr, char* find)
{
    while (curr != NULL)
    {
        if (strcmp(curr->CharArr, find) == 0)
            return 1;
        curr = curr->next;
    }
    return 0; 
}

并且在你的main函数中,你需要将head传递给search函数。 curr 指向您插入的最新节点。而curr->next需要先分配才能成为链表的一部分。

    curr->next = createNode("Orange");
    curr = curr->next;

    printf ("%s\n", curr->CharArr) ;

    printf("\nEnter fruit name to search in the linked list: ");
    scanf("%s", find);

    int result = search(head, find);

您的代码有很多问题,导致您的代码无法运行:

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

struct LLNode
{
    char CharArr[10];   // problem 1: use array of char, not array of pointer of char
    struct LLNode *next;
};

struct LLNode * createNode (const char* val)
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    strncpy(temp-> CharArr, val, 9); // problem 2: assign value to array
    temp->CharArr[9] = 0;            // be sure to make it null-terminated
    temp-> next = NULL;
    return (temp) ;
};

int search(struct LLNode *curr, char* find)
{
    while (curr != NULL)
    {
        if (strncmp(curr->CharArr, find, 9) == 0)   // problem 3: the way of comparing string
        {
            return 1;             // problem 4: logic of `search` function. Need follow the logic: return 1 if found. You change `cur` pointer after return is do nothing  
        }
        else
        {
            curr = curr->next;    // move to next element if not found in current node
        }
    }

    return 0;          // return 0 if not found at all
}


int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;
    char find[9] = {0};

    printf("The nodes are:\n");

    head = curr = createNode ("Apple") ;
    printf ("%s\n", curr->CharArr) ;

    curr->next = createNode("Orange");    // problem 5: assign wrong pointer for new element in linked list

    printf ("%s\n", curr->next->CharArr) ;

    printf("\nEnter fruit name to search in the linked list: ");
    scanf("%s", find);

    int result = search(head, find);    // problem 6: pass `head` to search, not `curr`

    if (result == 1)
    {
        printf("%s found in the list.\n", find);
    }
    else if (result == 0)
    {
        printf("%s not found in the list.\n", find);
    }

}

以上代码为修复示例