DFS:
#include<iostream>
#include<stdlib.h>
#define OK 1
#define ERROR 0
using namespace std;
#define MAX_VERTEX_NUM 20 //图的邻接表存储表示
typedef int InfoType;
typedef int VertexType;
typedef struct ArcNode{
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
InfoType *info; //该弧相关信息的指针
}ArcNode;
typedef struct VNode {
VertexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点弧的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices;
int vexnum,arcnum; //图的当前顶点数和弧数
int kind; //图的种类标志
}ALGraph;
bool visited[MAX_VERTEX_NUM];
int CreateUDG(ALGraph *G);
int CreateGraph(ALGraph *G);
int LocateVex(ALGraph G,int v);
int OutPutUDG(ALGraph G);
void DFS(ALGraph G,int v);
void DFSTraverse(ALGraph G);
void DFSTraverse(ALGraph G){
for(int v=0;v<G.vexnum;v++) visited[v]=false;
for(int v=0;v<G.vexnum;v++){
if(!visited[v]) DFS(G,v);
}
}
void DFS(ALGraph G,int v){
visited[v]=true;
printf("%d ",v);
ArcNode *w=G.vertices[v].firstarc;
while(w){
if(!visited[w->adjvex]) DFS(G,w->adjvex);
w=w->nextarc;
}
}
int CreateGraph(ALGraph *G){
printf("你想建哪种类型的图(1:DG 2:UDG):");
scanf("%d",&(G->kind)); //2
switch(G->kind){
//case DG :return CreateDG(G);
case 2:printf("UDG"); return CreateUDG(G);
default: printf("ERROR"); return ERROR;
}
}<