问题及代码:
/*
*烟台大学计算机与控制工程学院
*作 者:孙丽玮
*完成日期:2016年11月17日
*问题描述:利用图算法库实现深度优先(DFS)遍历
*/
1、graph.h的代码(见图基本算法库)
2、graph.cpp的代码(见图基本算法库)
3、main.cpp的代码
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int visited[MAXV];
void DFS(ALGraph *G, int v)
{
ArcNode *p;
int w;
visited[v]=1;
printf("%d ", v);
p=G->adjlist[v].firstarc;
while (p!=NULL)
{
w=p->adjvex;
if (visited[w]==0)
DFS(G,w);
p=p->nextarc;
}
}
int main()
{
int i;
ALGraph *G;
int A[6][6]=
{
{0,1,1,1,0,0},
{1,0,1,0,1,0},
{1,1,0,0,0,1},
{1,0,0,0,0,1},
{0,1,0,0,0,1},
{0,0,1,1,1,0}
};
ArrayToList(A[0], 6, G);
for(i