GATE CSE
First time here? Checkout the FAQ!
x
+7 votes
498 views

Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are:

  1. isEmpty (Q) — returns true if the queue is empty, false otherwise.
  2. delete (Q) — deletes the element at the front of the queue and returns its value.
  3. insert (Q, i) — inserts the integer i at the rear of the queue.

Consider the following function:

void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
   i = delete(Q);
   f(Q);
   insert(Q, i);
  }
}

What operation is performed by the above function f ?

  1. Leaves the queue Q unchanged
  2. Reverses the order of the elements in the queue Q
  3. Deletes the element at the front of the queue Q and inserts it at the rear keeping the other elements in the same order
  4. Empties the queue Q
asked in DS by Veteran (19k points)   | 498 views

4 Answers

+11 votes
Best answer

insert() will inserts the value in just reverse order.

answered by Veteran (51.6k points)  
selected by
+5 votes
ans b)
answered by Boss (5.3k points)  
+4 votes
answer will be b.

explanation...

assume a queue of element 1 2 3 4 5...

now as Q is not empty it will delete 1 and 1 will be sored in i and den again f(Q) will be called which contains element 23456...but the trace (activation of inset (Q,1)) remains.it continues till 5 is deleted and again activation is executed by inserting q(5)...to q(1),,,thus reversing the queue
answered by Boss (8.1k points)  
is ny other better approach to understand it ?
0 votes
Answer: B
answered by Veteran (34k points)  
Members at the site
Top Users Feb 2017
  1. Arjun

    4672 Points

  2. Bikram

    4004 Points

  3. Habibkhan

    3738 Points

  4. Aboveallplayer

    2966 Points

  5. sriv_shubham

    2278 Points

  6. Smriti012

    2212 Points

  7. Arnabi

    1814 Points

  8. Debashish Deka

    1788 Points

  9. sh!va

    1444 Points

  10. mcjoshi

    1444 Points

Monthly Topper: Rs. 500 gift card

20,788 questions
25,938 answers
59,531 comments
21,923 users