数据结构系列:图(邻接矩阵)[C#]

说实话,昨天用c++学图的时候,感觉有些云里雾里,一直是跟着老师在打代码。
但是自己用c#写了一遍,整个流程清晰了很多,深度广度遍历也都完成了。
(虽然我的广度遍历其实借用了内置的 队列方法queue,也算是学以致用吧)

最后完成了这个图。还是有点开心的,虽然c++跟c#各自花了我两个多小时。

第一部分,结点类

1.储存A B C D E 等的行列的结点
2.储存布尔值,确认是否已经被遍历过了。

 class picNode
    {
   

        public char Data;//为什么用char 因为 char 就在那里// A B C
        public bool isVisited;

        public picNode(char data)
        {
   
            Data = data;
            isVisited = false;
        }

        public void ChangeVisited(bool trueorfalse )
        {
   
            isVisited = trueorfalse;
        }

    }

接口,实现了 很多方法

interface iPic
    {
   
        void AddNode(picNode node);
        void resetNode();//重置搜索

        bool SetValueToMatrixForDirectedGraph(int row, int col, int val);
        bool SetValueToMatrixForUndiretedGraph(int row, int col, int val);

        void printMatrix();

        void depthFirstTraverse(int nodeIndex);
        void breadthFirstTraverse(int nodeIndex);

        void setGDdate();
    }

正式实现这个方法
创建两个数组
一个NodeArray 【 容量为行 】 储存结点Node 看起来 就是 A B C D E F G
一个Array 【容量为 行列】储存内部的方向 0 0 0 1 0 1 1
一个Array 【容量为 行列】储存内部的方向 0 0 0 1 0 1 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 图csharp
{
   
    class myPic: iPic
    {
   
        private int Capacity;
        private int NodeCount;
        private int[] array;//邻接矩阵
        private picNode[] NodeArray;
        Queue<int> GDdata = new Queue<int>();



        private int getValueFromArray(int row,int col)
        {
   
            if 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值