主题:这个邻接矩阵存储图的原代码怎么运行输入一个数字后就出错??
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# define INFINITY 1000
# define MAX_VERTEX_NUM 20
# define OK 1
typedef enum{DG,DN,UDG,UDN} GraphKind;
typedef int EType;
typedef int InfoType;
typedef int VertexType;
typedef struct ArcCell //define structure MGraph
{ EType adj;
InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{ VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
int CreatUDN(MGraph *G) //CreatUDN() sub-function
{ int IncInfo,i,j,k,v1,v2,w;
printf("\n Please input the number of G->vexnum:");
scanf("%c",&G->vexnum);
printf("Please input the number of G->arcnum:");
scanf("%d",&G->arcnum);
printf("Please input IncInfo (0 for none) :");
scanf("%s",IncInfo);
for(i=0;i<G->vexnum;++i)
for(j=0;j<G->vexnum;++j)
{ G->arcs[i][j].adj=INFINITY; //initial G.arcs[i][j].adj
G->arcs[i][j].info=NULL; //initial G.arcs[i][j].info
}
printf("Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)...");
for(k=0;k<G->arcnum;++k) //input arc(v1,v2)
{
printf("Please input the %dth arc's v1 (0<v1<G->vexnum) :",k+1);
printf("Please input the %dth arc's v2 (0<v1<G->vexnum) :",k+1);
scanf("%d",&v2);
printf("Please input the %dth arc's weight :",k+1);
scanf("%d",&w);
i=v1;
j=v2;
while(i<1||i>G->vexnum||j<1||j>G->vexnum) //if (v1,v2) illegal,again
{
printf("the num you input isn't suitable,please input again\n");
printf("Please input the %dth arc's v1 (0<v1<G->vexnum) :",k+1);
scanf("%d",&v1);
printf("Please input the %dth arc's v2 (0<v1<G->vexnum) :",k+1);
scanf("%d",&v2);
printf("Please input the %dth arc's weight :",k+1);
scanf("%d",&w);
i=v1;
j=v2;
} //while end
i--;
j--;
G->arcs[i][j].adj=w; //weight
printf("G->arcs[%d][%d].adj=",i+1,j+1);
if(IncInfo)
{
printf("Please input the %dth arc's Info :",k+1);
scanf("%s",G->arcs[i][j].info);
}
} //for end
return (OK);
} //CreatUDN() end
void main() //main() function
{ MGraph *G;
printf("\n\nCreatUDN.c");
printf("\n============\n");
if(CreatUDN(G)) //call CreatUDN()
printf("\nCreate MGraph success !");
printf("\n\n...OK!...");
getch();
} //main() end
我觉得可能是没所初始化这个图
??
# include <stdlib.h>
# include <conio.h>
# define INFINITY 1000
# define MAX_VERTEX_NUM 20
# define OK 1
typedef enum{DG,DN,UDG,UDN} GraphKind;
typedef int EType;
typedef int InfoType;
typedef int VertexType;
typedef struct ArcCell //define structure MGraph
{ EType adj;
InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{ VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
int CreatUDN(MGraph *G) //CreatUDN() sub-function
{ int IncInfo,i,j,k,v1,v2,w;
printf("\n Please input the number of G->vexnum:");
scanf("%c",&G->vexnum);
printf("Please input the number of G->arcnum:");
scanf("%d",&G->arcnum);
printf("Please input IncInfo (0 for none) :");
scanf("%s",IncInfo);
for(i=0;i<G->vexnum;++i)
for(j=0;j<G->vexnum;++j)
{ G->arcs[i][j].adj=INFINITY; //initial G.arcs[i][j].adj
G->arcs[i][j].info=NULL; //initial G.arcs[i][j].info
}
printf("Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)...");
for(k=0;k<G->arcnum;++k) //input arc(v1,v2)
{
printf("Please input the %dth arc's v1 (0<v1<G->vexnum) :",k+1);
printf("Please input the %dth arc's v2 (0<v1<G->vexnum) :",k+1);
scanf("%d",&v2);
printf("Please input the %dth arc's weight :",k+1);
scanf("%d",&w);
i=v1;
j=v2;
while(i<1||i>G->vexnum||j<1||j>G->vexnum) //if (v1,v2) illegal,again
{
printf("the num you input isn't suitable,please input again\n");
printf("Please input the %dth arc's v1 (0<v1<G->vexnum) :",k+1);
scanf("%d",&v1);
printf("Please input the %dth arc's v2 (0<v1<G->vexnum) :",k+1);
scanf("%d",&v2);
printf("Please input the %dth arc's weight :",k+1);
scanf("%d",&w);
i=v1;
j=v2;
} //while end
i--;
j--;
G->arcs[i][j].adj=w; //weight
printf("G->arcs[%d][%d].adj=",i+1,j+1);
if(IncInfo)
{
printf("Please input the %dth arc's Info :",k+1);
scanf("%s",G->arcs[i][j].info);
}
} //for end
return (OK);
} //CreatUDN() end
void main() //main() function
{ MGraph *G;
printf("\n\nCreatUDN.c");
printf("\n============\n");
if(CreatUDN(G)) //call CreatUDN()
printf("\nCreate MGraph success !");
printf("\n\n...OK!...");
getch();
} //main() end
我觉得可能是没所初始化这个图
??