If i want to search through the linked list and delete a node by last name and first name, how would i go about comparing string and delete the node.
This is the function to delete:
1 2 3 4 5 6 7 8 9
/*function deletes members from list*/
struct member_account *delete_from_list(struct member_account *list) {
struct member_account *current;
current = list->next;
free(list);
return(current);
} // end delete_from_head
The delete function i created only deletes the node at the beginning but i have to search to the list and delete nodes by last name and first name. So how would i delete by last name and first name.
struct member_account *delete_from_list(struct member_account *list, char *last, char *first)
{
struct member_account *current = list;
struct member_account *prev = list;
bool found = false;
while(current != NULL)
{
// this is wrong: if((strc(current->member_last,last)==last && (strc(current->member_first,first)==first)
// what I put is the c way of doing a string compares
if( (strcmp(current->member_last, last) == 0)&&(strcmp(current->member_first, first)== 0) )
{
found = true;
if(current == list) // case for hitting the top element in the list
{
list = current->next;
free(current);
current = list;
prev = list;
}
else // we are not at the top of the list
{
prev->next = current->next;
free(current);
current = prev->next;
}
}
else
{
// we didn't match our search criteria...
prev = current;
current = current->next;
}
}// the while.
if(!found)
{
printf("No such entry to delete");
return NULL;
}
else
{
// not sure what the function would return
// if I was returning the deleted element I couldn't free(deallocate) it in this function
return NULL;
}
}// end of function.
It seems to be the week of Linked lists all week.
both in c and c++
can someone tell me why i cant delete the first node of the list. I can delete from middle and end of the list but when i try to delete the very first node it wouldn't do it.
@Azagaros: "It seems to be the week of Linked lists all week."
School has just started again in most of Europe. Week one of any computer science/applied engineering study usually contains an introduction to "Algorithms and Data Structures", of which the start is Linked Lists.