What is wrong in my implementation of DFS?

I have learnt DFS and tried to implement it. Here is my code -Ideone
The expected output should be 1 2 4 3 but my program displays 1 3 4 2.
Please help me where I have went wrong.


1 3 4 2 is also a correct DFS ordering for that graph. The difference is due to different ordering. For eg: https://ideone.com/3gGU9L this outputs as you wanted

1 Like

Thanks! I recently learnt it so I thought that just like a learner I might have made a mistake. Thanks again!