C 在结构列表中搜索字符串
我制作了一个结构列表,有两个字符串,现在我想要搜索并打印结构,其中第一个字符串中有搜索字符串作为子字符串。 我的代码:C 在结构列表中搜索字符串,c,search,structure,C,Search,Structure,我制作了一个结构列表,有两个字符串,现在我想要搜索并打印结构,其中第一个字符串中有搜索字符串作为子字符串。 我的代码: # include <stdio.h> # include <stdlib.h> #include <string.h> struct test_struct { char * first; char * second; struct test_struct *next; }; struct test_struct
# 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;
}
现在,它似乎打印出了我所期望的成员如果可以找到子字符串,则search_in_list()中的while循环是一个无限循环,因为在这种情况下“ptr”永远不会为NULL。您可以省略“found”变量,只需在printf()后面加上“return 1”,并在函数末尾加上“return 0”马蒂亚斯 感谢@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;
}
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如果可以找到子字符串,则search_in_list()中的while循环是一个无限循环,因为在这种情况下“ptr”永远不会为NULL。您可以省略“found”变量,只需在printf()后面加上“return 1”,并在函数末尾加上“return 0”马蒂亚斯 感谢@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;
}
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如果可以找到子字符串,则search_in_list()中的while循环是一个无限循环,因为在这种情况下“ptr”永远不会为NULL。您可以省略“found”变量,只在printf()后面“return 1”,在函数末尾“return 0”。如果可以找到子字符串,则search_in_list()中的while循环是无限循环,因为在这种情况下“ptr”永远不会为NULL。可以省略“found”变量,只在printf()后面加上“return 1”,在函数末尾加上“return 0”。