GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
138 views

I am getting a) as ans. Plz tell how C?

asked in DS by Veteran (58.6k points)   | 138 views

Initially queue contains elements like 6,8,9,2

I)while statement is true.

II)dequeue from queue and push into stack until queue becomes enpty.

stack values are from top to bottom in order 2,9,8,6.//after completion of first while loop

III)2nd while loop pop one value from stack and enqueue into queue until stack becomes empty.

initially pop top value from stack i.e)2 and enqueue into queue.

after 2nd while loop false contents of queue is 2,9,8,6.

Hence it reverses the elements in queue

yes tnks

2 Answers

0 votes
Yes, answer will be C .
answered by (445 points)  
0 votes

Correct answer is C

Take an example. Queue Q contains 5 elements 10, 20, 30, 40 ,50.

Stack : __ __ __ __ __

Queue : 10 20 30 40 50

When queue is not empty, we are to perform PUSH operation 5 times taking all the 5 elements via dequeue operation.

So we push in sequence, 10, 20, 30 ,40, 50 (Since Queue is FIFO order) ie. elements will be removed in same sequence as they were inserted.

Stack : 10 20 30 40 50

Queue : __ __ __ __ __

Now again we Enqueue the elements one by one taking all the 5 elements via POP operation.

So enqueue in sequence 50 40 30 20 10. Here's the catch. POP removes elements from last. Since stack is LIFO order.

Stack : __ __ __ __ __

Queue : 50 40 30 20 10.

Hence, the contents of Queue is reversed the original content.  So, C is correct answer.

answered by Junior (537 points)  

Related questions

+1 vote
0 answers
1
asked in DS by Lakshman Patel RJIT Junior (757 points)   | 61 views
0 votes
0 answers
3
asked in DS by Bhargav Zantye 1 (21 points)   | 112 views


Top Users Sep 2017
  1. Habibkhan

    7828 Points

  2. Warrior

    2746 Points

  3. rishu_darkshadow

    2692 Points

  4. Arjun

    2672 Points

  5. A_i_$_h

    2426 Points

  6. nikunj

    1980 Points

  7. manu00x

    1920 Points

  8. Bikram

    1854 Points

  9. makhdoom ghaya

    1770 Points

  10. SiddharthMahapatra

    1718 Points


26,239 questions
33,805 answers
80,214 comments
31,159 users