If the sequence of operations – push (1), push (2), pop, push (1), push (2), pop, pop, pop, push (2), pop are performed on a stack, the sequence of popped out values
(A)
2,2,1,1,2
(B)
2,2,1,2,2
(C)
2,1,2,2,1
(D)
2,1,2,2,2
Answer: (A)
Explanation:
Let’s simulate the sequence of operations on a stack:
Push(1): Stack = [1]
Push(2): Stack = [1, 2]
Pop: Pop 2, Stack = [1]
Push(1): Stack = [1, 1]
Push(2): Stack = [1, 1, 2]
Pop: Pop 2, Stack = [1, 1]
Pop: Pop 1, Stack = [1]
Pop: Pop 1, Stack = []
Push(2): Stack = [2]
Pop: Pop 2, Stack = []
The sequence of popped out values is: 2, 2, 1, 1, 2.
Quiz of this Question
Please comment below if you find anything wrong in the above post