如何在C中正确打印图表

问题描述 投票:0回答:1

所以我有这样的文件格式

aAa     5     bBb
bBb     3     cCc
cCc     7     dDd
dDd     2     eEe
eEe     8     fFf
fFf     4     gGg
gGg     6     hHh
hHh     1     iIi
iIi     9     jJj
jJj     5     kKk
kKk     3     lLl
lLl     7     mMm
mMm     4     nNn
nNn     6     oOo
oOo     2     pPp
pPp     8     qQq
qQq     1     rRr
rRr     9     sSs
sSs     5     tTt

其中,

column one
代表
vertex1
column 2
代表
weight
column 3
代表
vertex2
与顶点1相连的顶点。

我必须做更多的事情,但现在我只需要打印这个,我们必须使用链接列表来完成它。

这是我对练习的看法

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

typedef struct edge_s edge_t;
typedef struct vertex_s vertex_t;

struct edge_s {
    struct edge_s *next;
    int weight;
    vertex_t *vertex;
};

struct vertex_s {
    struct vertex_s *next;
    char *name;
    edge_t *edge;
    int traversed;
};

typedef struct graph_s {
    vertex_t *head;
    int n;
} graph_t;

vertex_t *find_vertex(graph_t *graph, const char name[20]);
void vertex_add(graph_t *graph, const char *name);
void edge_add(graph_t *graph, vertex_t *vertex1, vertex_t *vertex2, int weight);
graph_t *graph_init(void);
graph_t *fileread(const char *name);
void graph_write(graph_t *graph);

graph_t *graph_init() {
    graph_t *graph = malloc(sizeof(graph_t));
    if (graph == NULL)
        exit(EXIT_FAILURE);
    graph->head = NULL;
    graph->n = 0;

    return graph;
}

void edge_add(graph_t *graph, vertex_t *vertex1, vertex_t *vertex2, int weight) {
    edge_t *edge = malloc(sizeof(edge_t));
    if (edge == NULL)
        exit(EXIT_FAILURE);
    edge->weight = weight;
    edge->vertex = vertex2;
    edge->next = vertex1->edge;
    vertex1->edge = edge;
}

void vertex_add(graph_t *graph, const char *name) {
    vertex_t *vertex = malloc(sizeof(vertex_t));
    if (vertex == NULL)
        exit(EXIT_FAILURE);
    vertex->edge = NULL;
    vertex->traversed = 0;
    vertex->name = strdup(name);
    vertex->next = graph->head;
    graph->head = vertex;
}

vertex_t *find_vertex(graph_t *graph, const char name[20]) {
    vertex_t *v = graph->head;
    while (v != NULL) {
        if (strcmp(v->name, name) == 0)
            return v;
        v = v->next;
    }
    return NULL;
}

graph_t *fileread(const char *name) {
    FILE *fp = fopen(name, "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);

    graph_t *graph = graph_init();
    int weight;
    char name1[20], name2[20];
    vertex_t *vertex1, *vertex2;

    while (fscanf(fp, "%s %d %s", name1, &weight, name2) != EOF) {
        vertex1 = find_vertex(graph, name1);
        if (vertex1 == NULL) {
            vertex_add(graph, name1);
            vertex1 = graph->head; 
        }

        vertex2 = find_vertex(graph, name2);
        if (vertex2 == NULL) {
            vertex_add(graph, name2);
            vertex2 = graph->head; 
        }

        edge_add(graph, vertex1, vertex2, weight);
    }


    fclose(fp);
    return graph;
}

void graph_write(graph_t *graph) {
    vertex_t *vertex = graph->head;
    edge_t *edge;

    while (vertex != NULL) {
        printf("%s->", vertex->name);
        edge = vertex->edge;
        while (edge != NULL) {
            printf(" %d -> %s", edge->weight, edge->vertex->name);
            edge = edge->next;
        }
        printf("\n");
        vertex = vertex->next;
    }
}

int main() {
    graph_t *graph = fileread("file");
    graph_write(graph);
    return 0;
}

这些函数你可以通过名称猜出它们的含义,所以我怀疑我需要解释太多,但如果你质疑结构中未使用的变量,因为它是练习的第二部分,我将在打印此后解决。

事情是当我打印练习时它显示了这个

tTt->
sSs-> 5 -> tTt
rRr-> 9 -> sSs
qQq-> 1 -> rRr
pPp-> 8 -> qQq
oOo-> 2 -> pPp
nNn-> 6 -> oOo
mMm-> 4 -> nNn
lLl-> 7 -> mMm
kKk-> 3 -> lLl
jJj-> 5 -> kKk
iIi-> 9 -> jJj
hHh-> 1 -> iIi
gGg-> 6 -> hHh
fFf-> 4 -> gGg
eEe-> 8 -> fFf
dDd-> 2 -> eEe
cCc-> 7 -> dDd
bBb-> 3 -> cCc
aAa-> 5 -> bBb

除了第一行之外,大部分是正确的。第一行不应该在那里。知道如何解决它吗?

c graph linked-list
1个回答
1
投票

graph_write
中,检查
edge
是否是
NULL
打印顶点名称和
->
之前。如果顶点没有通向任何地方,则无需打印任何内容。

void graph_write(graph_t *graph) {
    vertex_t *vertex = graph->head;
    edge_t *edge;

    while (vertex != NULL) {
        edge = vertex->edge;

        if (edge == NULL) continue;

        printf("%s->", vertex->name);

        while (edge != NULL) {
            printf(" %d -> %s", edge->weight, edge->vertex->name);
            edge = edge->next;
        }
        printf("\n");
        vertex = vertex->next;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.