#include#includetypedef int Dtype;#define MAXVEX 20#defineINFINITY 32768int visited[MAXVEX] = { 0 };typedef struct ArcNode{int Adjvex;struct ArcNode *next;}ArcNode;typedef struct VertexNode{Dtype Adjdata;ArcNode *first;}VertexNode;typedef struct{VertexNode vertex[MAXVEX];int vexnum;int arcnum;}AdjList;void CreatAdjList(AdjList *G){ArcNode *p;printf("请输入图中顶点的个数:");scanf_s("%d",&G->vexnum);printf("请输入图中的边的个数:");scanf_s("%d",&G->arcnum);G->vertex[0].Adjdata = 0;G->vertex[0].first=NULL;for (int i = 1; i <= G->vexnum; i++) {printf("请输入第%d个顶点的值:\n",i);scanf_s("%d", &G->vertex[i].Adjdata);G->vertex[i].first = NULL;}for (int k = 1; k <= G->arcnum;k++){int i,j;printf("请输入边(vi-vj)的对应的下标值:");scanf_s("%d,%d",&i,&j);p = (ArcNode*)malloc(sizeof(ArcNode));p->Adjvex = j;p->next = G->vertex[i].first;G->vertex[i].first = p;p = (ArcNode*)malloc(sizeof(ArcNode));p->Adjvex = i;p->next = G->vertex[j].first;G->vertex[j].first = p;}}void visit(int v){printf("%d\t", v);}void DFS(AdjList *g, int i){visit(g->vertex[i].Adjdata);visited[i] = 1;ArcNode *p;p = g->vertex[i].first;while (p){if (visited[p->Adjvex] == 0)DFS(g, p->Adjvex);p = p->next;}}void DFSTraverse(AdjList *g){int i;for (i = 1; i <= g->vexnum; i++)visited[i] = 0;for (i = 1; i <= g->vexnum; i++){if (visited[i] == 0){DFS(g,i);}}}int main(){AdjList G;CreatAdjList(&G);DFSTraverse(&G);}