c - 为什么“搜索”功能不运行?
问题描述
我编写了一个函数来搜索单链表中的节点。当我运行它时,它会在打印节点存在或不存在时卡住。代码中没有警告,所以我想知道我的函数调用概念是否有问题。
我知道它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);
}
}
解决方案
您的代码有很多问题,导致您的代码无法正常工作:
#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);
}
}
上面的代码是修复示例
推荐阅读
- android - 向下滑动,向上动画仅在片段中第一次起作用
- java - JobIntentService onHandleWork 未被调用
- ios - 快速从 ID 元素获取 innerText 不能与 webView 一起使用
- azure-data-factory-2 - How to send the parameter values to azure data factory using powershell
- python - 使用 for 和 if 循环将不同长度的元素组合到一个 Python 列表中
- android - 为什么在将 Android Path 对象添加到数组后,还可以向其添加更多行?
- angular - Angular 2+ 动画:向循环生成的单个复选框添加复选框检查效果
- java - 使用 JAX-RS 2.0 客户端 API 将文件作为表单参数之一的输入发布
- c# - 模拟存根和起订量设置的行为不同?Moq 中存根的等价物是多少?
- c++ - c ++值未保存到指针(二叉搜索树)