in DS edited by
16,270 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$
in DS edited by
16.3k views

4 Comments

I think the program will never come to execute insert(Q, i) operation because the program recurse before it and as it become empty the isEmpty() becomes true and if section becomes false and it should not enter that setion and terminates.

Kindly tell me where I am wrong.
2
2
i also have the same doubt, initially it goes on deleting the elements from the queue and then the loop condition becomes false. So how are we executing the insert(q,i) statement...
1
1
@Anand Mundhe

According to me,

implementing using arrays : call by reference is default.

implementing using linked list : you are sending a pointer of the queue, which is also call by reference.

So I think we don’t have to worry about call by value here
0
0

11 Answers

41 votes
41 votes
Best answer

 

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

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

edited by

4 Comments

any reference ? I donot think call by reference or call by value will make any problem here.
1
1
The above answer will be alright if we consider it to be global so that all invocations will be able to make changes to the same Queue. Have you considered it to be global???
0
0
Arrays  in C are passes as reference so it shouldn't be a problem. Ofcourse I'm making an assumption that Queue is implemented using array.
0
0
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

4 Comments

is ny other better approach to understand it ?
0
0
why it is saved... it is not declared as static and default everything is in activation record please explain this point .
0
0
Call by value.....
–1
–1
But it is clearly mentioned that
insert(Q,i) — inserts the integer i at the rear of the queue.
Hence we have to insert the elements at rear , like 5 will be inserted at rear , then 4 , then 3 , then 2 then 1
Hence correct option should be option A .

But it is also a fact that , in the question , it is no where mentioned that how rear will be decreamented after each function ends .
0
0
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

1 comment

Only happens if call by reference..... can u see & anywhere?
0
0
3 votes
3 votes
ans b)

1 comment

Wrong
–2
–2
Answer:

Related questions