Flip a stack using an additional stack and some variables

Asked

Viewed 1,285 times

1

I’m needing to reverse an A stack using another additional stack B and some variables in pseudo code. For the stack A to be reversed at the end of the algorithm. Someone can help?

  • Is battery B obligatory? Using Bubble Sort nor is it necessary to have another battery.

  • Yes Diego, it is mandatory to use a stack B and after reversing on stack B, use variables to return the elements to stack A.

1 answer

1


When stacking, the last entry becomes the first exit. Knowing this, just remove the stack elements at the same time you add it in another.

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

Stack B;

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

Cell B = 1;

Cell A = 5, 4, 3 (Top)

Cell B = 1, 2;

etc..

  • R.Carvalho, I need to invert to A to B, but I need to invert to A so that it is reversed from A initially. That is, I need the stack A to be reversed using the B and some variables so that when it goes back to A it is totally reversed

  • At the end of this example, Voce will have the stack B reversed to A, now it is necessary to pass B to A using variables, to the point that A is exactly like B now, reversed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.