algo_079
Problem Statement
Problem Statement
Write an algorithm to reverse a stack using an additional stack.
Algorithm
procedure ReverseStack(S1, S2, N)
begin
for i ← 1 to N do
temp ← pop(S1)
// Move remaining elements to S2
for j ← 1 to N - i do
push(S2, pop(S1))
endfor
// Place the extracted element at the bottom of the current sequence
push(S1, temp)
// Move elements back from S2 to S1
while S2 is not empty do
push(S1, pop(S2))
endwhile
endfor
end procedure1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19