【问题标题】:I can't make this dijkstra code compile. (The Algorithm Design Manual)我无法编译这个 dijkstra 代码。 (算法设计手册)
【发布时间】:2010-06-28 22:31:24
【问题描述】:

此代码是我从算法设计手册中构建的代码,但我无法编译它,因为我对指针的经验很少我认为这是我认为我无法编译它的主要原因:

如果有人可以在 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;
}

【问题讨论】:

  • 除了(未使用的?)iostream 包括这是 C,而不是 C++。因此我添加了 C 标签。
  • 如果您发布编译器错误消息可能会有所帮助。
  • @Yacoby:还有using namespace std;
  • @Fred: ...这是用于(未使用的?)iostreams。
  • 这与概率无关,但您应该(阅读必须)尽可能检查 NULL 指针。例如。每当你有函数 foo(char pChar)*,你要做的第一件事就是 if(pChar==NULL) return;

标签: c++ c graph dijkstra shortest-path


【解决方案1】:

更改结构的定义,它会编译。

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

虽然这会解决您的问题,但请不要相信我在下面的回答,除非有人比我更优秀。


您的代码有什么问题?

在编译器知道该类型之前,您正在使用 typedef-ed type。相反,您需要使用 structure_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 返回 *void**,您需要将其转换为您分配给它的类型。 因此,在函数 add_edges 中,进行此更正(请在书中详细了解此内容,了解这一点很重要):

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

【讨论】:

  • 你提到了第一个问题,但实际上 add_edge 函数中还有另一个问题是阻止代码编译,我不知道它是怎么回事??(可能是关于内存分配)跨度>
  • 非常感谢你拯救了我的一天:D
  • 实际上 Gollum 我改变了边缘的结构,使其成为 struct edge{ int y;重量;整数距离;边缘*下一个; };并试图通过堆制作一个dijkstra,但我真的无法管理它,这是我的dijkstra代码,你会更新它吗?
【解决方案2】:

类型定义结构

{

int y;

重量;

struct edgenode *next;

} 边缘节点;

在这里,您使用 typedef 结构而不定义它,然后您在定义 edgenode 之前在结构定义中使用 edgenode。


所以你应该把它改成:

typedef struct _edgenode

{

int y;

重量;

struct _edgenode *next;

} 边缘节点;

【讨论】:

  • 如果您正在编写代码,然后将其缩进 4 个空格字符,将为您生成一个代码块:-)。
  • @咕噜,谢谢哥们。我不知道该怎么做。谢谢。 :)
  • 你提到了第一个问题,但实际上 add_edge 函数中还有另一个问题是阻止代码编译,我不知道它是怎么回事??(可能是关于内存分配)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
相关资源
最近更新 更多