要求:对有向图进行DFS(深度优先遍历)、BFS(广度优先遍历)、拓扑排序。写出深度优先遍历的递归和非递归算法。
代码如下:
在此非常感谢crazy_27的评论,给我指出错误。
#include
#include
#include
#include
#include
#define MAX 100
using namespace std;
int visited[MAX+1];//记录是否访问过
typedef struct node{
int data;
struct node *next;
}Node,*ArcNode;
//定义邻接表的边类型
typedef struct vnode{
char data;
ArcNode firstarc;
}VNode;
//定义邻接表的结点类型
typedef struct Graph{
VNode AdjList[MAX];
int vexnum,arcnum;
}MGraph;
//定义图类型
int LocateVex(char ch,MGraph G)
{//取该结点在数组中的位置
int i;
for(i=0;i
if(G.AdjList[i].data==ch)
return i;
return -1;
}
void InitGraph(MGraph &G)
{//建立一个有向图
int n,m;
int i,j,k;
char ch1,ch2;
ArcNode p;
printf("请输入结点个数和边个数:");
scanf("%d %d",&G.vexnum,&G.arcnum);
printf("请输入结点:");
getchar();
for(i=0;i
{
scanf(