邻接表 实现图的遍历 C++
2016-11-29 22:28
615 查看
#include <iostream> #include <queue> using namespace std; const int MaxSize = 5; struct ArcNode //边表节点 { int adjvex; ArcNode* next; }; template <class T> struct VertexNode //定义顶点表节点 { T vertex; ArcNode* firstedge; }; template <class T> class AdjGraph { public: AdjGraph(T a[],int n,int e); //构造函数,建立具有n个顶点e条边的图 void dfsTraverse(int v); //深度优先遍历图 void bfsTraverse(int v); //广度优先遍历图 void Insert(int a,int b); //插入边表 void Show(); //显示邻接表 private: VertexNode<int> adjlist[MaxSize]; //存放顶点表的数组 int vertexNum, arcNum; //图的顶点数和边数 int visited[MaxSize]; //有没访问数组 queue<int> que; }; template <class T> AdjGraph<T>::AdjGraph(T a[],int n,int e) { vertexNum = n;arcNum = e; for(int i=0;i<vertexNum;i++) //初始化头节点表 { visited[i]=0; adjlist[i].vertex = a[i]; adjlist[i].firstedge=NULL; } } template <class T> void AdjGraph<T>::Insert(int vertex,int arcNode) { ArcNode *pArc = new ArcNode(); pArc->adjvex = arcNode; if(adjlist[vertex].firstedge == NULL) adjlist[vertex].firstedge=pArc; else { ArcNode *p = adjlist[vertex].firstedge; while(p->next!=NULL) p = p->next; p->next = pArc; } } template <class T> void AdjGraph<T>::Show() { cout<<"邻接表为:"<<endl; for(int i = 0; i < vertexNum; i++) { cout <<adjlist[i].vertex; ArcNode *p = adjlist[i].firstedge; while(p != NULL) { cout << "->" << p->adjvex; p = p->next; } cout << "->NULL" << endl; } } template <class T> void AdjGraph<T>::dfsTraverse(int v) //深度优先 { cout<<adjlist[v].vertex<<" "; visited[v]=1; ArcNode *p = adjlist[v].firstedge; //指向顶点v的边表 while(p!=NULL) { int j = p->adjvex; if(visited[j]==0)dfsTraverse(j); p=p->next; } } template <class T> void AdjGraph<T>::bfsTraverse(int v) //广度优先 { for(int i=0;i<vertexNum;i++) //被访问节点初始化 { visited[i]=0; } cout<<adjlist[v].vertex<<" "; visited[v]=1; que.push(v); //先把第一个元素放入队列 while(que.empty()!=1) //队列非空时 { v = que.front(); //获取队头元素并弹出 que.pop(); ArcNode *p = adjlist[v].firstedge; //指向顶点v的边表 while(p!=NULL) { int j = p->adjvex; if(visited[j]==0) //若没被访问 { cout<<adjlist[j].vertex<<" "; visited[j]=1; que.push(j); } p=p->next; } } } void main() { int a[5] = {0,1,2,3,4}; AdjGraph<int> myGraph(a,5,6); myGraph.Insert(0,1); myGraph.Insert(0,3); myGraph.Insert(1,0); myGraph.Insert(1,2); myGraph.Insert(1,4); myGraph.Insert(2,1); myGraph.Insert(2,3); myGraph.Insert(2,4); myGraph.Insert(3,0); myGraph.Insert(3,2); myGraph.Insert(4,2); myGraph.Insert(4,1); myGraph.Show(); cout<<endl<<"深度优先遍历:"<<endl; myGraph.dfsTraverse(0); cout<<endl; cout<<endl<<"广度优先遍历:"<<endl; myGraph.bfsTraverse(0); cout<<endl; }
相关文章推荐
- C++实现图的邻接表存储和广度优先遍历实例分析
- 图(有向图)的邻接表表示 C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency list
- 图的深度优先和广度优先遍历(图以邻接表表示,由C++面向对象实现)
- 图论(2) 无向图的实现(邻接表) 图的遍历
- 数据结构_图_邻接表做存储结构实现求无向图的连通分量_C++实现
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- 图的存储及遍历 深度遍历和广度遍历 C++代码实现
- c++与erlang实现目录遍历
- C++ 二叉树遍历(三种遍历的递归实现)
- 数据结构_树_二叉树的建立、遍历、复制与移除_二叉链表存储_C++实现
- 数据结构_图_邻接多重表做存储结构遍历无向图_C++实现
- 无向图的深度优先搜索(采用邻接表存储)C++实现
- 二叉树的后序遍历非递归算法之c++实现
- 求有向图的强连通分支(邻接表存储)C++实现
- 最小生成树Prim算法实现(采用邻接表存储)C++实现
- 二叉树(前中后序递归非递归遍历,层次遍历,C++实现)
- 邻接表实现图的遍历
- 图的遍历和生成树求解实现|图遍历,生成树,实现,邻接矩阵,邻接表,深度广度遍历,最小生成树
- 图的存储及遍历 深度遍历和广度遍历 C++代码实现
- 无向图的广度优先搜索(采用邻接表存储)C++实现