数据结构-图-C语言-邻接表-图的遍历
所需的结构
typedef int Vertex;
typedef int WeightType;
typedef struct GNode *pGNode;
struct GNode {
int nv;
int ne;
AdjList g;
};
typedef pGNode LGraph;
typedef struct ENode *pENode;
struct ENode {
Vertex v, w;
WeightType weight;
};
typedef pENode Edge;
struct VNode {
pAdjVNode firstEdge;
};
typedef VNode AdjList[Max];
typedef struct AdjVNode *pAdjVNode;
struct AdjVNode {
Vertex adjV;
WeightType weight;
pAdjVNode next;
};
实现
bool visited[100];
void visit(Vertex V){
printf("正在访问顶点%d\n", V);
}
void DFS(Vertex v, LGraph graph, void(*visit)(Vertex v)) {
pAdjVNode vNode = graph->g[v].firstEdge;
visit(v);
visited[v] = true;
for (vNode = graph->g[v].firstEdge; vNode; vNode->next) {
if (!visited[vNode->adjV]) {
DFS(vNode->adjV, graph,visit);
}
}
}