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 に答える