Reverse a stack without any other data structure !!

Yes, the question is simple. Reverse a stack without using any other any other data structure.
But this one is really a very good question. You need to think a lot to answer this correctly. If you hear this question for the first time in the interview, then i am pretty sure that if you are not a extraordinarily good, you can’t answer this question. So, here is how the solution goes.

Make two functions:-
1. The first function reverse is to pick the top element from the current stack.
2. The second function is to insert the element a to the last of the current stack.
So at first we put the 2nd element at last position. then the third element, and so on…
Thus finally we get a reversed stack. Hope the code helps you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s