2
I need to create a program to read a graph and represent in an adjacency structure, however, my compiler is accusing Segmentation fault and I can’t figure out what might be causing it. It might help me?
#include <stdio.h>
#include <stdlib.h>
FILE *ent;
FILE *saida;
int V, E;
int **matriz;
struct adj{
int w, v;
struct adj *prox;
};
typedef
struct adj grafo;
grafo **G;
void Estrutura (int V);
void M_Adj (int **matriz, int V);
void ZerarMatriz (int **matriz);
void printMatriz (int **matriz, int V);
void printEstrutura (grafo **G, int V);
int main()//(int argc, char *argv[])
{
//declarações e abertura de arquivos
ent = fopen("ent.txt", "r");
saida = fopen("sai.txt", "w");
//ler arquivo de entrada e declarações
int i,v, w, Vi;
fscanf(ent, "%d %d", &V, &E);
//alocar matriz
matriz = malloc(V * sizeof(int*));
for (i = 0; i < V; i++)
matriz[i] = (int*)malloc(V * sizeof(int));
//alocar vetor cabeça do grafo
G = (grafo **) malloc(sizeof(V*sizeof(grafo)));
//int matriz[V][V];
fprintf (saida, "Arquivo de entrada \n \n");
fprintf(saida, "%d %d \n", V, E);
for(i = 0; i < V; i++){
fscanf(ent, "%d", &v);
fprintf(saida, "%d ", v);
fscanf(ent, "%d", &w);
if (w != 0) fscanf(ent, "%d", &Vi);
while (w != 0){
fprintf(saida,"%d %d ", w, Vi);
fscanf(ent, "%d", &w);
if (w != 0) fscanf(ent, "%d", &Vi);
}
fprintf(saida, "\n");
}
ZerarMatriz(matriz);
M_Adj(matriz, V);
Estrutura(V);
fclose(ent);
fclose(saida);
free(matriz);
return 0;
}
void Estrutura (int V){
ent = fopen("ent.txt", "r");
int i, Vi, w, v, VV, EE;
fscanf(ent, "%d", &VV);
fscanf(ent, "%d", &EE);
printf("Tam: %d %d \n", VV, EE);
for (i = 0; i < V; i++)
(G[i]->prox) = NULL; -- O ERRO É ACUSADO AQUI
struct adj *aux = malloc(sizeof(struct adj));
for(i = 0; i < V; i++){
fscanf(ent, "%d %d", &Vi, &w);
printf("\n %d %d \n", Vi,w);
while (w != 0){
fscanf(ent, "%d", &v);
aux->prox = NULL;
aux->v = v;
aux->w = w;
if (&(G[i]->prox) == NULL){ -- SE EU NÃO COLOCAR O & COMERCIAL TAMBÉM É ACUSADO AQUI
G[i]->prox = aux;}
else
{
grafo *aux2 = G[i];
while(&(aux2->prox) != NULL) -- SE EU NÃO COLOCAR O & COMERCIAL TAMBÉM É ACUSADO AQUI
aux2 = (grafo *) (aux2->prox); -- ISSO TAMBÉM NÃO PARECE FUNCIONAR
(aux2->prox) = aux;
aux2 = NULL;
}
fscanf(ent, "%d", &w);
}
}
aux = NULL;
fprintf(saida," \n Estrutura de Adjacência: \n");
printEstrutura(G, V);
}
I know it’s a little redundant, but it’s for teaching purposes. Thank you.
------------------------------------ Updating ----------------------------
Victor, thank you very much for the answer but even with these changes there are still some problems, so I decided to add the new vertices at the beginning of the list, with the changes the code was like this:
void Estrutura (int V){
ent = fopen("ent.txt", "r");
int i, Vi, w, v, VV, EE;
fscanf(ent, "%d", &VV);
fscanf(ent, "%d", &EE);
printf("Tam: %d %d \n", VV, EE);
struct adj *aux = malloc(sizeof(struct adj));
for(i = 0; i < V; i++){
fscanf(ent, "%d %d", &Vi, &w);
printf("\n %d %d ", Vi,w);
while (w != 0){
fscanf(ent, "%d", &v);
printf(" %d %d ", w, v);
aux->prox = NULL;
aux->v = v;
aux->w = w;
if (G[i] == NULL){
G[i] = aux;
}
else{
aux->prox = G[i];
G[i] = aux;
}
fscanf(ent, "%d", &w);
}
}
aux = NULL;
printEstrutura(G, V);
}
void printEstrutura (grafo **G, int V){
int i;
struct adj *aux;
fprintf(saida, "/n PRINTANDO ESTRUTURA :DDDDD");
for(i = 0; i < V; i++){
aux = G[i];
while (aux != NULL){
fprintf(saida, "%d %d", aux->w, aux->v);
aux = aux->prox;
}
}
}
But with the changes, my code is looped in the print function. I’ve been working hard for a long time and it’s getting in the way of my income, I’m going to get some sleep to avoid slips like the last one. Thank you.
I updated my answer.
– Victor Stafusa