在结构列表中搜索字符串

时间:2016-12-17 13:52:05

标签: c search structure

我制作了一个结构列表和两个字符串,现在我想要搜索和打印teh结构,其中搜索字符串作为第一个字符串中的子字符串。  我的代码:

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

struct test_struct
{
    char * first;
    char * second;
    struct test_struct *next;
};

struct test_struct *head = NULL;
struct test_struct *curr = NULL;

struct test_struct *create_list(char *first, char *second)
{
    printf("First structure %s: %s\n", first, second);
    struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
    if(NULL == ptr)
    {
        printf("\n Node creation failed \n");
        return NULL;
    }
    ptr->first = first;
    ptr->second = second;
    ptr->next = NULL;

    head = curr = ptr;
    return ptr;

}

struct test_struct* add_to_list(char *first, char *second)
{
    if(NULL == head)
    {
        return (create_list(first, second));
    }

    printf("\n Added to list %s: %s\n", first, second);

    struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
    if(NULL == ptr)
    {
        printf("\n Node creation failed \n");
        return NULL;
    }
    ptr->first = first;
    ptr->second = second;
    ptr->next = NULL;

    curr->next = ptr;
    curr = ptr;

    return ptr;
}

void print_list(void)
{
    struct test_struct *ptr = head;

    printf("\n -------Printing list Start------- \n");
    while(ptr != NULL)
    {
        printf("Položka seznamu %s: %s\n", ptr->first, ptr->second);
        ptr = ptr->next;
    }
    printf("\n -------Printing list End------- \n");

    return;
}

int search_in_list(char *search, struct test_struct **prev)
{
    struct test_struct *ptr = head;
    struct test_struct *tmp = NULL;
    int found = 0;
    char *p;

    printf("\n Searching the list for value [%s] \n",search);

    while(ptr != NULL)
    {
        p = strstr(ptr->first, search);
        if(p)
        {
            found = 1;
            printf("Member of list %s: %s\n", ptr->first, ptr->second);
        }
        else
        {
            tmp = ptr;
            ptr = ptr->next;
        }
    }

    return found;
}

int main()
{

    char first[] = "54545";
    char second[] = "Big Boss";

    char first2[] = "78945";
    char second2[] = "Small Boss";

    char first3[] = "782355";
    char second3[] = "Medium Boss";

    char search[] = "45";

    add_to_list(first, second);
    add_to_list(first2,second2);
    add_to_list(first3, second3);
    print_list();

    search_in_list(search, head);

    return 1;
}

但是我的代码有一些我想要打印的错误 54545:大老板 78945:小老板

但我的代码打印无休止重复第一行。哪里有我的错误?

感谢@Matthias我编辑搜索功能

int search_in_list(char *search, struct test_struct **prev)
{
    struct test_struct *ptr = head;
    struct test_struct *tmp = NULL;
    int found = 0;
    char *p;

    printf("\n Searching the list for value [%s] \n",search);

    while(ptr != NULL)
    {
        p = strstr(ptr->first, search);
        if(p)
        {
            found = 1;
            printf("Member of list %s: %s\n", ptr->first, ptr->second);

        }
            tmp = ptr;
            ptr = ptr->next;

    }

    return found;
}

现在似乎打印成员我的预期

1 个答案:

答案 0 :(得分:0)

如果可以找到子字符串,则search_in_list()中的while循环是无限循环,因为在这种情况下,“ ptr”将永远不会为NULL。您可以省略“ found”变量,而只需在printf()之后添加“ return 1”,然后在函数末尾添加“ return 0”。 –马蒂亚斯(Matthias)

感谢@Matthias,我将搜索功能编辑为

int search_in_list(char *search, struct test_struct **prev)
{
    struct test_struct *ptr = head;
    struct test_struct *tmp = NULL;
    int found = 0;
    char *p;

    printf("\n Searching the list for value [%s] \n",search);

    while(ptr != NULL)
    {
        p = strstr(ptr->first, search);
        if(p)
        {
            found = 1;
            printf("Member of list %s: %s\n", ptr->first, ptr->second);

        }
            tmp = ptr;
            ptr = ptr->next;

    }

    return found;
}

现在似乎可以打印出成员的期望值了– mardon