Welcome to |

Algorithm_Weaver

园龄:2年4个月粉丝:0关注:0

图的存储之邻接矩阵

邻接矩阵存图:

c++

复制代码
 1 #include<iostream>
 2 using namespace std;
 3 
 4 class AM//邻接矩阵存图 用法 AM 图的名称 ={规模长(int),规模宽(int),是否为无向图,是为true,不是为false(bool)} 
 5 {
 6     private:
 7         int sn, sm;
 8         bool is_undirected_graph = true;//是否为无向图,是为true,不是为false
 9         bool arry[sn][sm];
10     public:
11         void AM(int input_n, int input_m,bool input_is_undirected_graph)//构造函数 input_n是规模长,input_m是规模宽,input_is_undirected_graph是 是否为无向图
12         {
13             this->sn = input_n; this->sm = input_m;//赋值
14             this->is_undirected_graph = input_is_undirected_graph;//赋值
15             return;//写不写都行
16         }
17         void ~AM(){}//析构函数,这里没什么用,写上更好
18         void link(int _A_, int _B_)//从_A_到_B_连边 
19         {
20             if (is_undirected_garph == true)//如果是无向图
21             {
22                 this->arry[_A_][_B_] = this->arry[_B_][_A_] = true;//双向边连上
23                 return;//写不写都行
24             }
25             else//如果不是
26             {
27                 this->arry[_A_][_B_] = true;//只连一个
28                 return;//写不写都行
29             }
30             return//写不写都行
31         }
32         bool unlink(int _A_, int _B_)//去掉_A_到_B_这条边,成功返回true,失败返回false
33         {
34             if (this->arry[_A_][_B_] == false)//如果没边 return false;
35             {
36                 return false;
37             }
38             if (is_undirected_garph == true)//如果是无向图
39             {
40                 this->arry[_A_][_B_] = this->arry[_B_][_A_] = false;//两边都删
41                 return true;//成功
42             }
43             else//如果不是
44             {
45                 this->arry[_A_][_B_] = false;//只删一边
46                 return true;//成功
47             }
48         }
49         boll ask(int _A_, int _B_)//_A_到_B_是否有边
50         {
51             return this->arry[_A_][_B_] == true ? true : false;
52             /*三目运算符,如果有边,返回true,没变返回false
53             * 格式为 a ? b : c
54             * 意为如果a成立那么表达式为b,如果a不成立,那么返回c
55             * 可以译作C++代码:
56             * if (a) //也可写作if (a==true)
57             * {
58             *    return b;
59             * }
60             * else
61             * {
62             *    return c;
63             * }
64             */
65         }
66 };    
复制代码

 

本文作者:Algorithm_Weaver的博客

本文链接:https://www.cnblogs.com/algorithm-weaver/p/juzhen.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Algorithm_Weaver  阅读(48)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起