现在的位置: 首页 > 综合 > 正文

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

2011年03月11日 ⁄ 综合 ⁄ 共 3567字 ⁄ 字号 评论关闭
这一段时间偶一直在研究图的写法,这个比链表难多了,肯定有很多错误的地方,希望各位同道给与指点。在此谢过。
图的节点:

using System;

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

              {

抱歉!评论已关闭.