recategorized
4,642 views
1 votes
1 votes

Consider the following statements :

$S_{1}$ : A queue can be implemented using two stacks.

$S_{2}$ : A stack can be implemented using two queues.

Which of the following is correct ?

  1. $S_{1}$ is correct and $S_{2}$ is not correct.
  2. $S_{1}$ is not correct and $S_{2}$ is correct.
  3. Both $S_{1}$ and $S_{2}$ are correct.
  4. Both $S_{1}$ and $S_{2}$ are not correct.
recategorized

1 Answer

2 votes
2 votes

Both of the statements are true.Lets understand one by one.In both the cases , we are making enqueue simpler and dequeue operation costlier.

Explanation of statement 1:

Let us have 2 stacks S1 and S2 for  our purpose.For enqueue operation , we simply push in S1.For dequeue operation , we have 2 cases :

a) S2 is not empty : In that case ,we simply pop from this stack.

b) S2 is empty : In that case , first we pop all elements from S1 and push into S2.After pushing all the elements from S1 to S2 we pop the topmost element from S2.

Explanation of statement 2:

For this we take 2 queues , say Qand Q2. Now ,

For push operation of stack , we simply enqueue into that queue which is not empty.

For pop operation , in any case , we do the following :

a) Dequeue all elements from the queue which is non empty and enqueue into the empty queue.

b) Then dequeue the front element from the queue where enqueue has been performed recently.

Hence, both statements S1 and S2 are true.Hence C is the correct option.

Related questions

0 votes
0 votes
2 answers
1
makhdoom ghaya asked Sep 26, 2016
5,615 views
Given the following prefix expression :$^{*} + 3 + 3 ↑ 3 + 3 3 3$What is the value of the prefix expression ?$2178$ $2199$$2205$ $2232$
0 votes
0 votes
1 answer
2
makhdoom ghaya asked Sep 26, 2016
9,095 views
The runtime for traversing all the nodes of a binary search tree with $n$ nodes and printing them in an order is$O(\lg n)$$O(n \lg n)$ $O(n)$$O(n^{2})$
3 votes
3 votes
2 answers
4
go_editor asked Aug 16, 2016
3,736 views
Which of the following is not an inherent application of stack?Implementation of recursionEvaluation of a postfix expressionJob schedulingReverse a string