typedef struct _set{
int root;
int rank;
}Set;
void Kruskal(Graph* g)
{
Set uni[g->nv];
Edge result[g->nv - 1];
int i;
int count = 0;
int num = 0;
int aRoot, bRoot;
for(i = 0; i < g->nv; i++){
uni[i].root = i;
uni[i].rank = 0;
}
QuickSort(g, 0, g->ne-1);
while(count != (g->nv-1) && num != g->ne){
WeightedUnion(uni, g->path[num].src, g->path[num].dest);
aRoot = Find(uni, g->path[num].src);
bRoot = Find(uni, g->path[num].dest);
if( aRoot != bRoot){
result[num] = g->path[num];
count++;
}
num++;
}
if(count != g->nv-1){
printf("No spanning tree\n");
}
else{
for(i = 0; i <= count; i++){
printf("[%d] %d - %d : %d\n",i+1,result[i].src,result[i].dest,result[i].weight);
}
}
}
这是我的代码部分。问题是我无法初始化'uni [g-> nv]'。您可以在变量区域旁边看到“ for”循环。而且我确信信誉必须初始化此数组,但结果不是。该数组不包含任何其他值。只是空的。我找不到我的问题。请告诉我我的问题或错误。
您正在使用可变长度数组(VLA),该数组的长度在运行时动态依赖于表达式。由于大小在编译时未知,因此您无法使用初始化程序表达式对其进行初始化,但必须在执行操作时使用for
循环来进行初始化。
VLA通常是在您的程序在定义它的函数的所谓堆栈上执行时实现的。该堆栈有大小限制,您必须注意不要超过它。(如果这样做,则没有直接了解的工具。)
因此,请勿像对待大小未知的大数据那样使用VLA。而是使用指针并malloc
分配所需的内存。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句