c - Deleting nodes in linked list when strings match -


this question has answer here:

hello working on linked list - struct based project , asking guys little help. have delete node linked list when 1 string part of string struct data.

so far have come this. able use strstr() find match in 2 strings, after displays string #1 in string #2 whole program falls.

void zmaz(stuff *p_akt, stuff *p_prv, stuff *p_vymaz){ char meno_autor[100]; scanf("%s", meno_autor); while(p_akt!=null) {     if(strstr(p_akt->autori,meno_autor)!=null){        printf("string %s in %s\n", meno_autor, p_akt->autori);         p_vymaz=p_akt;         if (p_prv == p_vymaz){ //deleting first node             p_prv = p_prv->p_dalsi;         }          else {             p_akt = p_prv;             while (p_akt->p_dalsi != p_vymaz) p_akt = p_akt->p_dalsi;             p_akt->p_dalsi = p_vymaz->p_dalsi;         }         free((void*)p_vymaz);     } p_akt=p_akt->p_dalsi; } } 

i dont have delete node, move next nodes there no gap between of them. tried explain as could. thank you!

the reason fails because make following sequence of code:

while(){     if(){        ...        p_vymaz=p_akt;        ...        free((void*)p_vymaz);     }     p_akt=p_akt->p_dalsi; } 

you have freed p_akt assigning p_vymaz p_akt , freeing p_vymaz. such, p_akt->p_dalsi no longer valid.


Comments