0

So I have a linkedlist and I am trying to delete the name of the input, but the delete function cannot find the name on the list. I am trying to find the logic flaw, I need some help!

some knowledge to know: name is a character array in the struct

 case 4:
                     if(head == NULL)
                        printf("List is Empty\n");
                    else
                    {
                        printf("Enter the name to delete : ");
                        scanf("%s",&user);

                        if(delete(user))
                            printf("%s deleted successfully\n",user);
                        else
                            printf("%s not found in the list\n",user);
                    }
                    break;




int delete(const char* input)
{
    struct person *temp, *prev;
    temp = head;

    while(temp != NULL)
    {
         if(temp->name == input)
        {
            if(temp == head)
            {
                head = temp->next;
                free(temp);
                return 1;
            }
            else
            {
                prev->next = temp->next;
                free(temp);
                return 1;
            }
        }
        else
        {
            prev = temp;
            temp = temp->next;
        }
    }
    return 0;
}
2

2 Answers 2

1

if(temp->name == input) is just comparing pointers. Try if (strcmp(temp->name, input) == 0)...

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. What does strcmp do that changes the linked list
strcmp() doesn't change the list, it is just the correct way top compare strings in C.
1

Donot use temp->name == input, use strcmp(temp->name, input) see strcmp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.