لا يمكنني جعل هذا رمز Dijkstra ترجمة. (دليل تصميم الخوارزمية)
-
01-10-2019 - |
سؤال
هذا الرمز عبارة عن رمز قمت ببنيه من كتاب "تصميم الخوارزمية" ، لكن لا يمكنني أن أجعله يترجم لأنني لم أحصل على خبرة قليلة مع المؤشرات ، أعتقد أن هذا هو السبب الرئيسي الذي أعتقد أنني لا أستطيع تجميعه:
وإذا تمكن شخص ما من التغيير قليلاً في 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;
}
المحلول
تغيير تعريف الهيكل ، وسوف يجمع.
struct edgenode_tag
{
int y;
int weight;
struct edgenode_tag *next;
};
typedef edgenode_tag edgenode;
على الرغم من أن هذا سيحل مشكلتك ، لا تثق في إجابتي أدناه حتى يعلق شخص أفضل مني.
ما هو الخطأ في الكود الخاص بك؟
أنت تستخدم typedef-ed يكتب قبل أن يعرف المترجم هذا النوع. بدلاً من ذلك ، تحتاج إلى استخدام protect_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));
نصائح أخرى
Typedef Struct
{
int y ؛
وزن int
struct edgenode *التالي ؛
} edgenode ؛
هنا تستخدم هيكل typedef دون تحديد هذا ، ثم تستخدم edgenode في البنية الدفاسية قبل تحديد edgenode.
لذلك يجب عليك تغييره إلى:
Typedef struct _edgenode
{
int y ؛
وزن int
struct _edgenode *التالي ؛
} edgenode ؛