I’m doing some practise questions. This one needs to reverse a stack without using any other data structures except another stack.
I know I will need a helper function that appends the pop-ed numbers once the original stack is empty.
Can somebody get me started? I’m stuck here
def flip_stack(s):
if not s.is_empty():
temp = s.pop
flip_stack(s)
Thanks!
The Stack class has pop, push and is_empty functions.
Here’s another possibility, using an accumulator and a helper function. I’m only using the methods provided in your
Stackclass, and no other data structures (such as Python’s lists) :Be aware that the original stack will be empty at the end, and the flipped stack is returned.