这一段时间偶一直在研究图的写法,这个比链表难多了,肯定有很多错误的地方,希望各位同道给与指点。在此谢过。
图的节点:
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
{
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;
}
}
}
}
这是我写的图的邻接矩阵存储方式。大家多多指正错误哈。
我们都是新手希望大虾们不要笑我哈,只是想认真学习一种思想。