用邻接表存储有向图并实现DFS(递归+非递归)BFS(非递归)两种遍历

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
#define MAX 20
typedef char Dtype;
typedef int mapmax[MAX][MAX];
int visited[MAX];

//-----------邻接表--------------
typedef char Diantype;
//弧的结构体 
typedef struct ArcNode{
    int         adddain;//指向顶点位置的指针 
    struct  ArcNode   *nextarc;//指向下一条弧的指针 
}ArcNode,*Arc;
//点的结构体 
typedef struct VNode{
    Diantype	data;//顶点信息 
    ArcNode		*firstarc;//指向第一条依附该点的弧的指针 
}VNode, AdjList[MAX];
typedef  struct{
      AdjList	toulist;	//所有头节点的表 
      int	dian,hu;	//当前顶点数和弧数
}ALG;     
int locate_dian(ALG H,Diantype a);
void Creat_ALG(ALG &H);

//-------------栈------------------
typedef struct{
	Arc *base;
	int top;
	int size;
}Stack;

int  Gettop(Stack S,Arc &e);
void Pop(Stack &S,Arc &e);
void Push(Stack &S,Arc e);
int Isempty(Stack S);
void InitStack(Stack &S);
//------------队列----------------------
typedef struct {	//队列 
	int *base;
	int front;
	int rear;
	int tag;
}Sq;
void InitSq(Sq &QQ);
int Getlen(Sq QQ);
int Isempty(Sq QQ);
void InSq(Sq &QQ,int e);
void OutSq(Sq &QQ,int &e);
void GetHead(Sq QQ); 
//-------------DFS-----------------
int first(ALG H,int v);//递归
void dfs1_ALG(ALG H,int i);	//递归深度遍历  
void DFS(ALG H,int v);
//---------------bfs------------------
void BFS(ALG H)
{
	memset(visited,0,sizeof(visited));
	Sq q;	InitSq(q);
	int u;
	for(int i=1;i<=H.hu;i++)
	{
		if(!visited[i])
		{	visited[i]=1;	cout<<H.toulist[i].data;
			InSq(q,i);
			ArcNode *p;
			while(!Isempty(q))
			{
				OutSq(q,u);
				if(!visited[u])
				{
					cout<<H.toulist[u].data;
					visited[u]=1;
				}
				if(H.toulist[u].firstarc)
				{
					p=H.toulist[u].firstarc;
				}
				while(p)
				{
					if(!visited[p->adddain])
					{
						cout<<H.toulist[p->adddain].data;	visited[p->adddain]=1;
						InSq(q,p->adddain);
					}
					p=p->nextarc;
				}
				/*for(int w=FirstAdjVex(G,u);w>= 0;w=NextAdjVex(G,u,w))	//注释掉之后为有向图 
				{
                    if( !visited[w])
					{
						visited[w]=1;	cout<<H.toulist[u].data;
                        InSq(q,w);
                    }//if
                }*/
			}
		}
	}
}

int main()
{
	ALG A;
	Creat_ALG(A);
	memset(visited,0,sizeof(visited));
	//----------DFS 递归+非递归-------------- 
/*	for(int i=1;i<=A.dian;i++)
	{
		if(!visited[i])
			dfs1_ALG(A,i);//递归 
			DFS(A,i);	//非递归 
	}
*/
	//-----------BFS-------------------------
	printf("\nBFS结果如下\n");
	BFS(A);
	
	return 0;
}
//-------------------dfs fei dugui---------------
void DFS(ALG H,int v)
{
	Stack S;InitStack(S);
	visited[v]=1;	cout<<H.toulist[v].data;
	ArcNode *p=H.toulist[v].firstarc;
	while(p || !Isempty(S))
	{
		while(p)
		{
			if(visited[p->adddain])
				p=p->nextarc;
			else
			{
				cout<<H.toulist[p->adddain].data;	visited[p->adddain]=1;
				Push(S,p);	p=H.toulist[p->adddain].firstarc;
			}
		}
		if(!Isempty(S))
			{
				Pop(S,p);	p=p->nextarc;
			}
	}
	
}


//---------------digui DFS----------------
int first(ALG H,int v)
{
	ArcNode *p=H.toulist[v].firstarc;
	if(!p)
		return 0;
	else
	{
		while(p->nextarc)
		{
			if(!visited[p->adddain])
			{
				return p->adddain;
				break;
			}
			p=p->nextarc;
		}
		if(!visited[p->adddain])
			return p->adddain;
		return 0;
	}
}
void dfs1_ALG(ALG H,int i)	//递归深度遍历 
{
	int w;
	visited[i]=1;	cout<<H.toulist[i].data<<endl;
	for(w=first(H,i);w>0;w=first(H,i))
	{
		if(!visited[w])
			dfs1_ALG(H,w);
	}
}
//---------------邻接表---------------------- 
int locate_dian(ALG H,Diantype a)
{
	for(int i=1;i<=H.dian;i++)
	{
		if(H.toulist[i].data==a)
			return i;
	}
	return 0;
}
void Creat_ALG(ALG &H)
{//根据输入的有向图G的顶点数及边数,建立图G的邻接表
	printf("请输入点数和弧数\n");
	cin>>H.dian>>H.hu;
	getchar();
	printf("请输入节点信息\n");
	for(int i=1;i<=H.dian;i++)
	{
		cin>>H.toulist[i].data;
		H.toulist[i].firstarc=NULL;
	}
	getchar();
	printf("请输入%d条弧\n",H.hu);
	for(int k=1;k<=H.hu;k++)
	{
		Diantype x,y;
		int i,j;
		ArcNode *p,*node;
		cin>>x>>y;	getchar();
		i=locate_dian(H,x);j=locate_dian(H,y);
		//----i->j-------
		node=new ArcNode;
		node->adddain=j;	node->nextarc=NULL;
		p=H.toulist[i].firstarc;
		if(!p)
			H.toulist[i].firstarc=node;
		else
			{
				while(p->nextarc)
					p=p->nextarc;
				p->nextarc=node;
			}
		//----j->i--------
		/*node=new ArcNode;
		node->adddain=i;	node->nextarc=NULL;
		p=H.toulist[j].firstarc;
		if(!p)
			H.toulist[j].firstarc=node;
		else
		{
			while(p->nextarc)
				p=p->nextarc;
			p->nextarc=node;
		}
		*/
	}
}
//---------------------栈-----------------
//初始化 
void InitStack(Stack &S)
{
	S.base=(Arc*)malloc(MAX*sizeof(ArcNode));
	if(!S.base)
		return ;
	S.top=0;
	S.size=MAX;
}
//判空操作,返回 1 表示栈为空 
int Isempty(Stack S)
{
	if(S.top==0)
		return 1;
	return 0;
}
//入栈操作
void Push(Stack &S,Arc e)
{
	if(S.top>=S.size)
	{
		printf("该栈已满\n");
		return ;	
	}

	S.base[S.top++]=e;
}
//出栈操作
void Pop(Stack &S,Arc &e)
{
	if (Isempty(S))    return;     //栈为空 
    e = S.base[--S.top];
}
//Gettop 取栈顶元素
int  Gettop(Stack S,Arc &e){
 //若栈不空则用e返回S的栈顶元素
 
   if (S.top == 0)
         return 0;      //栈为空
   e = S.base[S.top-1];
   return 1;
}
//----------------队列---------------- 
void InitSq(Sq &QQ)
{
	QQ.base=(int*)malloc(MAX * sizeof(int));	//S.base=(BiTree*)malloc(MAX*sizeof(BiTree)); 
	if(!QQ.base)
		return ;
	QQ.front=QQ.rear=QQ.tag=0;
}

int Getlen(Sq QQ)
{
	if(!QQ.tag)		//分情况讨论tag为 0,长度肯定为 0; 
		return 0;
	else
	{
		if(QQ.rear==QQ.front)
			return MAX;
		return (QQ.rear-QQ.front+MAX)%MAX;
	}
	
}

int Isempty(Sq QQ)
{
	if(QQ.front==QQ.rear && QQ.tag==0)
		return 1;
	return 0;	
}

void InSq(Sq &QQ,int e)
{

	if (QQ.front == QQ.rear && QQ.tag==1)	//tag与队头队尾必须同时满足 
	{
		printf("%d\n",QQ.tag);
		printf("队列已满\n");
		return ;
	}
	QQ.tag=1;
	//	Q.base=(Type )malloc(MAX * sizeof(Type));						
    QQ.base[QQ.rear] = e;
    QQ.rear =(QQ.rear+1)%MAX;
}
void OutSq(Sq &QQ,int &e)
{
	if(QQ.front == QQ.rear && QQ.tag==0)
	{
		printf("队列已空\n");	
		return;
	}
	e=QQ.base[QQ.front];
	QQ.front=(QQ.front+1)%MAX;
	if(QQ.front == QQ.rear)
		QQ.tag=0;
}

void GetHead(Sq QQ,int e)
{
    if(QQ.front == QQ.rear && QQ.tag==0) return;
    e = QQ.base[QQ.front];
} 
 

好的,这个问题是关于数据结构算法的,我可以回答你。 设计有向图邻接表类,可以定义如下: ``` class Graph { private: int numVertices; // 图中顶点数量 vector<vector<int>> adjList; // 邻接表,用 vector 存储每个顶点的邻居 public: Graph(int n) { numVertices = n; adjList.resize(n); } // 添加一条边 void addEdge(int src, int dest) { adjList[src].push_back(dest); } // 深度优先非递归遍历 void dfs(int start) { vector<bool> visited(numVertices, false); // 标记顶点是否已经被访问 stack<int> s; // 模拟递归栈 s.push(start); while (!s.empty()) { int cur = s.top(); s.pop(); if (visited[cur]) { continue; } visited[cur] = true; cout << cur << " "; // 输出遍历结果 // 将未访问过的邻居按倒序压入栈中,保证顺序是从左到右 for (int i = adjList[cur].size() - 1; i >= 0; i--) { int neighbor = adjList[cur][i]; if (!visited[neighbor]) { s.push(neighbor); } } } } // 广度优先遍历 void bfs(int start) { vector<bool> visited(numVertices, false); // 标记顶点是否已经被访问 queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int cur = q.front(); q.pop(); cout << cur << " "; // 输出遍历结果 // 遍历所有邻居,如果未访问过则压入队列 for (int neighbor : adjList[cur]) { if (!visited[neighbor]) { q.push(neighbor); visited[neighbor] = true; } } } } }; ``` 你可以根据以上代码实现一个有向图邻接表,并且调用 `dfs()` 或 `bfs()` 方法进行遍历。希望可以帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值