delete

hello,

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.

Last edited on
can someone please give me a hint on how to search by last name and delete from the link
Just loop through your list (while (current = current->next)) until the names are found?
thanks for your replay. Can someone explain to me how would i string compare to match the node that need to be deleted. THANKS
There's string::compare:
http://www.cplusplus.com/reference/string/string/compare/

Although I think C++ strings even have the '==' operator (but I'm not sure).
this is what i did to delete from the head if the search result is in the head, but it doesn't work. It seems right to me.



can someone tell me why this function isn't working. I am trying to search byy last and first to delete from the list if it matches. THANKS
Last edited on
can someone tell me why the function isn't working. It seem correct but not sure what i did wrong
first off the function would look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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++

hope that helps
thanks for your help. How would i delete from the head of the list or any where from the list. THANKS YOU
Last edited on
My delete function only deletes from the end of the list but wouldn't delete from head or anywhere else. Can anyone else help me.

1
2
3
4
/
struct member_account *delete_from_list(struct member_account *list, char *last, char *first) {
	
}// end delete_from_list  


Last edited on
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.
It's probably because list isn't passed by reference.

What you should do is this:
struct member_account *delete_from_list(struct member_account **list /* passing address of head */, ...)
so if i do this void delete_from_list(struct member_account **list, char *last, char *first)

then the list should be like this:
1
2
3
struct member_account *current=*list;
    struct member_account *prev = *list;
	

and so on for the list

when i do this and this is used to call the delete_from_list function in my main function which is: delete_from_list(head, last, first);

i get and warning saying passing arg 1 of delete_from_list from incompatible pointer type.
Last edited on
You should do this instead: delete_from_list(&head, last, first);

The & is the "address of" operator.
THANK YOU shacktar
@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.
Topic archived. No new replies allowed.