Graph represented as list linked to list

Asked

Viewed 56 times

1

I am trying to implement a graph with list structure linked to list by pulling the data from a txt file (input.txt).

A 100 2 B 3 D 4
B 150 2 C 3 E 2
C 150 1 F 2
D 150 1 E 4
E 100 1 F 1
F 200 0

The idea is that when reading the file a first time, capture the letters of the first column and with this create the nodes of the first list. Close the file, open it again and with this start to build the second list by making the connections. But only the first character of the first column is read, the others are not and with this the first list is not created, leading the structure to point to memory junk. I’m using the Xcode. Thanks for your help and attention

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

typedef struct elementoarco {
int valorarco;
int usado;
struct elementoarco *proxarco;
struct elementono *padj;
} arco;

typedef struct elementono {
char dado;
int valorno;
int usado;
struct elementono *proxno;
struct elementoarco *pacessoarco;
} no;

void *Procurar (no *ginicio, char chave) {
no *n1;
n1 = ginicio;

while ((n1 != NULL) && (n1->dado != chave))
    n1 = n1->proxno;

return n1;
}

void Construir (no **ginicio) {
FILE *arq;
no *no1, *no2, *no3, *no4;
arco *arco1, *arco2;
char c;
int n, i, v;

arq = fopen ("entrada.txt", "r");
*ginicio = NULL;

int primeira = 1;

while ((c = getc (arq)) != EOF) {
    if (c == '\n')
        primeira = 1;
    else
        primeira = 0;

    if (primeira == 1){
            no1 = malloc (sizeof (no));
            no1->dado = c;
            no1->valorno = 0;
            no1->usado = 0;
            no1->pacessoarco = NULL;
            no1->proxno = NULL;

            if (*ginicio == NULL)
                *ginicio = no1;

            else
                no2->proxno = no1;

            no2 = no1;
    }
}
/*
fclose(arq);
arq = fopen ("entrada.txt", "r");
*/
while ((c = getc (arq)) != EOF) {
    if (c != '\n') {
        no3 = Procurar (*ginicio, c);

        printf("%c", no3->dado);

        fscanf (arq, "%d ", &n);

        no3->valorno = n;

        fscanf (arq, "%d ", &n);

        for (i = 1; i != n + 1; i++) {
            c = getc (arq);

            arco1 = malloc (sizeof (arco));

            fscanf (arq, "%d ", &v);

            arco1->valorarco = v;
            arco1->usado = 0;

            no4 = Procurar(*ginicio, c);
            arco1->padj = no4;

            arco1->proxarco = NULL;

            if (no3->pacessoarco == NULL)
                no3->pacessoarco = arco1;
            else
                arco2->proxarco = arco1;

            arco2 = arco1;
        }
    }
}

fclose (arq);
return;
}


int main() {
no *ginicio;

Construir (&ginicio);

no *teste;
arco *arcoteste;

teste = ginicio;

while (teste != NULL) {
    printf("%c", teste->dado);
    printf("\t%i", teste->valorno);
    printf("\t%i", teste->usado);

    arcoteste = teste->pacessoarco;

    printf("\n");

    while (arcoteste != NULL) {
        printf(" Arco: %c", arcoteste->padj->dado);
        printf(" Valor do Arco: %d", arcoteste->valorarco);
        printf("\n");
        arcoteste = arcoteste->proxarco;
    }

    printf("\n");
    teste = teste->proxno;
    }

    return 0;
}

The part of the second fclose is commented on because it does not compile.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.