数据结构C++实现邻接矩阵存储图

本文介绍如何使用C++实现邻接矩阵来存储图。内容包括创建图、获取边权重、查找邻接顶点、深度优先遍历等操作,并提供了相关代码实现。
摘要由CSDN通过智能技术生成

定义邻接矩阵存储的图类。
[实验要求]

  1. 创建一个邻接矩阵存储的图;
  2. 返回图中指定边的权值;
  3. 查找图中某顶点的第一个邻接顶点、某顶点关于另一个顶点的下一个邻接顶点序号;
  4. 图的深度优先遍历;

[截图]

  1. 实验例图
    在这里插入图片描述
    在这里插入图片描述
  2. 操作截图

在这里插入图片描述

[实现代码]
一共有三个文件Graph_Martix.h、Graph_Martix.cpp和main.cpp

  • Graph_Martix.h
#ifndef _GRAPH_MARTIX_H
#define _GRAPH_MARTIX_H
#include <iostream>
using namespace std;
const int MaxGrzphSize=256;             //图的最大顶点个数
const int MaxWeight=1000;               //图中允许的最大权值
class Graph_Martix
{
   
private:
int edge[1000][256];
int graphsize;
public:
Graph_Martix();
int GetWeight(const int v1,const int v2);
int GetFirstNeighbor(const int v);
void GetAllNeighbor(const int v);
int GetNextNeighbor(const int v1,const int v2);
void DepthFirstSearch(int v);
void RDFS(const int v,int *visited);

};
#endif
  • Graph_Martix.cpp
#include "Graph_Martix.h" 
Graph_Martix::Graph_Martix() {
      
	cout
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值