【数据结构与算法】带权有向图

本文介绍如何在C++中实现带权有向图的数据结构,并探讨相关算法,包括MyGraph.h和MyGraph.cpp的实现,以及源.cpp文件中具体的图操作示例。
摘要由CSDN通过智能技术生成

MyGraph.h

#pragma once
#include <iostream>
#include <stack>
#include <queue>
using namespace std;

// 邻接矩阵
// 带权有向图

const int MAXSIZE = 20;
const int INFINITE = 100;

template <class T>
class CMyGraph
{
public:
    CMyGraph();
    ~CMyGraph();

private:
    T vertexArray[MAXSIZE] = {
  0};               // 顶点集合
    int edge[MAXSIZE][MAXSIZE];                 // 边集合
    int numVertex = 0;                          // 顶点的数量
    int numEdge = 0;                            // 边的数量

private:
    bool isGraphEmpty(void) const;      // 图是否为空
    bool isGraphFull(void) const;       // 图是否满了
    int getVertexPos(const T& vertex);  // 得到顶点的位置,若不存在则返回-1
    void errorMessage(int i);           // 错误信息

public:
    int GetNumOfVertices(void) const;       // 获得顶点的数量
    int GetNumOfEdge(void) const;           // 边的数量
    int GetWeight(const T& vertex1, const T& vertex2);  //得到边的权重
    void InsertVertex(const T& vertex);                                         // 插入顶点
    void InsertEdge(const T& vertex1, const T& vertex2, const int weight);      // 插入边
    void DeleteVertex(const T& vertex);     // 删除顶点
    void DeleteEdge(const T& vertex1, const T& vertex2); // 删除边
    void Display(void);         // 显示图

    T* DepthFirstSearch(const T& beginVertex);  //深度优先搜索
    T* BreathFirestSearch(const T& beginVertex);//广度优先搜索

    bool PathConnect(const T& vertex1, const T& vertex2);   //两顶点之间是否联通

};

MyGraph.cpp

#include "MyGraph.h"

template<class T>
CMyGraph<T>::CMyGraph()
{
    for (int i = 0; i < MAXSIZE; i++) {
        for (int j = 0; j < MAX
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值