首页 > 解决方案 > 为什么“搜索”功能不运行?

问题描述

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

我知道它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);
    }

}

标签: csingly-linked-listfunction-call

解决方案


您的代码有很多问题,导致您的代码无法正常工作:

#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);
    }

}

上面的代码是修复示例


推荐阅读