WAP to reverse a queue using recursion.
// 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);
}
Comments
Leave a comment