主题:程序算法
我有个程序,,没有语法错误,,运行时总是显示,,.exe停止工作,大家帮忙看看哪错了??谢啦
#include<stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 16
int visited[MAX_VERTEX_NUM];//指示顶点是否在当前路径上
typedef struct ArcNode
{
int adjvex;//该弧所指向的顶点的位置
struct ArcNode *nextarc;//指向下一条弧的指针
}ArcNode;
typedef struct VNode
{
int data;//顶点信息
ArcNode *firstarc;//指向第一条依附该顶点的弧的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum,arcnum;//图的当前顶点数和弧数
}ALGraph ;
void creat_DG_ALGraph(ALGraph *G)
{ //用邻接表的方式建一个有向图
int i,j,k;ArcNode *p;
*p=NULL;
printf("Please input: vexnum,arcnum=:");
scanf("%d,%d",&G->vexnum,&G->arcnum);
printf("Please input VNode:\n");
for(i=0;i<G->vexnum;i++)//输入顶点
{
scanf("%d",&G->vertices[i].data);
G->vertices[i].firstarc=NULL;
}
for(k=0;k<G->arcnum;k++)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
printf("please input edge <i,j>: ");//第k+1条边依附的两个顶点
scanf("%d,%d", &i, &j);
p->adjvex = j;
p->nextarc=G->vertices[i].firstarc;
G->vertices[i].firstarc=p;
}
}
int exist_path_DFS(ALGraph *G,int vi,int vj)//深度优先判断有向图G中顶点vi到顶点vj是否有路径
{
if(vi==vj) return 1; //vi就是vj
else
{
visited[vi]=1; //顶点存在
for(ArcNode *p=G->vertices[vi].firstarc;p;p=p->nextarc)
{
int k;
k=p->adjvex;
if(!visited[k]&&exist_path_DFS(G,k,vj)) return 1;//vi下游的顶点到vj有路径
}//for
return 0;
}//else
}//exist_path_DFS
void main()
{ALGraph *G;
int vi,vj;
G=NULL;
creat_DG_ALGraph(G);
printf("Please input vi->vj you want to find:\n");
scanf("%d,%d",&vi,&vj);
if(exist_path_DFS(G,vi,vj)) printf("Exist the path!");
else printf("Not exist the path");
}
#include<stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 16
int visited[MAX_VERTEX_NUM];//指示顶点是否在当前路径上
typedef struct ArcNode
{
int adjvex;//该弧所指向的顶点的位置
struct ArcNode *nextarc;//指向下一条弧的指针
}ArcNode;
typedef struct VNode
{
int data;//顶点信息
ArcNode *firstarc;//指向第一条依附该顶点的弧的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum,arcnum;//图的当前顶点数和弧数
}ALGraph ;
void creat_DG_ALGraph(ALGraph *G)
{ //用邻接表的方式建一个有向图
int i,j,k;ArcNode *p;
*p=NULL;
printf("Please input: vexnum,arcnum=:");
scanf("%d,%d",&G->vexnum,&G->arcnum);
printf("Please input VNode:\n");
for(i=0;i<G->vexnum;i++)//输入顶点
{
scanf("%d",&G->vertices[i].data);
G->vertices[i].firstarc=NULL;
}
for(k=0;k<G->arcnum;k++)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
printf("please input edge <i,j>: ");//第k+1条边依附的两个顶点
scanf("%d,%d", &i, &j);
p->adjvex = j;
p->nextarc=G->vertices[i].firstarc;
G->vertices[i].firstarc=p;
}
}
int exist_path_DFS(ALGraph *G,int vi,int vj)//深度优先判断有向图G中顶点vi到顶点vj是否有路径
{
if(vi==vj) return 1; //vi就是vj
else
{
visited[vi]=1; //顶点存在
for(ArcNode *p=G->vertices[vi].firstarc;p;p=p->nextarc)
{
int k;
k=p->adjvex;
if(!visited[k]&&exist_path_DFS(G,k,vj)) return 1;//vi下游的顶点到vj有路径
}//for
return 0;
}//else
}//exist_path_DFS
void main()
{ALGraph *G;
int vi,vj;
G=NULL;
creat_DG_ALGraph(G);
printf("Please input vi->vj you want to find:\n");
scanf("%d,%d",&vi,&vj);
if(exist_path_DFS(G,vi,vj)) printf("Exist the path!");
else printf("Not exist the path");
}