Reverse a stack using an additional stack and some variables

1

I need to invert a stack A using another stack B and some variables in pseudo-code. So that stack A is inverted at the end of the algorithm. Can anyone help?

    
asked by anonymous 10.05.2016 / 15:22

1 answer

1

When stacking, the last element to enter becomes the first to exit. Knowing this, just remove the elements from the stack while adding it to another.

Stack A = 5, 4, 3, 2, 1 (Top)

Stack B;

Stack A = 5, 4, 3, 2 (Top)

Stack B = 1;

Stack A = 5, 4, 3 (Top)

Stack B = 1, 2;

etc.

    
10.05.2016 / 15:33