codegeek123
codegeek123

Reputation: 43

Linked List delete function issue in C

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;
}

Upvotes: 0

Views: 50

Answers (2)

blindpirate
blindpirate

Reputation: 114

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

Upvotes: 1

John3136
John3136

Reputation: 29266

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

Upvotes: 1

Related Questions