Output cycle found by DFS
When true is returned I can output the cycle by taking the nodes from the stack.


The stack contains the nodes you still have to visit, so they can not be part of the cycle.

There are at least two options to actually obtain data from a DFS.


Create the DFS tree explicitly; this gives you full information.
If you only want one cycle, you can store a pointer to the visited node's parent (the tree edge) instead of labelling it gray.