I know the answer to my question is already posted in the chegg archives, But I
ID: 3882786 • Letter: I
Question
I know the answer to my question is already posted in the chegg archives, But I am trying to get a detailed answer explaining the process thanks ..
(Psuedo Code required)
A) Use the operations push, pop, peek and empty to construct an operation which sets i to the bottom element of the stack, leaving the stack unchanged. (hint: use an auxiliary stack.)
B) Use the operations push, pop, peek and empty to construct an operation which sets i to the third element from the bottom of the stack. The stack may be left unchanged
Explanation / Answer
A)
h = pop (s);
i = pop (s);
push (s, i);
push (s, h);
B)
(STACK s1, s2)
while (!empty (s1))
{
h = pop (s1);
push (s2, h);
}
for (elem = 1; elem <= 3; elem++)
i = pop (s2);
/* i will exit loop with value of 3rd element from bottom of s1, the original stack */