数据结构BFS与DFS邻接表

#include<bits/stdc++.h>
#define MVNum 100
#define OK 1
#define ERROR 0
using namespace std;
typedef int Status;
typedef int OtherTnfo;
typedef char VerTexType;
bool visit[MVNum];

typedef struct ArcNode
{
	int adjvex;
	struct ArcNode *nextarc;
	OtherTnfo info;
}ArcNode;

typedef struct VNode
{
	VerTexType data;
	struct ArcNode *firstarc;
}VNode,AdjList[MVNum];

typedef struct
{
	AdjList vertices;
	int vexnum,arcnum;
}ALGraph;

int LocateVex(ALGraph &G,char &v)
{
	int i;
	for (i=0;i<G.vexnum;i++)
	{
		if (G.vertices[i].data==v)
			return i;
	}
	if (i>=G.vexnum) return ERROR;
	else return 0;
}

Status CreateUDG(ALGraph &G)
{
	int i,j;
	char v1,v2;
	ArcNode *p1,*p2;
	printf("输入图的顶点数:");
	scanf("%d",&G.vexnum);
	printf("输入边的条数:");
	scanf("%d",&G.arcnum);
	printf("输入各个点的信息:\n");
	for (int i=0;i<G.vexnum;i++)
	{
		cin >> G.vertices[i].data;
		G.vertices[i].firstarc = NULL;
	}
	printf("\n输入由两个顶点构成的边:\n");
	for (int k=0;k<G.arcnum;k++)
	{
		cin >> v1 >> v2;
		i = LocateVex(G,v1);
		j = LocateVex(G,v2);
		p1 = new ArcNode;
		p1->adjvex = j;
		p1->nextarc = G.vertices[i].firstarc;
		G.vertices[i].firstarc = p1;
		p2 = new ArcNode;
		p2->adjvex = i;
		p2->nextarc = G.vertices[j].firstarc;
		G.vertices[j].firstarc = p2;
	}
	printf("\n图创建成功!\n");
	return OK;
}

void dfs(ALGraph G,int v)//非递归 
{
	stack<int> s;
	visit[v] = 1;
	s.push(v); 
	printf("%c ",G.vertices[v].data);
	ArcNode *p = G.vertices[v].firstarc;
	while (!s.empty())
	{
		while (p)
		{
			if (visit[p->adjvex])
				p = p->nextarc;
			else
			{
				printf("%c ",G.vertices[p->adjvex].data);
				visit[p->adjvex] = 1;
				s.push(p->adjvex);
				p = G.vertices[p->adjvex].firstarc;
			}
		}
		if (!s.empty())
		{
			p = G.vertices[s.top()].firstarc;
			s.pop();
		}
	}
}

void DFS(ALGraph G,int v)//递归 
{
	visit[v] = 1;
	printf("%c ",G.vertices[v].data);
	ArcNode *p = G.vertices[v].firstarc;
	while (p)
	{
		int w = p->adjvex;
		if (!visit[w]) DFS(G,w);
		p = p->nextarc;
	}
}

void BFS(ALGraph G,int v)
{
	ArcNode *p;
	queue<int> q;
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
	{
		if (!visit[i])
		{
			visit[i] = 1;
			printf("%c ",G.vertices[i].data);
			q.push(i);
			while (!q.empty())
			{
				i = q.front();
				q.pop();
				p = G.vertices[i].firstarc;
				while (p)
				{
					if (!visit[p->adjvex])
					{
						visit[p->adjvex] = 1;
						printf("%c ",G.vertices[p->adjvex].data);
						q.push(p->adjvex);
					}
					p = p->nextarc;
				}
			}
		}
	}
}

void DFSTravel(ALGraph G)
{
	printf("\nDFS递归遍历:\n");
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
		if (!visit[i]) 
			DFS(G,i);
	printf("\n");
	printf("\nDFS非递归遍历:\n"); 
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
		if (!visit[i]) 
			dfs(G,i);
	printf("\n");
}

void BFSTravel(ALGraph G)
{
	printf("\nBFS遍历:\n");
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
		if (!visit[i])
			BFS(G,i);
	printf("\n");
}

int main()
{
	ALGraph G;
	CreateUDG(G);
	DFSTravel(G);
	BFSTravel(G);
	return 0;
}

 

点赞