Skip to content
Related Articles

Related Articles

Algorithms Quiz | SP Contest 3 | Question 4
  • Last Updated : 25 Jul, 2018

What does the below function do in general?

filter_none

edit
close

play_arrow

link
brightness_4
code

void fun(Queue *Q)
{
    Stack S; // Say it creates an empty stack S
  
    // Run while Q is not empty
    while (!isEmpty(Q))
    {
        // deQueue an item from Q and
        // push the dequeued item to S
        push(&S, deQueue(Q));
    }
  
    // Run while Stack S is not empty
    while (!isEmpty(&S))
    {
    // Pop an item from S and enqueue
    // the poppped item to Q
    enQueue(Q, pop(&S));
    }
}

chevron_right


(A) Removes the last element from the queue, Q
(B) Keeps the queue, Q same as it was before the call
(C) Makes queue Q empty
(D) Reverses the queue Q


Answer: (D)

Explanation: The function takes a queue Q as an argument. It dequeues all items of Q and pushes them to a stack S. Then pops all items of S and enqueues the items back to Q. Since stack is LIFO order, all items of the queue are reversed.

Quiz of this Question
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up
Recommended Articles
Page :