views:

185

answers:

6

My problem is deleting a node from linked list.

I have two structs :

typedef struct inner_list 
{
 int count;
 char word[100];
 inner_list*next;
} inner_list;
typedef struct outer_list
{
 char word [100];
 inner_list * head;
 int count;
 outer_list * next; 
} outer_list;

My problem is in deleting a node from outer_list linked list. For example when user entered aaa to delete, delete function should find the node with outer_list->word = aaa and delete this node and reconnect the list again. I tried the below code to do this. but After finding and deleting I'm losing the list. I don't know what's wrong. Please notice that outer_list have also a linked list of inner_list inside.

void delnode(outer_list **head,char num[100])//thanks to both Nir Levy and Jeremy P.
{
    outer_list *temp, *m;
    m=temp=*head; /*FIX #1*/
    while(temp!=NULL) {
        if(strcmp(temp->word,num)==0) {
            if(temp==*head) {
                delinner(temp->head); /* FIX#2 */
    *head=temp->next;

                free(temp);
                return;
            } else {
                delinner(temp->head); /* FIX#2 */ 
    m->next=temp->next;

                free(temp);
                return;
            }
        } else {
            m=temp;
            temp= temp->next;
        }
    }
    printf(" ELEMENT %s NOT FOUND ", num);
}
void delinner(inner_list *head) { /* FIX#2 */
    inner_list *temp;
    temp=head;
    while(temp!=NULL) {
        head=temp->next;
        free(temp);
        temp=head;
    }
}

Now my problem is updated. While deleting an element from inner list I am also trying the delete the same element from inner_list too.

For example: - Let's say aaa is an element of outer_list linked list and let's point it with outer_list *p - This aaa can also be in an inner_list linked list too. (it can be in p->head or another innerlist.) Now, the tricky part again. I tried to apply the same rules with outer_list deletion but whenever i delete the head element of inner_list it gives an error. Here is what I tried:

void delnode2(outer_list *up,inner_list **head,char num[100])
{
    inner_list *temp2,*temp, *m;
 outer_list *p;
 p = up;

 while(p!=NULL){m=temp=temp2=p->head; 
    while(temp!=NULL) {
        if(strcmp(temp->word,num)==0) {
            if(temp==(*head)) {
                *head=temp->next;

                free(temp);
                return;
            } else {
                m->next=temp->next;

                free(temp);
                return;
            }
        } else {
            m=temp;
            temp= temp->next;
        }
    }
 p=p->next;
 }
    printf(" ELEMENT %s NOT FOUND ", num);
}

Here i'm trying to send the node and checking all the inner_lists of outer_list elements and perform deletion however when the first element is deleted it crashes. Please ask for further information. I might use very untidy words.

A: 

m pointer is not set to any valid address before you dereference it and your program runs into undefined behavior.

In order to fix your implementation use two pointers - one onto the current element and the other to the previous one and update both of them as you traverse the list. You'll have to treat cases with zero and oneelemenats as special - be careful.

sharptooth
I thought it as temporary pointer.
LuckySlevin
@LuckySlevin:I see, but you need to assign a valid address to a pointer before dereferencing it.
sharptooth
A: 

I don't think you are freeing your inner_list. You will get a memory leak.

Lucas
that sound reasonable, You mean i have to make another delete function for inner_list too?
LuckySlevin
@LuckySlevin: Yes, you are only deleting the pointer `inner_list * head` when you free the struct `outer_list`, but not the memory that the pointer is pointing to. Although I don't think that is the problem you are seeing right now. If you are on Linux I recommend using Valgrind to find memory leaks.
Lucas
The problem is after some tests, deleting the first node make the program crashes :S. for example i'm deleting "aaa" and if its the first node of list, program crashes.
LuckySlevin
A: 

Your if condition that is checking for the word, isn't it to supposed to be the following:

if(strcmp(temp->word, num)==0)

?

Klarth
it was already fixed. I guess i made a mistake while copying it to here. Thanks tough.
LuckySlevin
A: 

try this (only for the outer list, it wont release the inner one):

void delnode(outer_list *head,char num[100]) 
{ 
    outer_list *temp, *m. *helper; 
    temp=head; 
    while(temp!=NULL) 
    { 
        if(strcmp(temp->word,num)==0) 
        { 
            if(temp==head) 
            { 
                head=temp->next; 
                free(temp); 
                return; 
            } 
            else 
            { 
                m = temp;
                temp = temp->next;
                helper->next = temp; //link the previous item
                free(m); 
                return; 
            } 
        }
        else 
        { 
            helper = temp;
            temp= temp->next; 
        } 

    } 
    printf(" ELEMENT %s NOT FOUND ", num); 
} 
rursw1
Nice. You could tidy up the code by changing the first `if` to `if( ... != 0)` and using `continue` after proceeding to the next item - the whole thing will become less nested.
sharptooth
The problem is after some tests, deleting the first node make the program crashes :S. for example i'm deleting "aaa" and if its the first node of list, program crashes.
LuckySlevin
I guess the problem is that the function doesn't modify the head of the list.
sharptooth
I was thinking along the same lines as sharptooth. I'm wondering how many times does the test delete the first node? Is it more than once? I think the problem is that you are using the method to delete the first node in the list and you are trying to update the "head", which is a pointer argument. When exiting from delnode, the caller will still think that the "head" parameter (or whatever pointer the caller is giving it) is still pointing to the original head (which you have just freed).
Klarth
+2  A: 

FIX#1 (optional) - it is a good practice to initialize all variables. note that in this specific case since you have handles the head secanrio then you should not have a problem becuase m is set to temp later on, but still..

FIX#2 - make sure you delete the inner list completely before you free a node.

Here is the code (untested, sorry)

void delnode(outer_list *head,char num[100])
{
    outer_list *temp, *m;
    m=temp=head; /*FIX #1*/
    while(temp!=NULL) {
        if(strcmp(temp->word,num)==0) {
            if(temp==head) {
                head=temp->next;
                delinner(temp->inner_list); /* FIX#2 */
                free(temp);
                return;
            } else {
                m->next=temp->next;
                delinner(temp->inner_list); /* FIX#2 */
                free(temp);
                return;
            }
        } else {
            m=temp;
            temp= temp->next;
        }
    }
    printf(" ELEMENT %s NOT FOUND ", num);
}
void delinner(inner_list *head) { /* FIX#2 */
    inner_list *temp;
    temp=head;
    while(temp!=NULL) {
        head=temp->next;
        free(temp);
        temp=head;
    }
}
Nir Levy
That works perfectly, unless the deleted node is the first node. Deleting the first node make the program crashes :S.
LuckySlevin
I cant see any reason why it should crash. Try to put a printf before the "return" and see if it gets that far. BTW, what does the "count" variable does? it is completely ignored in this section and might be important somewhere else..
Nir Levy
No, i will deal with count at the end. it was defaulted to zero. There is nothing connected to it. The problem is when I try to display the list after deletion it gives an error. Maybe i'm calling the function wrong :S.outer_list *p;//p is used for appending function etc.//thendelnode(p,word);//word to be searcheddisplay(p);//display the list start from p
LuckySlevin
can you update the question and add the code you mention?
Nir Levy
I will open another question about display function i guess. This question the title must change. I will post the link.
LuckySlevin
I updated the question.
LuckySlevin
http://stackoverflow.com/questions/2850517/display-problem-after-deletion-in-linked-list-in-c
LuckySlevin
+1  A: 

There's a big problem in that if you end up needing to delete the first element of the outer list, you never pass back the new head of the list. Your origuinal code needs changing as follows (also put in all the other good suggestions):

void delnode(outer_list **tbd,char num[100]) // pass a pointer to tbd
{
    outer_list *temp, *m;
    temp = *tbd;
    while(temp!=NULL)
    {
        if(strcmp(temp->word,num)==0)
        {
            if(temp==*tbd)
            {
                // Delete the inner list here
                *tbd=temp->next;
                free(temp);
                return;
            }
     // rest of function

You'd call it like this:

outer_list* myList;

// lots of code including initialising and adding stuff to the list

delnode(&mylist, wordtoDelete);  // note the '&' sign
JeremyP
great answer with the delnode(it solved the problem for deleting head. There stays one issue i will now update the question.
LuckySlevin
Question is updated.
LuckySlevin