The places where you do stack.push() seem to be natural places for the recursive calls. pop() should probably corresponds to returning with false as the result.
But maybe it's more straightforward to convert the recursive dfs()
function into the function you need:
Convert
dfs()
to your target data structure (Graphnode
->Vertex
,Iterator
... ->while(adjacentUnvisitedVertex(...)
etc.). Check if it works withSystem.out.println()
. This might be the hardest part. If you get stuck there, post the output here.Just add a
dest
parameter and the check if the nodes match (returntrue
in this case,false
after the loop otherwise). Make sure you check the result of the recursivedfs()
call in the loop and returntrue
if it has found the element.