# include <stdio.h>
# include <stdlib.h>
#include <conio.h>
# define MAX 20
typedef int VexType;
typedef struct Vnode
{ VexType data;
struct Vnode *next;
}Vnode;
typedef Vnode Lgraph[MAX];
void creat_L(Lgraph G);
void out_L(Lgraph G);
void dfsL(Lgraph G,int v);
Lgraph Ga;
int n,e, vis[MAX];
main()
{ int v1,i; char ch;
for(i=0;i<MAX;i++) vis[i]=0;
creat_L(Ga);
out_L(Ga);
printf("\n "); scanf("%d",&v1);
dfsL(Ga,v1);
printf("\n\n 打回车键,继续。");
ch=getch();
}
void creat_L(Lgraph G)
{ Vnode *p,*q; int i,j,k;
printf("输入 n,e=?"); scanf("%d,%d",&n,&e);
for(i=1; i<=n; i++) { G[i].data=i; G[i].next=NULL;}
for(k=1;k<=e; k++)
{ printf("输入 vi,vj=?");scanf("%d,%d",&i,&j);
p=(Vnode *)malloc(sizeof(Vnode));
p->data=i;
p->next=G[j].next; G[j].next=p;
q=(Vnode *)malloc(sizeof(Vnode));
q->data=j;
q->next=G[i].next; G[i].next=q;
}
}
void out_L(Lgraph G)
{ int i; Vnode *p; char ch;
for (i=1; i<=n; i++)
{ printf("\n i=%d",i);
p=G[i].next;
while(p!=NULL) { printf("%5d",p->data); p=p->next;}
}
printf("\n\n 打回车键,继续。"); ch=getch();
}
void dfsL(Lgraph G,int v)
{ Vnode *p;
printf("%3d",G[v].data); vis[v]=1;
p=G[v].next;
while(p){ v=p->data;
if(vis[v]==0)dfsL(G,v);
p=p->next;
}
}