Mhennaoui Mohamed
Mhennaoui Mohamed

Reputation: 25

linked-list is always getting NULL even after adding elements to it

I tied to list the linked-list, but nothing happen. It looks like my list is NULL even if I add elements to it. The function insertTete adds element to the begin of the list. affichListe lists the content of the list.
What I did is created a list called maillon then gave it an alias liste using typdef maillon liste. Then I created a variable named liste* l in int main and passed that variable to function as parameter. When I use printf inside insertTete it shows me the values, so insertTete working but when I try to call affichListe function it does not show anything.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct eleve {
    char nom[20];
    char prenom[20];
    int val;
};

struct maillon {
    eleve ele;
    maillon *suivant;
};

typedef maillon liste;

void insertTete(liste *premier, eleve ele) {
     liste *nouvEleve;
     nouvEleve = (liste *)malloc(sizeof(liste));
     nouvEleve->ele = ele;
     nouvEleve->suivant = premier;
     premier = nouvEleve; 
}

liste *dernier(liste *premier) {
    while (premier != NULL)
        premier = premier->suivant;
    return premier;
}

void affichListe(liste *premier) {
    while (premier != NULL) {
        printf("Nom etudiant: %s", premier->ele.nom);
        printf("Prenom etudiant: %s", premier->ele.prenom);
        printf("Note etudiant: %i", premier->ele.val);
    }
}

int main() {
    eleve ele;
    liste *l = NULL;
    
    strcpy(ele.nom, "Ahmed");
    strcpy(ele.prenom, "bejaoui");
    ele.val = 15;
    insertTete(l, ele);
    printf("%i", l == NULL);
    affichListe(l);
}

Upvotes: 0

Views: 72

Answers (2)

chqrlie
chqrlie

Reputation: 144695

There are multiple problems in your code:

  • insertTete() should take a pointer to the head pointer so the caller's variable is updated.
  • dernier() should loop while (premier->suivant). As currently coded, it always returns NULL.
  • in affichListe() you should update premier to point to the next node inside the loop body.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct eleve {
    char nom[20];
    char prenom[20];
    int val;
};

struct maillon {
    eleve ele;
    maillon *suivant;
};

typedef maillon liste;

liste *insertTete(liste **premier, eleve ele) {
    liste *nouvEleve = malloc(sizeof(liste));
    if (nouvEleve) {
        nouvEleve->ele = ele;
        nouvEleve->suivant = *premier;
        *premier = nouvEleve; 
    }
    return nouvEleve;
}

liste *dernier(liste *premier) {
    if (premier != NULL) {
        while (premier->suivant != NULL)
            premier = premier->suivant;
    }
    return premier;
}

void affichListe(const liste *premier) {
    while (premier != NULL) {
        printf("Nom étudiant: %s\n", premier->ele.nom);
        printf("Prénom étudiant: %s\n", premier->ele.prenom);
        printf("Note étudiant: %i\n\n", premier->ele.val);
    }
}

int main() {
    eleve ele;
    liste *l = NULL;
    
    strcpy(ele.nom, "Ahmed");
    strcpy(ele.prenom, "Bejaoui");
    ele.val = 15;
    insertTete(&l, ele);
    affichListe(l);
    return 0;
}

Upvotes: 0

Equod
Equod

Reputation: 556

The issue is l's value is never modified. You should pass it as a pointer to a pointer

void insertTete(liste** premier, eleve ele) {
  liste* nouvEleve;
  nouvEleve = (liste*) malloc(sizeof(liste));
  nouvEleve->ele = ele;
  nouvEleve->suivant = *premier;
  *premier = nouvEleve;
}

or assign it from the return value

liste* insertTete(liste* premier, eleve ele) {
  liste* nouvEleve;
  nouvEleve = (liste*) malloc(sizeof(liste));
  nouvEleve->ele = ele;
  nouvEleve->suivant = premier;
  premier = nouvEleve;
  return premier;
}

l = insertTete(l, ele);

Upvotes: 2

Related Questions