I want to search in a linked list that stores names, I created the function but the printf
statements that output if the element was found or not don't print anything. The code seems correct, I tried to debug it line by line but I couldn't figure it out.
Here's the function:
void exist(Name **head) {
Name *ptr = malloc(sizeof(Name));
printf("Enter your name to search:\n");
fgets(ptr->name, 50, stdin);
Name *p = *head;
bool found = false;
while (p != NULL) {
if (strcmp(ptr->name, p->name) == 0) {
printf("Node found\n");
found = true;
break;
}
p = p->next;
}
if (!found)
printf("Node not found\n");
}
and the structure:
typedef struct Name {
char name[50];
struct Name *next;
} Name;
this is the main
function:
int main() {
Name *head = NULL;
Name *newNode = malloc(sizeof(Name));
strcpy(newNode->name, "John");
newNode->next = NULL;
exist(head);
}