Linked List, a pointer problem?
Hello, I'm new to linked lists and I'm in a bit of a bind. I need to check to see if something already exists in the list before adding it. If it does exist, it should not be added. The problem is if you add it the first time, it adds to the list. If you add it a second time it adds to the list, which it shouldn't. If you add it a third time, it will tell you it was not added...I think I need to add an if statement for this specific situation, or change the while parameters...here's what I have...
Code:
// Check if car already exists
if ((*head) == NULL) // Add car to empty list
;
else
{
crnt = (*head); // Point to beginning of list
while(crnt->next != NULL)
{
if (strcmp(plate, crnt->plate) == 0) /* plate is what user just entered,
crnt->plate is what's already in list */
{
printf("\nCar %s not added.\nThis car already exists!\n", plate);
return;
}
crnt = crnt->next;
}
}
Help?