This is the implementation of DFS using stacks, but when I try to run this it gives an infinite loop at the first vertex. Graph class has a array of nodes as it's attribute. The node class is used for both Vertices and Edges. I've tested whether the graph is read in correctly or not and it has been. I could provide the input file and the readFile method used to generate the graph. I don't know why it gets stuck in an infinite loop here.
public static void dfs(Node start) { //DFS uses Stack data structure Stack s=new Stack(); s.push(start); start.visited=true; System.out.println(start.item); while(!s.isEmpty()) { Node n= (Node)s.peek(); Node child=getUnvisitedChildNode(n); if(child!=null) { child.visited=true; System.out.println(child.item); s.push(child); } else { s.pop(); } } } public static Node getUnvisitedChildNode(Node n){ Node missing_child = new Node(); Node counter = new Node(n.item); while(counter!=null){ if (!counter.visited) missing_child = counter; counter = counter.next; } return missing_child; }