i created a function to detect if a word does exist in a linked list with recursion.
i wanted to know if its correct or not .
bool does_exist_in_list(index *head,char word[25]){
while( head != NULL ){
//to detect the first element
if(strcmp(word,head->word) == 0)
return true;
else{
//to go to the next element
return does_exist_in_list(head->next,word);
}
}
}
because im using it in a long code so i dont know if there is a problem in it
CodePudding user response:
The while
loop is misleading, as the function always returns during the first iteration.
Additionally, if head
is NULL, the function fails to return a value at all, invoking Undefined Behaviour.
bool does_exist_in_list(index *head, char word[25])
{
if (!head)
return false;
if (0 == strcmp(word, head->word))
return true;
return does_exist_in_list(head->next, word);
}