




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、图实验一, 邻接矩阵的实现1. 实验目的(1) 掌握图的逻辑结构(2) 掌握图的邻接矩阵的存储结构(3) 验证图的邻接矩阵存储及其遍历操作的实现2. 实验内容(1) 建立无向图的邻接矩阵存储(2) 进行深度优先遍历(3) 进行广度优先遍历3设计与编码MGraph.h#ifndef MGraph_H#define MGraph_Hconst int MaxSize = 10;template<class DataType>class MGraphpublic:MGraph(DataType a, int n, int e);MGraph()void DFSTraverse(int v
2、);void BFSTraverse(int v);private:DataType vertexMaxSize;int arcMaxSizeMaxSize;int vertexNum, arcNum;#endifMGraph.cpp#include<iostream>using namespace std;#include "MGraph.h"extern int visitedMaxSize;template<class DataType>MGraph<DataType>:MGraph(DataType a, int n, int e
3、)int i, j, k;vertexNum = n, arcNum = e;for(i = 0; i < vertexNum; i+)vertexi = ai;for(i = 0;i < vertexNum; i+)for(j = 0; j < vertexNum; j+)arcij = 0;for(k = 0; k < arcNum; k+)cout << "Please enter two vertexs number of edge: "cin >> i >> j;arcij = 1;arcji = 1;t
4、emplate<class DataType>void MGraph<DataType>:DFSTraverse(int v)cout << vertexv;visitedv = 1;for(int j = 0; j < vertexNum; j+)if(arcvj = 1 && visitedj = 0)DFSTraverse(j);template<class DataType>void MGraph<DataType>:BFSTraverse(int v)int QMaxSize;int front = -
5、1, rear = -1;cout << vertexv;visitedv = 1;Q+rear = v;while(front != rear)v = Q+front;for(int j = 0;j < vertexNum; j+)if(arcvj = 1 && visitedj = 0)cout << vertexj;visitedj = 1;Q+rear = j;MGraph_main.cpp#include<iostream>using namespace std;#include "MGraph.h"ext
6、ern int visitedMaxSize;template<class DataType>MGraph<DataType>:MGraph(DataType a, int n, int e)int i, j, k;vertexNum = n, arcNum = e;for(i = 0; i < vertexNum; i+)vertexi = ai;for(i = 0;i < vertexNum; i+)for(j = 0; j < vertexNum; j+)arcij = 0;for(k = 0; k < arcNum; k+)cout &l
7、t;< "Please enter two vertexs number of edge: "cin >> i >> j;arcij = 1;arcji = 1;template<class DataType>void MGraph<DataType>:DFSTraverse(int v)cout << vertexv;visitedv = 1;for(int j = 0; j < vertexNum; j+)if(arcvj = 1 && visitedj = 0)DFSTravers
8、e(j);template<class DataType>void MGraph<DataType>:BFSTraverse(int v)int QMaxSize;int front = -1, rear = -1;cout << vertexv;visitedv = 1;Q+rear = v;while(front != rear)v = Q+front;for(int j = 0;j < vertexNum; j+)if(arcvj = 1 && visitedj = 0)cout << vertexj;visitedj
9、 = 1;Q+rear = j;4. 运行与测试5. 总结与心得通过该实验的代码编写与调试,熟悉了邻接矩阵在图结构中的应用,在调试过程中遇到很多的问题,在解决问题过程中也使我的写代码能力得到提升二, 邻接表的实现1. 实验目的(1) 掌握图的逻辑结构(2) 掌握图的邻接表存储结构(3) 验证图的邻接表存储及其遍历操作的实现2. 实验内容(1) 建立一个有向图的邻接表存储结构(2) 对建立的有向图进行深度优先遍历(3) 对建立的有向图进行广度优先遍历3. 设计与编码ALGraph.h#ifndef ALGraph_H#define ALGraph_Hconst int MaxSize = 10;
10、struct ArcNodeint adjvex;ArcNode * next;template<class DataType>struct VertexNodeDataType vertex;ArcNode * firstedge;template<class DataType>class ALGraphpublic:ALGraph(DataType a, int n, int e);ALGraph();void DFSTraverse(int v);void BFSTraverse(int v);private:VertexNode<DataType>
11、adjlistMaxSize;int vertexNum, arcNum;#endifALGraph.cpp#include<iostream>using namespace std;#include"ALGraph.h"extern int visitedMaxSize;template<class DataType>ALGraph<DataType>:ALGraph(DataType a, int n, int e)ArcNode * s;int i, j, k;vertexNum = n; arcNum = e;for(i = 0;
12、 i < vertexNum; i+)adjlisti.vertex = ai;adjlisti.firstedge = NULL;for(k = 0; k < arcNum; k+)cout << "Please enter the edge of the serial number of two vertices: "cin >> i >> j;s = new ArcNode; s->adjvex = j;s->next = adjlisti.firstedge;adjlisti.firstedge = s;t
13、emplate<class DataType>ALGraph<DataType>:ALGraph()ArcNode * p = NULL;for(int i = 0; i < vertexNum; i+)p = adjlisti.firstedge;while(p != NULL)adjlisti.firstedge = p->next;delete p;p = adjlisti.firstedge;template<class DataType>void ALGraph<DataType>:DFSTraverse(int v)Arc
14、Node * p = NULL; int j;cout << adjlistv.vertex;visitedv = 1;p = adjlistv.firstedge;while(p != NULL)j = p->adjvex;if(visitedj = 0) DFSTraverse(j);p = p->next;template<class DataType>void ALGraph<DataType>:BFSTraverse(int v)int QMaxSize;int front = -1, rear = -1;ArcNode * p = N
15、ULL;cout << adjlistv.vertex; visitedv = 1; Q+rear = v;while(front != rear)v = Q+front;p = adjlistv.firstedge;while(p != NULL)int j = p->adjvex;if(visitedj = 0)cout << adjlistj.vertex; visitedj = 1; Q+rear = j;p = p->next;ALGraph_main.cpp#include<iostream>using namespace std;#include"ALGraph.cpp"int visitedMaxSize = 0;int main()char ch = 'A','B','C','D','E'int i;ALGraph<char> ALG(ch, 5, 6);for(i = 0; i < MaxSize; i+)visitedi = 0;cout << "Depth-first tra
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2030中国男茄克行业发展分析及竞争格局与发展趋势预测报告
- 2025至2030中国电子出版物行业深度研究及发展前景投资评估分析
- 2025至2030中国甲硝唑片行业产业运行态势及投资规划深度研究报告
- 《医疗机构工作人员廉洁从业九项准则》考核试卷(含答案)
- 茶艺知识培训课件
- 农林高校研究生课程思政建设评价研究
- 技术助力下的翻转课堂教学相长的实践案例
- 邮电系统培训课件资源
- 2025年中国PU球场数据监测研究报告
- 构建数字化教育公平的桥梁设计思维的智慧
- 艾滋病乙肝梅毒知识讲座
- 九年级化学下册 第11单元 课题2 化学肥料课件 新人教版
- 暖气片报价单范本
- 临床医学研究中心年度考核细则
- PSSE软件操作说明
- 22S803 圆形钢筋混凝土蓄水池
- 船舶交通管理系统VTS4
- 级配碎石试验段施工总结报告
- 人力资源管理概论第三章员工招聘、筛选与录用-董克用
- 护理部工作手册
- 盱眙龙虾连锁美食餐厅营运手册
评论
0/150
提交评论