in DS edited by
1,743 views
1 vote
1 vote

​​​​​Let $\mathrm{S} 1$ and $\mathrm{S} 2$ be two stacks. $\mathrm{S} 1$ has capacity of $4$ elements. $\mathrm{S} 2$ has capacity of $2$ elements. $\text{S1}$ already has $4$ elements: $100,200,300$, and $400$, whereas $\text{S2}$ is empty, as shown below.

400 (Top)
300
200
100

Stack $\text{S1}$

Stack $\text{S2}$

Only the following three operations are available:
 

PushToS2: Pop the top element from S1 and push it on S2.
PushToS1: Pop the top element from S2 and push it on S1.
GenerateOutput: Pop the top element from S1 and output it to the user.

Note that the pop operation is not allowed on an empty stack and the push operation is not allowed on a full stack.

Which of the following output sequences can be generated by using the above operations?

  1. $100,200,400,300$
  2. $200,300,400,100$
  3. $400,200,100,300$
  4. $300,200,400,100$
in DS edited by
by
1.7k views

1 comment

Note that size of stack $S_2$ is only $2,$ Not $4.$
0
0

1 Answer

0 votes
0 votes
It is given that the size of $S_1$ stack is $4$, size of $S_2$ stack is $2$. we can push and pop at any time but for printing the element is only from the stack $S_1$.

 Option B):$200,300,400,100$

 Here first Pop two items $400,300$ form $S_1$ and push it into $S_2$

$S_1=200,100; S_2=300,400$ from top to bottom. Print $200$ as output.

Pop $300$ from $S_2$ and push it into $S_1$, Print $300$. Now $S_1=100,S_2=400$

Pop $400$ form $S_2$ and push it back into $S_1$. Print 400,100.

In this way, we can generate $200, 300,400,100$ as output.

Option C) $400,200,100,300$

First print $400$ which top most element in $S_1.$

Pop $300$ from $S_1$ and push it into $S_2$.

Now print $200,100$ from $S_1$

Pop $300$ from $S_2$ and push it into $S_1$. print $300$

In this way, we can generate $400,200,100,300$

Option C) $300,200,400,100$

Pop $400$ from $S_1$ and push into $S_2$. Print $300,200$

Pop $400$ from $S_2$ and push it into $S_1$.

So $S_1=400,100$. Now print $400,100$

In this way, we can generate $300,200,400,100$

Option A) can not be generated to print $100$ we have to first pop $400,300,200$ which is not possible because the size of $S_2$ is two.

Option $(B,C,D)$ are correct.
Answer:

Related questions