Working with stack (pushing and popping) using a depth first search