0

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.

  • What is it that you wish to print to console? – Namirna Feb 20 '17 at 20:26
  • 1
    Possible duplicate of [Console.WriteLine and generic List](http://stackoverflow.com/questions/52927/console-writeline-and-generic-list) – Heretic Monkey Feb 20 '17 at 20:26
  • @Namirna I guess logic would say to print x as this is a list and print the list with both DFS/BFS? – ImTheOneWhoCodes Feb 20 '17 at 20:32
  • @MikeMcCaughan Yeah, just looked at that post and didn't find it too useful. – ImTheOneWhoCodes Feb 20 '17 at 20:33
  • That is literally what you're asking. If that's not what you're asking, you need to [edit] your post to make it clearer what you're asking. – Heretic Monkey Feb 20 '17 at 20:35
  • @MikeMcCaughan No don't get me wrong, I understand the for/foreach but it's what I should use I know it has to be x (my list) but my mind goes blank trying to figure out the rest. I appreciate your input. – ImTheOneWhoCodes Feb 20 '17 at 20:37
  • @ImTheOneWhoCodes so you just want to print the values in x after the search method is done? Then it is exactly like in the question linked above except you change list to x – Namirna Feb 20 '17 at 20:39
  • @Namirna I've just tried it, however, nothing seems to appear. I can print the values of X (characters) but if I use either method then I get no output, I believe I am doing something wrong. – ImTheOneWhoCodes Feb 20 '17 at 20:48
  • I don't think your DFS code is even working, it looks like an infinite loop. You never pop anything off of the stack. – MrZander Feb 20 '17 at 22:52
  • @MrZander This is true, I've noticed my `return adjlist();` returns null. How could I combat this? – ImTheOneWhoCodes Feb 21 '17 at 19:55

0 Answers0