Problems with code from a priority list in C

0

I'm creating a priority list in C (topological ordering) using vectors and within those vectors a linked list pointing out who has to come before whom. But it's giving the pointer error and I really can not figure it out.

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#define DIM 50

typedef struct list{
    struct list * prox;
    int contador;
}LISTA;

typedef struct ant{
    struct ant* prox;
    int chave;
}PRE;

void iniciar(int n, int m, LISTA lista[DIM], PRE *ptpre);

int main(void){
    LISTA lista[DIM],pt;
    PRE ptpre;

    int m,n,fim,objeto,indice,i;
    scanf("%d %d",&n,&m);
    iniciar(n,m,lista,&ptpre);
    fim = 0;
    lista[0].contador=0;
    for (i=1;i<=n;i++){
        if(lista[i].contador==0){
            lista[fim].contador = i;
            fim = i;
        } 
    }
    objeto=lista[0].contador;
    while(objeto != 0){
        printf("%d",objeto);
        pt = lista[objeto].prox;
        while (*pt != 0){
            indice = pt->chave;
            lista[indice].contador = lista[indice].contador -1;
            if (lista[indice].contador == 0){
                lista[fim].contador = indice;
                fim = indice;
            }
            pt = pt->prox;
        }
      objeto=lista[objeto].contador; 
    }
return 0;    
}

void iniciar(int n, int m, LISTA lista[DIM], PRE *ptpre){
    int i;
    for (i=0;i<=n;i++){
        lista[i].contador = 0;
        lista[i].prox = 0;
    }
    for (i=1;1<=m;i++){
        scanf("%d %d",&a,&b);
        ptpre = (PRE *) malloc(sizeof(PRE));
        ptpre -> chave = b;
        ptpre -> prox = lista[a].prox;
        lista[a].prox = ptpre;
        lista[b].contador = lista[b].contador + 1;
    }


}
    
asked by anonymous 14.06.2017 / 00:39

1 answer

0

In

LISTA lista[DIM],pt;

You are creating pt as a LISTA variable, not as a pointer for a LISTA variable. So you can not pass lista[objeto].prox to pt, because prox is a pointer and pt is any variable. To fix this problem, change the pt declaration to

LISTA lista[DIM], *pt;

I also noticed other errors in your code, like trying to access a variable chave within struct pt . This is impossible because pt is LISTA , and the only struct in your code that has a member named chave is the struct PRE .

    
14.06.2017 / 02:16