C++ 我能';不要让这个dijkstra代码编译。(算法设计手册)
这段代码是我根据《算法设计手册》编写的,但我无法编译,因为我对指针几乎没有经验,我认为这是我无法编译它的主要原因: 如果有人可以在djikstra中稍加修改,以使其通过当前配置的堆C++ 我能';不要让这个dijkstra代码编译。(算法设计手册),c++,c,graph,dijkstra,shortest-path,C++,C,Graph,Dijkstra,Shortest Path,这段代码是我根据《算法设计手册》编写的,但我无法编译,因为我对指针几乎没有经验,我认为这是我无法编译它的主要原因: 如果有人可以在djikstra中稍加修改,以使其通过当前配置的堆 #include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; const int MAXV=1000; const int MAXINT=99999; typedef struct{
#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;
}
#包括
#包括
#包括
使用名称空间std;
常数int MAXV=1000;
常量int MAXINT=99999;
类型定义结构{
int-y;
整数权重;
结构edgenode*下一步;
}边节点;
类型定义结构{
边节点*边[MAXV+1];
整数度[MAXV+1];
国际信托;
内杰斯;
布尔定向;
}图形;
void add_边(图*g,int x,int y,int weight,bool directed);
无效读取图(图*g,布尔定向){
整数x,y,重量,m;
g->n证书=0;
g->nedges=0;
g->定向=定向;
对于(int i=1;idegree[i]=0;
for(int i=1;iedges[i]=NULL;
scanf(“%d%d”,&(g->n证书),&m);
对于(int i=1;iweight=重量;
p->y=y;
p->next=g->边[x];
g->边[x]=p;
g->度[x]++;
如果(directed==false),则添加_边(g,y,x,weight,true);
else g->nedges++;
}
int dijkstra(图*g,int开始,int结束){
edgenode*p;
bool-intree[MAXV+1];
整数距离[MAXV+1];
对于(int i=1;逆变器;++i){
intree[i]=假;
距离[i]=MAXINT;
}
距离[起点]=0;
int v=开始;
while(intree[v]==false){
intree[v]=真;
p=g->边[v];
while(p!=NULL){
int cand=p->y;
int-weight=p->weight;
如果(距离[C]>距离[v]+重量)距离[C]=距离[v]+重量;
p=p->next;
}
v=1;
int dist=MAXINT;
对于(int i=1;逆变器;++i)
if((intree[i]==false)&&(dist>distance[i])){
dist=距离[i];
v=i;
}
}
返回距离[结束];
}
int main(){
图g;
读取图形(&g,false);
int x=1,y,最短;
而(x!=0){
scanf(“%d%d”,&x,&y);
最短=迪克斯特拉(&g,x,y);
printf(“从%d到%d的最短路径是%d”,x,y,最短);
}
返回0;
}
更改结构的定义,它将编译
struct edgenode_tag
{
int y;
int weight;
struct edgenode_tag *next;
};
typedef edgenode_tag edgenode;
虽然这会解决您的问题,但在有比我更好的人对我的答案发表评论之前,不要相信我下面的答案。
代码中有什么错误? 在编译器知道类型之前,您正在使用typedef-ed类型。相反,您需要使用structure_标记来定义类型本身的成员指针
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**,您需要将其强制转换为要分配给它的类型。
因此,在函数内部添加_边,进行此更正(请在本书中阅读更多关于此的内容,了解这一点很重要):
类型定义结构
{
int-y
整数权重
struct edgenode*下一步;
}边节点
在这里,您使用的是一个typedef结构,而没有定义它,然后在定义edgenode之前在结构定义中使用edgenode
因此,您应该将其更改为: typedef struct\u edgenode { int-y 整数权重 struct\u edgenode*下一步;
} EdGNODE;
除了(未使用的)IOFROW,这包括C,而不是C++。因此,我添加了C标记。如果您发布编译器错误消息,可能会有帮助。@ YACOBY:还有<代码>使用命名空间STD;< /Cord> @弗莱德:……是(未使用的)IOFATISH。这不与PROB有关,但您应该(读必须)。尽可能检查空指针。例如,每当你有函数foo(char pChar)*,你要做的第一件事就是如果(pChar==NULL)返回;如果你正在编写一个代码,然后用4个空格字符缩进它,就会为你生成一个代码块:-)。@Gollum,谢谢你,伙计。我不知道怎么做。谢谢。)您提到了第一个问题,但实际上add_edge函数中还有另一个问题,它阻止了代码的编译,我不知道这是关于什么的???(可能是关于内存分配)您提到了第一个问题,但事实上,add_edge函数中还有另一个问题,它阻止了代码的编译,我不知道这是怎么回事???(可能是关于内存分配)非常感谢您为我节省了很多时间:dactual Gollum我更改了edge的结构,使其成为struct edge{int y;int weight;int distance;edge*next;};并试图通过堆生成dijkstra,但我真的无法管理它,这是我的dijkstra代码。您能更新它吗? p = (edgenode*)malloc(sizeof(edgenode));