版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 3487-2024乘用车轮辋规格系列
- 过元宵节的日记六篇
- 2024-2025学年小学心理健康一年级鄂科版教学设计合集
- 2024-2025学年高中物理选修3-5沪科版教学设计合集
- 2024年锦纶66-DTY项目投资申请报告
- 第5课《秋天的怀念》教学设计 2024-2025学年统编版语文七年级上册
- 教科版六年级科学上册 第7课《能量从哪里来》(教案)
- 2020-2021学年高一数学人教A版必修4第一章1.4.1 正弦函数、余弦函数的图象 教案
- 人教版(2019)选择性必修第一册Unit 1 People of Achievement Reading and thinking 教学设计
- 人教版八年级地理下学期9.2高原湿地-三江源地区 教学设计
- 教科版四年级科学上册 (营养要均衡)课件
- 如坐针毡:我与通用电气的风雨16年
- 国家义务教育质量监测四年级德育-道德与法治创新作业测试卷附答案
- DBJ51T 189-2022 四川省建设工程施工现场安全资料管理标准
- 部编三年级上册语文第二单元提升练习 (含答案)
- 眼部美容手术-内眦赘皮矫正术(美容外科学课件)
- 面瘫面神经炎面神经麻痹
- 消防产品不合格品处理
- 小学音乐-在祖国怀抱里教学课件设计
- 东湖街道房屋租赁合同范本
- 【高中物理】光的折射+课件+高二上学期物理人教版(2019)选择性必修第一册+
评论
0/150
提交评论