我正在尝试在无向图中插入节点,在第一次插入时,代码效果很好,但是在第二次,代码不再起作用,我也不明白为什么。有人可以帮我解决这个问题吗?

我已经尝试在Dev-c ++中进行编译,有时可以运行代码,而其他人则不能,但在CodeBlocks和CMD(Windows)中则无法工作。

这是我的代码:

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

typedef struct tedge {
    int vdest; //destiny vertex
    double weight;
    struct tedge* next;
}TypeEdge;

typedef TypeEdge* TypePointer;

typedef struct {
    TypePointer* listAdj;
    int numVertex;
    int numEdges;
}TypeGraph;

//initialize the graph
bool initializeGraph(int nv, TypeGraph* graph) {
    if(nv <= 0) return false;

    int i;

    if(graph->listAdj = (TypePointer*)malloc(nv*sizeof(TypePointer))) {

        graph->numEdges = 0;
        graph->numVertex = nv;

        for(i = 0; i < nv; i++)
            graph->listAdj[i] = NULL;

        return true;
    }

    return false;
}


//insertion
bool insertEdge(int v1, int v2, double weight, TypeGraph *graph) {
    if(!graph) return false;
    if((v1 < 0) || (v1 >= graph->numVertex)) return false;
    if((v2 < 0) || (v2 >= graph->numVertex)) return false;

    TypePointer new = (TypePointer)malloc(sizeof(TypePointer));
    new->vdest = v2;
    new->weight = weight;
    new->next = graph->listAdj[v1];
    graph->listAdj[v1] = new;

    TypePointer simetry = (TypePointer)malloc(sizeof(TypePointer));
    simetry->vdest = v1;
    simetry->weight = weight;
    simetry->next = graph->listAdj[v2];
    graph->listAdj[v2] = simetry;

    graph->numEdges++;

    return true;
}

void printGraph(TypeGraph* graph) {
    int i;

    for(i = 0; i < graph->numVertex; i++) {
        TypePointer actual = graph->listAdj[i];
        printf("v %i: ", i);

        while(actual != NULL) {
            printf("(adj %i, weight %g); ", actual->vdest, actual->weight);
            actual = actual->next;
        }

        printf("\n");
    }
}

int main() {
    TypeGraph graph;


    initializeGraph(9, &graph);

    insertEdge(0, 1, 8, &graph);
    insertEdge(0, 3, 4, &graph);
    insertEdge(0, 6, 11, &graph);
    insertEdge(1, 2, 7, &graph);
    insertEdge(1, 4, 2, &graph);
    insertEdge(1, 8, 4, &graph);
    insertEdge(2, 5, 9, &graph);
    insertEdge(2, 8, 14, &graph);
    insertEdge(3, 6, 8, &graph);
    insertEdge(4, 6, 7, &graph);
    insertEdge(5, 8, 10, &graph);
    insertEdge(6, 7, 1, &graph);
    insertEdge(7, 8, 2, &graph);

    printGraph(&graph);

    return 0;
}


如果有人可以帮助我,我将接受任何建议。谢谢。

最佳答案

请参阅带有指针的typedef中的危险

TypePointer new = (TypePointer)malloc(sizeof(TypePointer));
new->vdest = v2;
new->weight = weight;
new->next = graph->listAdj[v1];


这只会为指针分配足够的内存。我会建议

TypePointer new = malloc(sizeof(*new));


也在这里

TypePointer simetry = (TypePointer)malloc(sizeof(TypePointer));


应该是

TypePointer simetry = malloc(sizeof(*simetry));


经过更正后,该程序报告:

v 0 :(调整6,权重11); (adj 3,权重4); (adj 1,权重8);
v 1:(adj 8,权重4); (adj 4,权重2); (adj 2,权重7); (adj 0,权重8);
v 2:(adj 8,重量14); (adj 5,权重9); (adj 1,权重7);
v 3:(adj 6,重量8); (adj 0,权重4);
v 4 :(调整6,权重7); (adj 1,权重2);
v 5 :(调整8,权重10); (adj 2,权重9);
v 6 :(调整7,权重1); (adj 4,权重7); (adj 3,权重8); (adj 0,权重11);
v 7 :(调整8,权重2); (adj 6,权重1);
v 8 :(调整7,权重2); (adj 5,权重10); (adj 2,权重14); (adj 1,权重4);


我也注意到这些函数返回一个状态,该状态将被忽略,尽管这并没有导致此处的崩溃。

10-08 03:56