版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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年高职模具设计与制造(复杂模具设计)试题及答案
- 大学(临床医学)儿科学基础2026年试题及答案
- 2025年中职(烹饪工艺)宴席菜品设计阶段测试题及答案
- 2025年大学大一(轮机工程)轮机自动化试题及答案
- 2025年高职(船舶电子电气技术)船舶电气设备试题及答案
- 2025年大学测绘工程(地图注记设计)试题及答案
- 2025年大学大二(种子科学与工程)种子生产学基础试题及答案
- 2025年中职(健康服务与管理)健康档案管理试题及答案
- 2025年高职汽车电子技术(汽车诊断技术)试题及答案
- 《李时珍》课件内容
- 2026高考化学复习难题速递之化学反应速率与化学平衡(解答题)(2025年11月)
- 2025年山东省枣庄市辅警(协警)招聘考试题库及答案
- 重庆试管婴儿合同协议
- 2025广西投资集团有限公司招聘4人笔试历年参考题库附带答案详解
- 基层医疗机构医疗质量精细化管理策略
- (安徽省十联考)合肥一中2026届高三12月份教学质量测生物试卷(含答案)
- 民兵军事训练的组织与实施
- 2025年广西公需科目答案2卷(含答案)
- DB11-T 2493-2025 餐饮服务单位使用丙类液体燃料消防安全管理规范
- 超星尔雅学习通《动手学AI人工智能通识与实践(理工版)》章节测试附答案
评论
0/150
提交评论