【发布时间】:2015-12-26 15:28:49
【问题描述】:
我正在尝试构建一个可以使用邻接列表或矩阵处理图形的程序,为此老师教我们将邻接声明为 void *,以便将其转换为列表或矩阵。
如您所见,B 节点中有一些奇怪的东西。
如果我尝试使用 CodeBlocks 进行调试,调试器会在 appendNodeList if (L->target != target) {.. 处提供 Segmentation Fault
我认为initGraphList中的动态分配存在问题,但我不知道如何解决。
您认为这里的问题是什么?是分配吗?如果是,我该如何解决? 提前感谢您的帮助!
代码:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "Graph.h"
int main(int argc, const char * argv[]) {
Graph G = NULL;
G = initGraphList(3);
addEdgeList(G, 0, 1, 1);
addEdgeList(G, 0, 2, 2);
addEdgeList(G, 1, 0, 3);
addEdgeList(G, 1, 2, 4);
addEdgeList(G, 2, 0, 5);
addEdgeList(G, 2, 1, 6);
printGraphList(G);
return 0;
}
图形.h
#include "List.h"
struct TGraph {
void **adj;
int nodes_count;
};
typedef struct TGraph *Graph;
typedef struct AdjList{
List *nodes;
}AdjList;
Graph initGraphList(int);
void printGraphList(Graph G);
void addEdgeList(Graph G, int source, int target, float peso);
图形.c
#include <stdio.h>
#include <stdlib.h>
#include "Graph.h"
Graph initGraphList(int nodes_count){
Graph G = (Graph)malloc(sizeof(struct TGraph));
G->adj = malloc(sizeof(AdjList));
G->nodes_count = nodes_count;
((AdjList *)(G->adj))->nodes = malloc(nodes_count * sizeof(List));
return G;
}
void printGraphList(Graph G) {
if (G != NULL) {
int i;
for(i = 0; i < G->nodes_count; i++) {
printf("%c -> ", i + 'A'); //I use this in order to print out the nodes as A,B,C,.. instead of 0,1,2,...
printList(((AdjList *)(G->adj))->nodes[i]);
puts("\n");
}
}
}
void addEdgeList(Graph G, int source, int target, float peso){
if(G != NULL){
if(source != target){
if(source < G->nodes_count){
if(target < G->nodes_count)
((AdjList*)(G->adj))->nodes[source]= appendNodeList(((AdjList*)(G->adj))->nodes[source], target, peso);
else
fprintf(stderr, "Il nodo %d non e' compreso nel grafo\n", target);
}else
fprintf(stderr, "Il nodo %d non e' compreso nel grafo\n", source);
}else
fprintf(stderr, "Non e' possibile inserire un arco che punta allo stesso nodo\n");
}else
fprintf(stderr, "Grafo invalido\n");
}
列表.h
struct TList {
char target;
float peso;
struct TList* next;
};
List initNodeList(int info, float peso);
List appendNodeList(List L, int target, float peso);
void printList(List L);
列表.c
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
List initNodeList(int info, float peso) {
List L = malloc(sizeof(struct TList));
L->target = info;
L->peso = peso;
L->next = NULL;
return L;
}
List appendNodeList(List L, int target, float peso) {
if (L != NULL) {
if (L->target != target) {
L->next = appendNodeList(L->next, target, peso);
}
} else {
L = initNodeList(target, peso);
}
return L;
}
void printList(List L) {
if (L != NULL) {
printf(" %c(%f), ", L->target + 'A', L->peso);
printList(L->next);
}
}
【问题讨论】:
-
你介意创建一个MCVE吗?
-
旁白:在声明指针类型时,如果类型名称和该类型的后续变量使用样式匈牙利符号来提示读者,它会使代码更具可读性,例如
typedef struct TGraph *pGraph;跨度> -
@SouravGhosh 现在应该是 MCVE,我删除了免费函数并添加了要包含的标题并将 Graph.h 与 AdjList.h 合并。 @WeatherVane 我想我是用
typedef struct TGraph *Graph;做到的,或者我不明白你在说什么。 -
贴出的代码无法编译。要么没有发布完整的代码,要么从未编译过代码,因为没有
List的定义,我怀疑文件list.h缺少typedef的List。 -
头文件都缺少“包含保护”“包含保护”可以是
pragma once或(IMO:首选)#ifndef UNIQUE_NAME / #define UNIQUE_NAME .....#endif // UNIQUE_NAME
标签: c debugging graph codeblocks void-pointers