c#数据结构————图的邻接矩阵储存

这一段时间偶一直在研究图的写法,这个比链表难多了,肯定有很多错误的地方,希望各位同道给与指点。在此谢过。
图的节点:

using System;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

namespace structure

{

     public class Node

     {

         private int num;//编号

         private string data;//数据

         internal NodesCollection nodes;//节点的集合

         private object parant;

         public Node()

         {

              nodes = new NodesCollection(this);

         }

         public string Data

         {

              get

              {

                   return data;

              }

              set

              {

                   data = value;

              }

         }

         public int Num

         {

              get

              {

                   return num;

              }

              set 

              {

                   num = value;

              }

         }

         public virtual NodesCollection Nodes

         {

              get

              {

                   return this.nodes;

              }

         }

         public object Parant

         {

              get

              {

                   return parant;

              }

              set 

              {

                   parant = value;

              }

         }

     }

}

 在图中,是节点的集合,所以有了节点的集合类:

using System;

using System.Collections;

namespace structure

{

     public class NodesCollection:CollectionBase

     {

         private object self;

         public NodesCollection()

         {

         }

         public NodesCollection(object parent)

         {

              this.self = parent;

         }

         public Node this[int index]

         {

              get

              {

                   return (Node) base.List[index];

              }

         }

         public object Parent

         {

              get

              {

                   return this.self;

              }

              set

              {

                   this.self = value;

              }

         }

         private void InitNodeCollection(Node node1)

         {

              node1.Parant = this.Parent;

         }

         protected override void OnSet(int index, object oldValue, object newValue)

         {

              this.InitNodeCollection((Node) newValue);

              base.OnSet(index, oldValue, newValue);

         }

         public void Add(Node item)

         {

              base.List.Add(item);

         }

         public void AddAt(int index, Node item)

         {

              base.List.Insert(index, item);

         }

         public bool Contains(Node item)

         {

              return base.List.Contains(item);

         }

         public int IndexOf(Node item)

         {

              return base.List.IndexOf(item);

         }

         public void Remove(Node item)

         {

              base.List.Remove(item);

         }

     }

}

再说边了,

using System;

namespace structure

{

     public class Edge

     {

         private Node snode;//起点

         private Node enode;//终点

         private int svalue;//权值

         internal EdgesCollection edges;//边的集合

         private object parant;

         public Edge()

         {

              snode = new Node();

              enode = new Node();

              edges = new EdgesCollection(this);

         }

         public Node Snode

         {

              get

              {

                   return snode;

              }

              set

              {

                   snode = value;

              }

         }

         public Node Enode

         {

              get

              {

                   return enode;

              }

              set 

              {

                   enode = value;

              }

         }

         public int Svalue

         {

              get

              {

                   return svalue;

              }

              set 

              {

                   svalue = value;

              }

         }

         public virtual EdgesCollection Edges

         {

              get

              {

                   return this.edges;

              }

         }

         public object Parant

         {

              get

              {

                   return parant;

              }

              set 

              {

                   parant = value;

              }

         }

     }

}

边的集合类:

using System;

using System.Collections;

namespace structure

{

     public class EdgesCollection:CollectionBase

     {

         private object self;

         public EdgesCollection()

         {

         }

         public EdgesCollection(object parent)

         {

              this.self = parent;

         }

         public Edge this[int index]

         {

              get

              {

                   return (Edge) base.List[index];

              }

         }

         public object Parent

         {

              get

              {

                   return this.self;

              }

              set

              {

                   this.self = value;

              }

         }

         private void InitNodeCollection(Edge node1)

         {

              node1.Parant = this.Parent;

         }

         protected override void OnSet(int index, object oldValue, object newValue)

         {

              this.InitNodeCollection((Edge) newValue);

              base.OnSet(index, oldValue, newValue);

         }

         public void Add(Edge item)

         {

              base.List.Add(item);

         }

         public void AddAt(int index, Edge item)

         {

              base.List.Insert(index, item);

         }

         public bool Contains(Edge item)

         {

              return base.List.Contains(item);

         }

         public int IndexOf(Edge item)

         {

              return base.List.IndexOf(item);

         }

         public void Remove(Edge item)

         {

              base.List.Remove(item);

         }

     }

}

图的创建:

using System;

namespace structure

{

     public class Graphic

     {

         private Node node;//

         private Edge edge;//

         public GraphKind kind;

         public Graphic()

         {

              node = new Node();

              edge = new Edge();

         }

         public enum GraphKind

         {

              DG,//有向图

              DN,//有向网

              AG,//无向图

              AN//无向网

         }

         public GraphKind Kind

         {

              get

              {

                   return this.kind;

              }

              set

              {

                   this.kind = value;

              }

         }

         public Node Node

         {

              get

              {

                   return this.node;

              }

              set

              {

                   this.node = value;

              }

         }

         public Edge Edge

         {

              get

              {

                   return this.edge;

              }

              set

              {

                   this.edge = value;

              }

         }

         public void CreateGraph(int n,int e)//n为顶点数,e为边数

         {

              int i,j,k,w;

              object b,t;

              Graphic Graphic1 = new Graphic();

              Console.Write("\n顶点数n和边数e");

              for(i=0;i<n;i++)

              {

                   Console.Write("\t"+i+"个顶点的信息:");

                   Node node1 = new Node();

                   node1.Num = i;

                   node1.Data = i.ToString();

                   Graphic1.Node.Nodes.Add(node1);

              }

              for(i=0;i<n;i++)

                   for(j=0;j<n;j++)

                   {

                       Edge edge1 = new Edge();

                       edge1.Snode = Graphic1.Node.Nodes[i];

                       edge1.Enode = Graphic1.Node.Nodes[j];

                       edge1.Svalue = 0;

                       Graphic1.Edge.Edges.Add(edge1);

                   }

              for(k=0;k<e;k++)

              {

                   Console.Write(""+k+"条边=>\n\t起点:");

                   b = Console.ReadLine();

                   Console.Write("终点");

                   t = Console.ReadLine();

                   Console.Write("权值");

                   w = int.Parse(Console.ReadLine());

                   i=0;

                   while(i<n && Graphic1.Node.Nodes[i].Data!=b.ToString())

                       i++;

                   if(i>n)

                   {

                       Console.Write("输入起点不存在");

                       Console.Error.Close();

                   }

                   j=0;

                   while(j<n && Graphic1.Node.Nodes[j].Data!=t.ToString())

                       j++;

                   if(j>n)

                   {

                       Console.Write("输入起点不存在");

                       Console.Error.Close();

                   }

                   Graphic1.Edge.Edges[k].Svalue =w;

              }

         }

     }

}

这是我写的图的邻接矩阵存储方式。大家多多指正错误哈。
我们都是新手希望大虾们不要笑我哈,只是想认真学习一种思想。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值