599 views
0 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 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
Position:
Show:

Related questions

0 0 votes
0 0 answers
904
904 views
abhishek1995_cse asked Dec 9, 2018
904 views
is hashing there in gate 2019 syllabus?
1 1 vote
2 answers 2 answers
2.7k
2.7k views
Prince Sindhiya asked Aug 11, 2018
2,727 views
We are provided with an undirected connected graph such that weight of all the edges is equal to some constant k. We wish to find the shortest distance between given pair...
0 0 votes
1 1 answer
1.1k
1.1k views
Na462 asked May 20, 2018
1,079 views
A min heap having $1024$ distinct elements with keys ranging from $0$ to $1023$ is stored in array of $1024$ indices. The maximum difference between $(n/2)^{th}$ element ...
0 0 votes
1 1 answer
4.0k
4.0k views
Nitesh Choudhary asked Apr 22, 2017
4,019 views
i want to read Tanenbaum Datastructure book. Can anyone tell me the source of pdf Tanenbaum Datastructure book ?