2017-04-11 3 views
0

모든 꼭지점이 서로 연결되어있는 임의의 그래프에 대한 코드를 구현하려고합니다. 모서리는 무작위로 선택해야합니다. 이 코드를 작성했습니다 :구조가있는 무작위 그래프

#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는 '성공했습니다'라고 알려주지 만 코드는 아무 것도 인쇄하지 않습니다. (현재 선택된 '꼭지점 수'만 나타납니다.) 인접 목록이없고 모서리가 없습니다 .. 오류를 확인하고 오류 위치를 말해주십시오.

+6

"작동하지 않습니다"하는 오류에 대한 설명입니다. 작동하지 않습니다 무엇? 그것은 컴파일합니까?이 연결합니까? 그것은 잘못된 출력을 제공 하는가하지 무엇을 기대 했습니까? 디버그 작업은 무엇입니까? – Lundin

+0

Xcode는 '빌드 성공'이라고 알려주지 만 코드는 아무런 인쇄도하지 않습니다 (현재는 '꼭지점 수 선택'). 인접성 목록 없음, 가장자리 없음 ... –

+0

@math. 세계. 발생한 문제에 대한 정확한 설명과 함께 질문을 편집하십시오. 주석에 관련 정보를 넣지 마십시오. –

답변

0

for 문 아래의 변수 V가 초기화되지 않습니다.

... 
struct node *read_list(void) { 
    struct node *p, *first = NULL; 
    int i, V; // <<<<<<<<<<<<<<<<<<< V not initialized 

    for (i = 0; i<V; i++) { 
       //^ trouble here 
... 
관련 문제