Question #2 What will be the output of the following program if the input is 'progra 1 for each character in the input) { push the character on to the stack 3 4 while (stack is not empty) 5 output + pop the stack 6.
Answers
Answered by
2
Answer:
Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 5, 3, 2, 1 }
Output: True
Following sequence can be performed:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 3, 5, 1, 2 }
Output: False
Answered by
2
Answer:
The output will be false.
Explanation:
Input: pushed = (1, 2, 3, 4, 5), popped = (4, 5, 3, 2, 1)
Output given is true.
Therefore, push(1), push(2), push(3), push(4), pop() -> 4
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Input: pushed = (1, 2, 3, 4, 5), popped = (4, 3, 5, 1, 2).
Hence, the output is false.
#SPJ3
Similar questions