【问题标题】:Issue with insertion in an adjacency list插入邻接列表的问题
【发布时间】:2019-03-29 18:12:32
【问题描述】:

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

我已经尝试在 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;
}

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

【问题讨论】:

  • 不要像 TypePointer 那样将指针性质隐藏在 typedef 后面。这样做导致的问题多于解决的问题。
  • 同样不要将malloc()的返回值转换为C。(但如果你用C++编译器编译,也不要假设你是用C编写的。)

标签: c graph adjacency-list insertion


【解决方案1】:

查看指针类型定义中的危险

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); (调整 3,权重 4); (调整 1,权重 8); v 1:(调整 8,权重 4); (调整 4,权重 2); (调整 2,权重 7); (调整 0,权重 8); v 2:(调整 8,权重 14); (调整 5,权重 9); (调整 1,权重 7); v 3:(调整 6,权重 8); (调整 0,权重 4); v 4:(调整 6,权重 7); (调整 1,权重 2); v 5:(调整 8,权重 10); (调整 2,权重 9); v 6:(调整 7,权重 1); (调整 4,权重 7); (调整 3,权重 8); (调整 0,权重 11); v 7:(调整 8,权重 2); (调整 6,权重 1); v 8:(调整 7,权重 2); (调整 5,权重 10); (调整 2,权重 14); (调整 1,权重 4);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2015-05-31
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    相关资源
    最近更新 更多