#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
int visited[MAXSIZE]={0};
typedef struct node
{
int adjvex;
struct node* next;
}EdgeNode;
typedef struct vnode
{
int vertex;
EdgeNode* firstedge;
}VertexNode;
void creatAdjlist(VertexNode g[],int e,int n)//n为顶点数,e为边数,g[]储存n个定点表节点
{
EdgeNode* p;
int i,j,k,x;
printf(“Input date of vetex(0~n-1)\n”);
for(i=0;i<n;i++)
{
printf(“input \n”);
scanf(“%d”,&x);
g[i].vertex=x;
g[i].firstedge=NULL;
}
for(k=1;k<=e;k++)
{
printf(“input edge of(i,j)”);
scanf(“%d%d”,&i,&j);
p=(EdgeNode*)malloc(sizeof(EdgeNode));
p->adjvex=j;
p->next=g[i].firstedge;
g[i].firstedge=p;
p=(EdgeNode*)malloc(sizeof(EdgeNode));
p->adjvex=i;
p->next=g[i].firstedge;
g[j].firstedge=p;
}
}
void DFS(VertexNode g[],int i)//深度优先搜索法
{
EdgeNode* p;
printf(“%4d”,g[i].vertex);
visited[i]=1;
p=g[i].firstedge;
while(p!=NULL)
{
if(!visited[p->adjvex])
DFS(g,p->adjvex);//对这个边结点进行深度优先搜索
p=p->next;
}
}
int main()
{
VertexNode g[4];
creatAdjlist(g,4,4);
DFS(g,1);
return 0;
}