I am struggling to print both my DepthFirstTraverse/BreadthFirstTraverse methods to the console. I'm guessing I require a for/foreach
loop:
Here are my methods:
public void DepthFirstTraverse(T startID, ref List<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.ID);
foreach (T type in adj)
{
if (!toVisit.Contains(type) && !visited.Contains((type)))
{
toVisit.Push(type);
}
}
}
}
public void BreadthFirstTraverse(T startID, ref List<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.ID);
foreach (T ID in adj)
{
if (!toVisit.Contains(ID) && !visited.Contains((ID)))
{
toVisit.Enqueue(ID);
}
}
}
}
}
In my Program class I have created an instance of a list:
List<char> x = new List<char>();
And this is calling the method:
myGraph.DepthFirstTraverse('A', ref x);
But I don't really have an idea on how use these methods to print out the result(s) which are so desired. Could someone possibly help me?
Thank you.
Everything works, no issues. Just literally I'm not too sure on how to go about printing to console.