Q. Reverse the order of the elements on a stack S
(i) by using two additional stacks
(ii) by using one additional queue.
Ans: Let S be the stack having n number of elements. Now we need to reverse the elements of S (i) using the two additional stack S1 and S2
while not empty (S)
{ C=pop(S);
push(S1,C);
}
While not empty(S1)
{ C=pop(S1);
push(S2,C);
}
While not empty(S3)
{ C=pop(S3);
push(S,C);
}
(ii) using one additional queue Q
while not empty (S)
{ C=pop(S);
enque(Q,C);
}
While not empty(Q)
{ C=deque(Q);
push(S,C);
}