edited by
16,110 views
41 votes
41 votes

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$
edited by

11 Answers

Best answer
41 votes
41 votes

 

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

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

edited by
9 votes
9 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 votes
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

Answer:

Related questions

62 votes
62 votes
11 answers
2
Ishrat Jahan asked Oct 29, 2014
28,941 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...