• edited by
24,917 views
52 52 votes

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

  1. $\text{isEmpty (Q)}$ — returns true if the queue is empty, false otherwise.
  2. $\text{delete (Q)}$ — deletes the element at the front of the queue and returns its value.
  3. $\text{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$

11 Answers

Best answer
57 57 votes

 

$insert()$ will inserts the values in reverse order.

Correct Answer: $B-$ Reverses the order of the elements in the queue $Q.$

• edited by
10 10 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
6 6 votes

Answer should be B because Deletion operation is performed until all elements are deleted and any insert function not invoked until all deletion operation completed when it is completed function returns and insert operation is called then element pushed into the queue in this way we get reverse of the elements of the queue Q 

option B is correct

0 0 votes
In this recursion, queue is being deleted by one element every time and getting saved in i. And again the function calls itself. When queue becomes empty, the last element will be inserted first in the queue. This will be for all the elements present. Thus, it reverses the order of elements in the queue.
Answer:
Position:
Show:

Related questions

258 258 votes
18 answers 18 answers
61.8k
61.8k views
Ishrat Jahan asked Oct 29, 2014
61,774 views
When searching for the key value $60$ in a binary search tree, nodes containing the key values $10, 20, 40, 50, 70, 80, 90$ are traversed, not necessarily in the order gi...
104 104 votes
13 answers 13 answers
47.0k
47.0k views
Ishrat Jahan asked Oct 29, 2014
46,969 views
Consider a hash function that distributes keys uniformly. The hash table size is $20$. After hashing of how many keys will the probability that any new key hashed collide...
34 34 votes
2 answers 2 answers
11.5k
11.5k views
Ishrat Jahan asked Oct 29, 2014
11,485 views
Consider the following C program: #include <stdio.h #define EOF -1 void push (int); /* push the argument on the stack */ int pop (void); /* pop the top of the stack */ vo...
61 61 votes
5 answers 5 answers
35.5k
35.5k views
Ishrat Jahan asked Oct 30, 2014
35,497 views
Consider the $B^{+}$ tree in the adjoining figure, where each node has at most two keys and three links.Keys $K15$ and then $K25$ are inserted into this tree in that orde...