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?
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?
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.