error in the code
I have edited the code, you have to pass stack using reference , refer to this code