Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 图形数据结构C的问题#_C#_Data Structures - Fatal编程技术网

C# 图形数据结构C的问题#

C# 图形数据结构C的问题#,c#,data-structures,C#,Data Structures,我刚刚开始学习如何编程,并且一直在关注这一点,但是我在使用给定的 AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost) 这将导致错误“方法'AddDirectedge'不重载2个参数”。将整数添加为第三个参数也没有帮助,它只会给出另一个错误“argument#:无法从'string'转换为'GraphTest.GraphNode” 我不知道如何解决这个问题,如有任何帮助/见解,将不胜感激。为了便于查看

我刚刚开始学习如何编程,并且一直在关注这一点,但是我在使用给定的

AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
这将导致错误“方法'AddDirectedge'不重载2个参数”。将整数添加为第三个参数也没有帮助,它只会给出另一个错误“argument#:无法从'string'转换为'GraphTest.GraphNode

我不知道如何解决这个问题,如有任何帮助/见解,将不胜感激。为了便于查看,下面显示了graph和graph node类(也可以通过上面的链接获得这些类):

公共类图形节点:节点
{
私人清单费用;
公共GraphNode():base(){}
公共GraphNode(T值):基(值){}
公共GraphNode(T值,节点列表邻居):基(值,邻居){}
新公共节点主义邻居
{
得到
{
if(base.neights==null)
base.neights=新节点列表();
返回基地。邻居;
}
}
公开费用清单
{
得到
{
如果(成本==null)
成本=新列表();
退货成本;
}
}
}

公共类图:IEnumerable
{
私有节点列表节点集;
公共图():此(空){}
公共图(节点列表节点集)
{
if(nodeSet==null)
this.nodeSet=新节点列表();
其他的
this.nodeSet=nodeSet;
}
公共void AddNode(GraphNode节点)
{
//将节点添加到图形中
添加(node);
}
公共void AddNode(T值)
{
//将节点添加到图形中
添加(新图形节点(值));
}
public void adddirectedge(GraphNode-from、GraphNode-to、int-cost)
{
from.neights.Add(to);
from.Costs.Add(成本);
}
public void addUndirectedge(GraphNode-from、GraphNode-to、int-cost)
{
from.neights.Add(to);
from.Costs.Add(成本);
to.neights.Add(from);
至。成本。添加(成本);
}
公共布尔包含(T值)
{
返回nodeSet.FindByValue(value)!=null;
}
公共布尔删除(T值)
{
//首先从节点集中删除节点
GraphNode nodeToRemove=(GraphNode)nodeSet.FindByValue(值);
if(nodeToRemove==null)
//未找到节点
返回false;
//否则,将找到该节点
nodeSet.Remove(nodeToRemove);
//枚举节点集中的每个节点,删除此节点的边
foreach(节点集合中的GraphNode gnode)
{
int index=gnode.neights.IndexOf(nodeToRemove);
如果(索引!=-1)
{
//删除对节点和关联成本的引用
gnode.neights.RemoveAt(索引);
gnode.成本.移除(索引);
}
}
返回true;
}
公共节点列表节点
{
得到
{
返回节点集;
}
}
公共整数计数
{
获取{return nodeSet.Count;}
}
公共IEnumerator GetEnumerator()
{
抛出新的NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
抛出新的NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
抛出新的NotImplementedException();
}
}

很明显,它在抱怨类型不匹配:

您提供的是
string
类型,而不是
GraphNode

相反,您应该将其称为:

web.AddDirectedEdge(new GraphNode("People.aspx"), new GraphNode("Privacy.htm"), 1);
其中,
1
是成本,这是必需的,或

var ppl = new GraphNode("People.aspx");
var prv = new GraphNode("Privacy.htm");
web.AddDirectedEdge(ppl, prv, 1);
如果您希望在代码中进一步重用节点

作为旁注,你似乎犯了一些小错误。在进一步讨论之前,我强烈建议你至少选修C#的基础课程。网络上有很多免费的

 public class Graph<T> : IEnumerable<T>
{
    private NodeList<T> nodeSet;

    public Graph() : this(null) { }
    public Graph(NodeList<T> nodeSet)
    {
        if (nodeSet == null)
            this.nodeSet = new NodeList<T>();
        else
            this.nodeSet = nodeSet;
    }

    public void AddNode(GraphNode<T> node)
    {
        // adds a node to the graph
        nodeSet.Add(node);
    }

    public void AddNode(T value)
    {
        // adds a node to the graph
        nodeSet.Add(new GraphNode<T>(value));
    }

    public void AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
    {
        from.Neighbors.Add(to);
        from.Costs.Add(cost);
    }

    public void AddUndirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
    {
        from.Neighbors.Add(to);
        from.Costs.Add(cost);

        to.Neighbors.Add(from);
        to.Costs.Add(cost);
    }

    public bool Contains(T value)
    {
        return nodeSet.FindByValue(value) != null;
    }

    public bool Remove(T value)
    {
        // first remove the node from the nodeset
        GraphNode<T> nodeToRemove = (GraphNode<T>)nodeSet.FindByValue(value);
        if (nodeToRemove == null)
            // node wasn't found
            return false;

        // otherwise, the node was found
        nodeSet.Remove(nodeToRemove);

        // enumerate through each node in the nodeSet, removing edges to this node
        foreach (GraphNode<T> gnode in nodeSet)
        {
            int index = gnode.Neighbors.IndexOf(nodeToRemove);
            if (index != -1)
            {
                // remove the reference to the node and associated cost
                gnode.Neighbors.RemoveAt(index);
                gnode.Costs.RemoveAt(index);
            }
        }

        return true;
    }

    public NodeList<T> Nodes
    {
        get
        {
            return nodeSet;
        }
    }

    public int Count
    {
        get { return nodeSet.Count; }
    }

    public IEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}
web.AddDirectedEdge(new GraphNode("People.aspx"), new GraphNode("Privacy.htm"), 1);
var ppl = new GraphNode("People.aspx");
var prv = new GraphNode("Privacy.htm");
web.AddDirectedEdge(ppl, prv, 1);