Answer to Question #263940 in C for F|\/|=-

Question #263940

WAP to reverse a queue using recursion.


1
Expert's answer
2021-11-10T14:51:06-0500
//	WAP to reverse a queue using recursion. 
void DisplayQ(queue<long int> Queue)
{
    while (!Queue.empty()) 
	{
        cout << Queue.front() << " ";
        Queue.pop();
    }
}
 
void ReverseQ(queue<long int>& q)
{
    if (q.empty())	return;
 
    long long int x = q.front();
    q.pop();
 
    ReverseQ(q);
    q.push(x);
}
 
int main()
{
    queue<long int> Queue;
    Queue.push(1);
    Queue.push(2);
    Queue.push(3);
    Queue.push(4);
    printf("\nOriginal Queue : ");
    DisplayQ(Queue);
    ReverseQ(Queue);
    printf("\nAfter reversing: ");
    DisplayQ(Queue);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS