0

I must create a Dijkstra algorithm program using an adjacency list. My teacher give me this struct.

But I have this error :

note: expected 'struct maillon **' but argument is of type 'LISTE'     
      void insere(int som_a, int som_b, int poids, LISTE Adj[]){

Where the argument nom is vertices, poids is weight, som_a and som_b are vertices.

function void insere : insert (som_b, poids) At the top of the adjacency list Adj[som_a]

    typedef struct maillon{
        struct maillon *suiv;
        int nom;
        int poids;
     } MAILLON, *LISTE;


    void insere(int som_a, int som_b, int poids, LISTE Adj[]) {
        LISTE prem = malloc(sizeof(LISTE));
        prem->nom = som_b;
        prem->poids = poids;
        prem->suiv = Adj[som_a];
        Adj[som_a] = prem;
    }

void dijsktra(int s, GRAPHE G) {
    int i, j, dist[NB_SOM_MAX], INT_MAX = 0, pred[NB_SOM_MAX], min, nb = 0, nbmin = 0;
    LISTE S,F = G.Adj;
    for(i = 0; i < G.nbSommets; i++) {
        dist[i] = INT_MAX;
        pred[i] = NULL;
    }
    dist[0] = 0;
    S = NULL;
    while(F != NULL){
        min = G.Adj[0]->poids;
        for(i = 1; i < G.nbSommets; i++) {
            if(min > G.Adj[i]->poids) {
                min = G.Adj[i]->poids;
                nbmin = i;
            }
        }
        insere(nb, nbmin, min, S);
        nb++;
        if(nbmin == 0){
            F = F->suiv;
        }
        else{ // F[nbmin-1]->suiv = F[nbmin + 1];
            F[nbmin - 1] = F[nbmin + 1];
        }

        for(i = G.Adj[nbmin]->nom; i < G.nbSommets; i++){
            if(dist[i] > dist[nbmin] + G.Adj[nbmin]->poids){
                dist[i] = dist[nbmin] + G.Adj[nbmin]->poids;
                pred[i] = nbmin;
            }
        }
    }

    for(i = 0; i < G.nbSommets; i++){
        printf("Chemin optimal de %d à %d : ", i, s);
        printf("%d-", i);
        j = i;
        while(pred[j] != s || pred[j] != NULL){
            printf("%d-", pred[j]);
            j = pred[j];
        }
        printf("\n");
    }
}
Toby
  • 9,696
  • 16
  • 68
  • 132
  • You should use `insere(nb, nbmin, min, &S)` – Garf365 Nov 29 '16 at 13:31
  • 4
    `LISTE prem=malloc(sizeof(LISTE));` The joys of typedef: you are allocating space for a pointer (instead of for the object that it points to) – joop Nov 29 '16 at 13:34
  • Typedefs like this: `typedef struct maillon{ struct maillon *suiv; int nom; int poids; } MAILLON, *LISTE;` are insane. Don't use this. – Jabberwocky Nov 29 '16 at 13:37
  • Unfortunately I have to use this struct. Thanks I added &S and it works. –  Nov 29 '16 at 13:46
  • Why do you have `INT_MAX = 0`?? – Toby Nov 29 '16 at 13:48
  • I wanted to initialize it. It's wrong ? Sorry I start in C –  Nov 29 '16 at 13:53
  • @Roxanne : you can omit the typedefs, and just use the struct. As in `struct maillon *prem = malloc(sizeof *prem);` , which is not only shorter and easyer to read, but also correct. – joop Nov 29 '16 at 13:53

0 Answers0