edited by
256 views
2 votes
2 votes

Elements $1,2,3,4,5$ are inserted in a stack in sequence. The elements are printed while popping them out. Which of the following is a wrong print sequence?

  1. $3 \ 5 \ 4 \ 2 \ 1$
  2. $1 \  3 \ 2 \ 4 \ 5$
  3. $5 \ 4 \ 3 \ 2 \ 1$
  4. $2 \ 4 \ 1 \ 5 \ 3$
edited by

1 Answer

Best answer
1 votes
1 votes
Elements $1,2,3,4,5$ are inserted in a stack in sequence.

For option $(A)$: push(1),push(2),push(3),pop,push(4),push(5),pop,pop,pop,pop$\implies3,5,4,2,1$

For option $(B)$: push(1),pop,push(2),push(3),pop,pop,push(4),pop,push(5),pop$\implies1,3,2,4,5$

For option $(C)$: push(1),push(2),push(3),push(4),push(5),pop,pop,pop,pop,pop$\implies5,4,3,2,1$

For option $(D)$: push(1),push(2),pop,push(3),push(4),pop after that we required $1$,but we can pop only $3$.And if we push(5),then we get $5$.

So, $2,4,1,5,3$ is not got at any operation we performed.

So,the correct answer is $(D)$.
selected by
Answer:

Related questions

0 votes
0 votes
1 answer
2
Bikram asked Nov 26, 2016
1,637 views
Three algorithms do the same task. Algorithm One is $O(N)$ and Algorithm Two is $O(\log N)$ and Algorithm Three is $O(N1/2)$. Which algorithm should execute the fastest f...
1 votes
1 votes
3 answers
3
0 votes
0 votes
0 answers
4