Home > Mobile >  can't delete the head node of my linked list
can't delete the head node of my linked list

Time:01-01

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);

CodePudding user response:

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);
  • Related