1

提前感谢您的帮助。我正在尝试在 C 中创建一个简单的图表。但是我得到了

graph.c:66:11: warning: incompatible pointer types initializing
      'edge *' (aka 'struct gegde *') with an expression of type
      'struct gedge *' [-Wincompatible-pointer-types]
    edge *node = fromptr->nextedge;
          ^      ~~~~~~~~~~~~~~~~~
graph.c:69:14: warning: incompatible pointer types assigning to
      'edge *' (aka 'struct gegde *') from 'struct gedge *'
      [-Wincompatible-pointer-types]
        node = node->nextedge;
             ^ ~~~~~~~~~~~~~~
graph.c:75:44: error: incomplete definition of type 'struct gedge'
    printf("val is %c\n", fromptr->nextedge->link->element);
                          ~~~~~~~~~~~~~~~~~^
graph.c:7:12: note: forward declaration of 'struct gedge'
    struct gedge *nextedge;
           ^

这是我的代码,我不确定我哪里出错了。我不明白为什么会收到不兼容的警告和前向声明错误。

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h> 
#include "graph.h"

typedef struct gegde {
    struct gedge *nextedge;
    struct gvertex *link;
}edge;

typedef struct gvertex {
    ge element;
    struct gvertex *nextvertex;
    struct gedge *nextedge;
    int visited;
}vertex;

void graph_add(ge c, vertex **root, vertex **last)
{
    vertex *temp = *root;
    printf("root points to %p\n", *root);
    while (temp != NULL) {
        // vertex already exists in graph, ignore insert
        if (temp->element == c) {
            return;
        }
        temp = temp->nextvertex;
    }

    vertex *v = malloc(sizeof(*v));
    v->element = c;
    v->nextedge = NULL;
    // first vertex in graph
    if (*root == NULL) {
        *root = v;
        *last = *root;
    } else {
        (*last)->nextvertex = v;
        *last = v;
    }
}

void graph_edge(ge from, ge to, vertex **root, vertex **last)
{
    vertex *temp = *root;
    vertex *fromptr, *toptr;
    int count = 0;
    while (temp != NULL) {
        // ensure both the vertices are present before drawing an edge
        if (temp->element == from) {
            fromptr = temp;
            count++;
        }
        if (temp->element == to) {
            toptr = temp;
            count++;
        }

        temp = temp->nextvertex;
    }
    if (count < 2) {
        printf("either %c or %c vertices do not exist in graph\n", from, to);
        return;
    }

    edge *node = fromptr->nextedge;
    while (node != NULL) {
        printf("found edge %c%c\n", fromptr->element, node->link->element);
        node = node->nextedge;
    }
    edge *newedge = malloc(sizeof(*newedge));
    newedge->nextedge = NULL;
    newedge->link = toptr;
    node = newedge;
    printf("val is %c\n", fromptr->nextedge->link->element);
}

int main(int argc, char **argv)
{
    // keep track of head
    vertex *root = malloc(sizeof(*root));
    root->nextvertex = NULL;
    root = NULL;

    // keep track of last vertext
    vertex *last = malloc(sizeof(*last));
    last->nextvertex = NULL;

    last = root;

    graph_add('a', &root, &last);
    graph_add('b', &root, &last);
    graph_add('c', &root, &last);
    graph_add('d', &root, &last);

    graph_edge('a', 'b', &root, &last);
    graph_edge('a', 'c', &root, &last);
    graph_edge('b', 'c', &root, &last);
    graph_edge('a', 'd', &root, &last);

    return 0;
}
4

1 回答 1

3

那么首先可能有一个错字:

typedef struct gegde {
    struct gedge *nextedge;
    struct gvertex *link;
}edge;

Astruct gegde不是struct gedge.

这似乎是编译器所抱怨的。

于 2013-10-31T04:44:44.733 回答