程序1
#define Max 20
#define NULL 0
typedef struct arcnode
{ int adjvex;
struct arcnode *nextarc;
}arcnode;
typedef struct vexnode
{ int vexdata;
arcnode *firstarc;
}vexnode;
typedef struct
{ vexnode adjlist[Max];
int vexnum;
}ALgraph;
int visited[Max];
main()
{ALgraph *g=NULL;
printf("Creat the Algragh:\n");
creat_adjlist(g);
printf("\nthe dfs is \n");
dfstraverse(g);
getch();
}
creat_adjlist(ALgraph *g)
{ arcnode *ptr;
int n,i,v1,v2;
printf("input number of vertex :\n");
scanf("%d",&n);
g->vexnum=n;
for (i=0;i<n;i++)
{ g->adjlist[i].firstarc=NULL;
g->adjlist[i].vexdata=i+1;
}
while(1)
{ printf("v1,v2(exit by 0 0 ):");
scanf("%d%d",&v1,&v2);
if((v1==0)&&(v2==0)) break;
ptr=(arcnode*)malloc(sizeof(arcnode));
ptr->adjvex=v2-1;
ptr->nextarc=g->adjlist[v1-1].firstarc;
g->adjlist[v1-1].firstarc=ptr;
ptr=(arcnode*)malloc(sizeof(arcnode));
ptr->adjvex=v1-1;
ptr->nextarc=g->adjlist[v2-1].firstarc;
g->adjlist[v2-1].firstarc=ptr;
}
}
dfstraverse (ALgraph *g)
{ int v;
for(v=0;v<g->vexnum;v++)
visited[v]=0;
for(v=0; v<g->vexnum; v++)
if (visited[v]==0) dfs (g,v);
}
dfs(ALgraph *g, int v0)
{ int w;
arcnode *p;
printf("%d ",v0+1 );
visited[v0]=1;
p=g->adjlist[v0].firstarc;
while(p)
{ w = p->adjvex;
if (visited[w]==0) dfs(g,w);
p = p->nextarc;
}
}
程序2
#define Max 20
#define NULL 0
typedef struct arcnode
{int adjvex;
struct arcnode *nextarc;
}arcnode;
typedef struct vexnode
{int vexdata;
arcnode *firstarc;
}vexnode;
typedef struct
{vexnode adjlist[Max];
int vexnum;
}ALgragh;
int visited[Max];
main()
{ALgragh *g=NULL;
printf("Creat the adjlist:\n");
creat_adjlist(g);
printf("\nthe dfs is \n");
dfstraverse(g);
getch();
}
creat_adjlist(ALgragh *g)
{arcnode *ptr;
int n,i,v1,v2;
printf("Input the number of vertex:\n");
scanf("%d",&n);
g->vexnum=n;
for(i=0;i<n;i++)
{g->adjlist[i].firstarc=NULL;
g->adjlist[i].vexdata=i+1;
/*visited[i]=0;*/
}
while(1)
{printf("\nv1,v2(end by 0 0):");
scanf("%d%d",&v1,&v2);
if((v1==0)&&(v2==0)) break;
ptr=(arcnode *)malloc(sizeof(arcnode));
ptr->adjvex=v2-1;
ptr->nextarc=g->adjlist[v1-1].firstarc;
g->adjlist[v1-1].firstarc=ptr;
ptr=(arcnode *)malloc(sizeof(arcnode));
ptr->adjvex=v1-1;
ptr->nextarc=g->adjlist[v2-1].firstarc;
g->adjlist[v2-1].firstarc=ptr;
}
}
dfstraverse (ALgraph *g)
{ int v;
for(v=0;v<g->vexnum;v++)
visited[v]=0;
for(v=0; v<g->vexnum; v++)
if (visited[v]==0) dfs (g,v);
}
dfs(ALgraph *g, int v0)
{ int w;
arcnode *p;
printf("%d ",v0+1 );
visited[v0]=1;
p=g->adjlist[v0].firstarc;
while(p)
{ w = p->adjvex;
if (visited[w]==0) dfs(g,w);
p = p->nextarc;
}
}
程序1是可用的,可是程序2在TC中调试时提示dfstraverse (ALgraph *g)参数列表错误,
两个程序基本相同,不知道问题在哪里,请高手指教!