首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有结构的随机图

具有结构的随机图
EN

Stack Overflow用户
提问于 2017-04-11 09:25:02
回答 1查看 62关注 0票数 0

我试图为一个随机图实现代码,其中所有的顶点都是相互连接的。边缘应随机选择。我写了这段代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define VOL 100

struct node{
    int info;
    struct node *next;
};

struct node *read_list(void){
    struct node *p, *first=NULL;
    int i,V;

    for(i=0;i<V;i++){
        p=malloc(sizeof(struct node));
        p->next=first;
        p->info=rand()%V;
        first=p;
    }
    return(first);
}

void print_list(struct node *p){
    while(p!=NULL){
        printf("%d-> ", p->info);
        p=p->next;
    }
    printf("NULL\n");
    return;
}

int read_graph(struct node *G[]){
    int i, V;
    printf("Select a number of vertices:\n");
    scanf("%d", &V);
    for(i=0;i<V;i++){
        printf("Adjacency list of vertex %d:\n", i);
        G[i]=read_list();
    }
    return(V);
}

void print_graph(struct node *G[], int V){
    int i;
    printf("Adjacency lists of the graph:\n");
    for(i=0;i<V;i++){
        printf("Adjacency vertices to %d: ",i);
        print_list(G[i]);
    }
    return;
}

int adj(int i, int j, struct node *G[]){
    int r;
    struct node *p;
    p=G[i];
    while (p!=NULL && p->info !=j)
        p=p->next;
    if(p==NULL)
        r=1;

    else
        r=0;
    return (r);
    }


int main(){

    srand(time(NULL));
    struct node *G[VOL], *L;
    int V;
    V=read_graph(G);
    print_graph(G, V);
    L=read_list();
    return 0;
}

但是,它不起作用,我也不知道为什么。Xcode告诉我“构建成功”,但是代码没有打印任何东西(目前只打印‘选择一些顶点’):没有邻接列表,没有边.请检查它并告诉我错误在哪里?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-11 09:32:56

在下面的for语句中,变量V未初始化。

代码语言:javascript
复制
...
struct node *read_list(void) {
  struct node *p, *first = NULL;
  int i, V;  // <<<<<<<<<<<<<<<<<<< V not initialized

  for (i = 0; i<V; i++) {
              //^ trouble here
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43341847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档