Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can't delete the head node of my linked list

I’m trying to create a sort of a simple compiler for a mini SQL language, and when I execute the algorithm there is a step when I need to call a function to delete the head node of a linked list(containing lexical units of the language)and assign the head to the second node. Inside the function, the code seems working just fine (when I print the list, the head is now the second node). but the change affection the original list is weird as the first head is still there but with a different value like 12321104(=p).so where is the problem
this is the structure of my linked list:

typedef struct U_Lexicale {
    char* Nom_UL; 
    int Type_UL; 
    int Ligne_UL;
    struct U_Lexicale* suivant;
} ElementUL;

void suppression(ElementUL* liste) {
    if (liste == NULL) {
        exit(EXIT_FAILURE);
    }

    if (liste != NULL) {
        ElementUL* tmp = liste;
        liste = liste->suivant;
        free(tmp);

        afficherListeUL(liste);
    }
}

suppression(copieAnalyseLex);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You modify liste, but you don’t modify copieAnalyseLex to reflect that change.

#include <assert.h>

void suppression(ElementUL** liste_p)
{
    assert( *liste_p != NULL );

    ElementUL* tmp = *liste_p;
    *liste_p = (*liste_p)->suivant;
    free(tmp);
}

suppression(&copieAnalyseLex);
afficherListeUL(copieAnalyseLex);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading