无向图的邻接矩阵,深度优先遍历和广度优先遍历的递归与非递归算法

/*(1)输入一组顶点,建立无向图的邻接矩阵。
进行DFS(深度优先遍历)和BFS(广度优先遍历)。
写出深度优先遍历的递归和非递归算法。*/
#include
#define max 40  //最大顶点个数
#define M 10000 //无穷 
#include
typedef struct ArcCell{
	int adj;  //1/0表示是否有边。网中表示权值 
}ArcCell,AdjMatrix[max][max];
typedef struct{
	char vexs[max]; //顶点表
	AdjMatrix arcs; //矩阵表 
	int vexnum,arcnum; //顶点数和边数 
}MGraph;
typedef struct QNode{
	int data;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue &Q){
	Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q.front)
		exit(0);
	Q.front->next=NULL;
	return 1;
}
int QueueEmpty(LinkQueue Q){
	if(Q.front==Q.rear)
		return 1;
	else
		return 0;
}
int EnQueue(LinkQueue &Q,int e){
	QueuePtr p;
	p=(QueuePtr)malloc(sizeof(QNode));
	if(!p) exit(0);
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	return 1;
}
int DeQueue(LinkQueue &Q,int &e){
	QueuePtr p;
    if(Q.front==Q.rear)
		return 0;
	p=Q.front->next;
	e=p->data;
	Q.front->next=p->next;
	if(Q.rear==p)
		Q.rear=Q.front;
	free(p);
	return 1;
}
int Locatevex(MGraph G,char v){
	int i; 
	for(i=0;i=0;w=NextAdjvex(G,u,w))
					if(!visited2[w]){
						visited2[w]=1;
						printf("%c",G.vexs[w]);
						EnQueue(Q,w);
					}	
		    }
	    }
} 
void BfsTraverse(MGraph G){
	//广度优先遍历
	int visited3[max];
	int v,u,w; 
	LinkQueue Q;
	for(v=0;v

你可能感兴趣的:(数据结构)