Vote count: 0
I have the code for both these methods:
public void DepthFirstTraverse(T startID, ref List<GraphNode<T>> visited)
{
LinkedList<T> adj;
Stack<T> toVisit = new Stack<T>();
GraphNode<T> current;
toVisit.Push(startID); //push the first id onto the stack
while (toVisit.Count != 0)
{
current = GetNodeByID(toVisit.Peek());
adj = current.GetAdjList();
visited.Add(current);
foreach (T type in adj)
{
if (!toVisit.Contains(type) && !visited.Contains(GetNodeByID(type)))
{
toVisit.Push(type);
}
}
}
}
public void BreadthFirstTraverse(T startID, ref List<GraphNode<T>> visited)
{
LinkedList<T> adj;
Queue<T> toVisit = new Queue<T>();
GraphNode<T> current;
toVisit.Enqueue(startID);
while (toVisit.Count != 0)
{
//get it off from the list
T currentID = toVisit.Dequeue();
current = GetNodeByID(currentID);
adj = current.GetAdjList();
//add the current to the visited list, so we know where we have been
visited.Add(current);
foreach (T ID in adj)
{
if (!toVisit.Contains(ID) && !visited.Contains(GetNodeByID(ID)))
{
toVisit.Enqueue(ID);
}
}
}
}
However, I'm really struggling to get them to work? I am doing the following:
Console.WriteLine(myGraph.BreadthFirstTraverse('A', ref myGraph));
However, I keep getting an error due to the ref I use. The error message that is displayed states the following:
C# Argument 2: cannot convert from 'ref Graph.Graph<char> to 'ref System.Collections.Generic.List<Graph.GraphNode<char>>'
I know the ref is meant to be of type List? I'm guessing, the only list I have is
public LinkedList<T> GetAdjList()
{
return adjList;
}
Which is in my GraphNode class, should I create a new instance of this? Please could someone help me out. I'm been ripping out my hair for the last so many hours. This is just the final step.
Thank you in advance.
asked 28 secs ago
C# - Executing Depth/Breadth First Traverse Method
Aucun commentaire:
Enregistrer un commentaire