Open In App

Data Structures | Queue | Question 1

Like Article
Like
Save
Share
Report

Following is C like pseudo-code of a function that takes a Queue as an argument, and uses a stack S to do processing. 

C




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 popped item to Q
      enQueue(Q, pop(&S));
    }
}


What does the above function do in general?

(A)

Removes the last from Q

(B)

Keeps the Q same as it was before the call

(C)

Makes Q empty

(D)

Reverses the 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 the stack is LIFO order, all items of the queue are reversed.

Hence option (D) is the correct answer.


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


Last Updated : 08 Jan, 2013
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads