邻接表的深度优先遍历
求指点,错哪了,不能遍历全部的节点//圖的鄰接表表示
#include<stdio.h>
#include<stdlib.h>
#define max_vertex_num 20
#define null 0
#define Gameover 0
int visited[max_vertex_num];
int (*visitfunc)(int v);
typedef struct arcnode
{
int adjvex;
struct arcnode * next;
}arcnode;
typedef struct vnode
{
int data;
arcnode * fristarc;
}vnode,adjlist[max_vertex_num];
typedef struct
{
adjlist vertices;
int vexnum,arcnum;
}Algraph;
Algraph G;
int create(Algraph & G)
{
printf("请输入顶点数目:\n");
scanf("%d",&G.vexnum);
printf("请输入弧的数目:\n");
scanf("%d",&G.arcnum);
printf("请输入顶点的信息:\n");
for(int i=0;i<G.vexnum;i++)
{
scanf("%d",&G.vertices[i].data);
G.vertices[i].fristarc=null;
}
for(int k=0;k<G.arcnum;k++)
{
arcnode * p;
int i,j;
printf("请输入弧的弧头和弧尾:\n");
scanf("%d %d",&i,&j);
p=(arcnode *)malloc(sizeof(arcnode));
p->adjvex=j;
if(!p)
{
printf("Gameover\n");
return 0;
}
p->next=G.vertices[i].fristarc;
G.vertices[i].fristarc=p;
}
return 1;
}
int put(Algraph & G)
{
arcnode *p;
printf("输出图:\n");
for(int k=0;k<G.vexnum;k++)
{
p=G.vertices[k].fristarc;
while(p!=null)
{
printf("(%d->%d)\n",k,p->adjvex);
p=p->next;
}
}
return 1;
}
int visit(int v)
{
printf("%d\n",G.vertices[v].data);
return 1;
}
int firstadjvex(Algraph & G,int v)
{
int w ;
if(G.vertices[v].fristarc!=null)
w=G.vertices[v].fristarc->adjvex;
else
w=-1;
return w;
}
int nextadjvex(Algraph & G , int v, int w)
{
if(G.vertices[v].fristarc->next->next!=null)
w=G.vertices[v].fristarc->next->adjvex;
else
w=-1;
return w;
}
void DFS(Algraph & G,int v)
{
int w;
visited[v]=1;
visitfunc(v);
printf("\n");
for(w=firstadjvex(G,v);w>=0;w=nextadjvex(G,v,w))
if(!visited[w])
DFS(G,w);
}
void dfstraverse (Algraph & G,int (* visit)(int v))
{
int v;
visitfunc=visit;
for(v=0;v<G.vexnum;++v)
visited[v]=0;
for(v=0;v<G.vexnum;++v)
if(!visited[v])
DFS(G,v);
}
void main()
{
int a;
for(int m=0;m<3;m++)
{
printf("请输入1,2,3来选择程序:\n");
scanf("%d",&a);
if(a==1)
create(G);
else
if(a==2)
put(G);
else
if(a==3)
dfstraverse (G,visit);
}
}