2010-06-28 4 views
2

이 코드는 알고리즘 설계 매뉴얼 책에서 작성한 코드이지만 포인터를 거의 사용하지 않아서 컴파일 할 수없는 이유는 그 것이 내가 생각할 수없는 주된 이유라고 생각합니다. 그것을 컴파일하십시오 :나는이 dijkstra 코드를 만들 수 없다. compille 누구든지 나를 도와 줄 수 있니? (알고리즘 설계 매뉴얼)

누군가가 djikstra에서 현재 구성으로 힙을 통과하도록 변경할 수있는 경우. 도와주세요.

#include<iostream> 
#include<stdio.h> 
#include<stdlib.h> 
using namespace std; 
const int MAXV=1000; 
const int MAXINT=99999; 

typedef struct{ 
    int y; 
    int weight; 
    struct edgenode *next; 
}edgenode; 
typedef struct{ 
    edgenode *edges[MAXV+1]; 
    int degree[MAXV+1]; 
    int nvertices; 
    int nedges; 
    bool directed; 
}graph; 

void add_edge(graph *g,int x,int y,int weight,bool directed); 

void read_graph(graph *g,bool directed){ 
    int x,y,weight,m; 
    g->nvertices=0; 
    g->nedges=0; 
    g->directed=directed; 
    for(int i=1;i<MAXV;++i) g->degree[i]=0; 
    for(int i=1;i<MAXV;++i) g->edges[i]=NULL; 
    scanf("%d %d",&(g->nvertices),&m); 
    for(int i=1;i<=m;++i){ 
     scanf("%d %d %d",&x,&y,&weight); 
     add_edge(g,x,y,weight,directed); 
    } 
} 

void add_edge(graph *g,int x,int y,int weight,bool directed){ 
    edgenode *p; 
    p=malloc(sizeof(edgenode)); 
    p->weight=weight; 
    p->y=y; 
    p->next=g->edges[x]; 

    g->edges[x]=p; 
    g->degree[x]++; 
    if(directed==false) add_edge(g,y,x,weight,true); 
    else g->nedges++; 
} 

int dijkstra(graph *g,int start,int end){ 
    edgenode *p; 
    bool intree[MAXV+1]; 
    int distance[MAXV+1]; 
    for(int i=1;i<=g->nvertices;++i){ 
     intree[i]=false; 
     distance[i]=MAXINT; 
    } 
    distance[start]=0; 
    int v=start; 
    while(intree[v]==false){ 
     intree[v]=true; 
     p=g->edges[v]; 
     while(p!=NULL){ 
      int cand=p->y; 
      int weight=p->weight; 
      if(distance[cand] > distance[v]+weight) distance[cand]=distance[v]+weight; 
      p=p->next; 
     } 
     v=1; 
     int dist=MAXINT; 
     for(int i=1;i<=g->nvertices;++i) 
      if((intree[i]==false) && (dist > distance[i])){ 
       dist=distance[i]; 
       v=i; 
      } 
    } 
    return distance[end]; 
} 

int main(){ 
    graph g; 
    read_graph(&g,false); 
    int x=1,y,shortest; 
    while(x!=0){ 
     scanf("%d %d",&x,&y); 
     shortest=dijkstra(&g,x,y); 
     printf("The shortest path from %d to %d is %d",x,y,shortest); 
    } 
    return 0; 
} 
+0

(사용하지 않은?) iostream을 제외하고 C++가 아니라 C가 포함됩니다. 그래서 나는 C 태그를 추가했다. – Yacoby

+0

컴파일러 오류 메시지를 게시 한 경우 도움이 될 수 있습니다. –

+0

@Yacoby : '네임 스페이스 표준 사용하기'도 있습니다. –

답변

3

구조체의 정의를 변경하면 컴파일됩니다.

struct edgenode_tag 
{ 
    int y; 
    int weight; 
    struct edgenode_tag *next; 
}; 
typedef edgenode_tag edgenode; 

이 문제를 해결할 수 있지만

은 나보다 더 나은 사람이 거기에 코멘트까지 아래 내 대답을 신뢰하지 않습니다.


코드에서 문제점은 무엇입니까? 컴파일러는 해당 유형에 대해 알고 전에

당신은 -ed 형식 정의 유형을 사용하고 있습니다. 대신 struct_tag를 사용하여 유형 자체의 멤버 포인터를 정의해야합니다.

typedef struct 
{ 
    ... 
    my_struct* pS; 
    ...   
} my_struct; // at this point compiler will know about *my_struct* type 
       // Hence, you can not use that name until after this line. 

       // To define the member pointer of type itself you need to 
       // to use the struct_tag, as I did in your example. 
       // where, struct_tag is *edgenode_tag* 

편집 :

는 또한, malloc에 ​​반환 당신이 그것을가 지정하는 형식으로 캐스팅 할 필요가있는 ** 무효 *. 따라서, 함수 내에서 add_edges,이 보정을 (책이에 대한 자세한 내용을 읽어 보시기 바랍니다 것이 중요 이것을 이해하기) :

    p = (edgenode*)malloc(sizeof(edgenode)); 
+0

첫 번째 문제에 대해 언급했지만 사실 add_edge 함수에서 코드가 컴파일되지 못하게하고 또 무엇이 있는지 모르겠습니다. (아마도 메모리 할당에 대해) – magiix

+0

@magiix는 업데이트 된 답변을 참조하십시오. –

+0

내 하루를 정말 많이 구해 주셔서 감사합니다. D – magiix

1

형식 정의 구조체

{

INT의 Y를;

int 중량;

구조체 edgenode * next;

} edgenode;

여기서 typedef 구조체를 정의하지 않고 사용하고 있으며 edgenode를 정의하기 전에 struct defination에서 edgenode를 사용하고 있습니다.


그래서 당신은 그것을 변경해야합니다 :

형식 정의 구조체 _edgenode

{

INT y를;

int 중량;

struct _edgenode * next;

} edgenode;

+0

코드를 작성한 후 4 개의 공백 문자로 들여 쓰면 코드 블록이 생성됩니다 :-). –

+0

@ 골룸, 고마워. 나는 그 일을하는 법을 몰랐다. 감사. :) –

+0

첫 번째 문제에 대해 언급했지만 사실 add_edge 함수에서 코드가 컴파일되지 못하게하고 또 어떤 문제인지 모르는 또 다른 문제가 있습니다. (아마도 메모리 할당에 대해) – magiix

관련 문제