c - 插入邻接列表时出现问题

标签 c graph adjacency-list insertion

我试图在无向图中插入节点,在第一次插入时代码表现得很好,但在第二次插入时代码不再工作,我不明白为什么。有人可以帮我解决这个问题吗?

我已经尝试在 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: (adj 6, weight 11); (adj 3, weight 4); (adj 1, weight 8);
v 1: (adj 8, weight 4); (adj 4, weight 2); (adj 2, weight 7); (adj 0, weight 8);
v 2: (adj 8, weight 14); (adj 5, weight 9); (adj 1, weight 7);
v 3: (adj 6, weight 8); (adj 0, weight 4);
v 4: (adj 6, weight 7); (adj 1, weight 2);
v 5: (adj 8, weight 10); (adj 2, weight 9);
v 6: (adj 7, weight 1); (adj 4, weight 7); (adj 3, weight 8); (adj 0, weight 11);
v 7: (adj 8, weight 2); (adj 6, weight 1);
v 8: (adj 7, weight 2); (adj 5, weight 10); (adj 2, weight 14); (adj 1, weight 4);

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

关于c - 插入邻接列表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423351/

相关文章:

c - 英特尔 SSE : Why does `_mm_extract_ps` return `int` instead of `float` ?

c++ - 创建具有可移动节点的有向图(使用 QT/Boost)

python - 将邻接矩阵转换为字典的有效方法是什么?

c - 在 C 中的数组末尾附加一个 float

c - 我如何使用 strcastr()?

graph - 非平面图的平面化算法

c++ - 从图复制到 vector 后顶点数量增加

mysql - 选择树节点的所有后代

c - 为 char** 分配内存

c++ - 最短/最便宜的路径?这里如何使用动态规划?