遍历图C++
来源:互联网 发布:jd是什么意思网络用语 编辑:程序博客网 时间:2024/06/05 22:33
#include<iostream>using namespace std;//构造一个循环队列来存放广度优先算法的下标#define ADD 5;using namespace std;class CirQueue{private: int * base; int front,rear,size,length;public: bool InitCirQueue(int size) { this->size=size; base=new int[size]; if(!base) { return false; } front=rear=length=0; return true; } bool insertQueue(int num) { if(length==size) { int newsize=size+ADD; int * newbase=new int[newsize]; if(!newbase) { return false; } int i=0; for(i=0;i<length;i++) { newbase[(front+i)%newsize]=base[(front+i)%size]; } rear=(front+i)%newsize; base=newbase; size=newsize; } base[rear]=num; rear=(rear+1)%size; ++length; return true; } int outQueue() { int temp=base[front]; front=(front+1)%size; --length; return temp; } void traverseQueue() { for(int i=0;i<length;i++) { cout<<base[(front+i)%size]<<endl; } } int getLength() { return length; } bool isEmpty() { if(0==length) { return true; } else { return false; } } bool isFull() { if(length==size) { return true; } else { return false; } }};//构造循环队列结束struct Arc{ int adjvex; Arc * next;};struct Vertex{ char data; Arc * firstarc;};class Map{private: Vertex * vexList; int vexNum; int arcNum; bool * visted;public: Map(int vexNum,int arcNum) { this->vexNum=vexNum; this->arcNum=arcNum; visted=new bool[vexNum]; for(int i=0;i<vexNum;i++) { visted[i]=false; } vexList=new Vertex[vexNum]; for(int i=0;i<vexNum;i++) { cout<<"请输入第"<<i<<"个顶点的数据:"; cin>>vexList[i].data; vexList[i].firstarc=NULL; } for(int i=0;i<arcNum;i++) { int a,b; cout<<"请输入第"<<i+1<<"条边的两顶点:"; cin>>a>>b; Arc * tempArc=new Arc; tempArc->adjvex=b; tempArc->next=vexList[a].firstarc; vexList[a].firstarc=tempArc; //因为是无向图所以是双向的 tempArc=new Arc; tempArc->adjvex=a; tempArc->next=vexList[b].firstarc; vexList[b].firstarc=tempArc; } } void DFS(int v)//深度优先遍历 { cout<<vexList[v].data<<endl; visted[v]=true; Arc * p=vexList[v].firstarc; while(p) { int u=p->adjvex; if(!visted[u]) { DFS(u); } p=p->next; } } void BFS(int v)//广度优先 { CirQueue cq; cq.InitCirQueue(5); cout<<vexList[v].data<<endl; visted[v]=true; cq.insertQueue(v); while(!cq.isEmpty()) { v=cq.outQueue(); Arc * p=vexList[v].firstarc; while(p) { int j=p->adjvex; if(!visted[j]) { cout<<vexList[j].data<<endl; visted[j]=true; cq.insertQueue(j); } p=p->next; } } } void ClearVisted() { for(int i=0;i<vexNum;i++) { visted[i]=false; } }};int main(){ Map map(5,4); cout<<"--------------深度优先遍历————————————————"<<endl; map.DFS(0); map.ClearVisted(); cout<<"--------------广度优先遍历————————————————"<<endl; map.BFS(0); return 0;}
阅读全文
0 0
- 图的遍历(C#)
- 图的深度遍历与广度遍历(C++)
- 数据结构(C++)<图的深度遍历和广度遍历>
- C/C++:遍历目录
- C语言 图的深度 遍历
- C语言数据结构之图的遍历
- C广度优先搜索,图的遍历
- 图的创建与遍历(C++)
- 图的遍历——C语言
- C-数据结构-图-深、广遍历
- C语言-数据结构-图的遍历
- 图bfs和dfs遍历 c语言版
- codeforces 839C Journey(图的遍历)
- 数据结构 图的遍历 C语言版
- 图的遍历 -深度优先—C
- 图的广度优先遍历—C
- linux c文件遍历
- C遍历磁盘文件
- 2017大数据版图
- windows 获取当前用户的sid
- web前端优化方法
- Spring bean的初始化和销毁三种方式及优先级
- 判断字符串是否为空 Android开发
- 遍历图C++
- 模板:最小割边数
- struts 2.3与2.5的差异
- 第一个Python程序
- java类集---IdentityHashMap
- 面试记录第二十二节——(lint检查)
- Swing界面中中文字体丑化的解决方案
- 批量免密登录
- vmware tools第一次安装失败后再次安装时出现的错误及解决