296 views
0 votes
0 votes

Consider the following code to reverse the elements of the Queue

  1.  

  1.   temp=dequeue(Queue);
    enqueue(temp,Queue);
    Reverse(Queue);
  2.   Reverse(dequeue(Queue));
    temp=dequeue(Queue);
    enqueue(temp,Queue);
  3.   temp=dequeue(Queue);
    Reverse(Queue);
    enqueue(temp,Queue);
  4.   None of above

1 Answer

Best answer
0 votes
0 votes

                                                  Recursive algorithm for reversing the queue 

1. Pop the element from the queue if it is not empty, otherwise, return empty queue

2. call reverse(queue) for remaining queue

3. push the popped element in the queue 


 

    1     2     3     4

after reversing we get,

    4     3     2     1

temp = dequeue(Queue)

Reverse(Queue)

enqueue(temp , Queue)

selected by

Related questions

0 votes
0 votes
0 answers
1
abhishek1995_cse asked Dec 9, 2018
596 views
is hashing there in gate 2019 syllabus?
0 votes
0 votes
1 answer
3
0 votes
0 votes
1 answer
4
Nitesh Choudhary asked Apr 22, 2017
3,447 views
i want to read Tanenbaum Datastructure book. Can anyone tell me the source of pdf Tanenbaum Datastructure book ?