簡體   English   中英

圖數據結構 C# 的問題

[英]Trouble with Graph Data Structure C#

我剛剛開始學習如何編程,並一直在學習有關圖形的教程,但我在使用給定的方法時遇到了一些問題

AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)

例如,使用教程中的默認設置無法構建

web.AddDirectedEdge("People.aspx", "Privacy.htm");  // People -> Privacy

這給出了錯誤“方法 'AddDirectedEdge' 沒有重載需要 2 個參數” 添加一個整數作為第三個參數也無濟於事,它只會給出另一個錯誤“Argument#: cannot convert from 'string' to 'GraphTest.GraphNode< string>'”

我不知道如何解決這個問題,任何有關它的幫助/見解將不勝感激。 為了便於查看圖形和圖形節點類如下所示(這些都可以通過上面的鏈接獲得):

 public class GraphNode<T> : Node<T>
{
    private List<int> costs;

    public GraphNode() : base() { }
    public GraphNode(T value) : base(value) { }
    public GraphNode(T value, NodeList<T> neighbors) : base(value, neighbors) { }

    new public NodeList<T> Neighbors
    {
        get
        {
            if (base.Neighbors == null)
                base.Neighbors = new NodeList<T>();

            return base.Neighbors;
        }
    }

    public List<int> Costs
    {
        get
        {
            if (costs == null)
                costs = new List<int>();

            return costs;
        }
    }
}

.

 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();
    }
}

好吧,顯然它在抱怨類型不匹配:

您提供的是string類型而不是GraphNode<T>

相反,您應該將其稱為:

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# 的基礎課程。 網絡上有很多免費提供,例如

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM