Reversing a Queue using another Queue
Given a queue. The task is to reverse the queue using another empty queue.
Examples:
Input: queue[] = {1, 2, 3, 4, 5}
Output: 5 4 3 2 1
Input: queue[] = {10, 20, 30, 40}
Output: 40 30 20 10
Approach:
- Given a queue and an empty queue.
- The last element of the queue should be the first element of the new queue.
- To get the last element there is a need to pop the queue one by one and add it to the end of the queue, size – 1 times.
- So after that, we will get the last element in front of the queue. Now pop that element out and add it to the new queue. Repeat the steps s – 1 times where s is the original size of the queue.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
queue< int > reverse(queue< int > q)
{
int s = q.size();
queue< int > ans;
for ( int i = 0; i < s; i++) {
for ( int j = 0; j < q.size() - 1; j++) {
int x = q.front();
q.pop();
q.push(x);
}
ans.push(q.front());
q.pop();
}
return ans;
}
int main()
{
queue< int > q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
q = reverse(q);
while (!q.empty()) {
cout << q.front() << " " ;
q.pop();
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Queue<Integer> reverse(Queue<Integer> q)
{
int s = q.size();
Queue<Integer> ans = new LinkedList<>();
for ( int i = 0 ; i < s; i++)
{
for ( int j = 0 ; j < q.size() - 1 ; j++)
{
int x = q.peek();
q.remove();
q.add(x);
}
ans.add(q.peek());
q.remove();
}
return ans;
}
public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();
q.add( 1 );
q.add( 2 );
q.add( 3 );
q.add( 4 );
q.add( 5 );
q = reverse(q);
while (!q.isEmpty())
{
System.out.print(q.peek() + " " );
q.remove();
}
}
}
|
Python3
from collections import deque
def reverse(q):
s = len (q)
ans = deque()
for i in range (s):
for j in range (s - 1 ):
x = q.popleft()
q.appendleft(x)
ans.appendleft(q.popleft())
return ans
q = deque()
q.append( 1 )
q.append( 2 )
q.append( 3 )
q.append( 4 )
q.append( 5 )
q = reverse(q)
while ( len (q) > 0 ):
print (q.popleft(), end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Queue< int > reverse(Queue< int > q)
{
int s = q.Count;
Queue< int > ans = new Queue< int >();
for ( int i = 0; i < s; i++)
{
for ( int j = 0; j < q.Count - 1; j++)
{
int x = q.Peek();
q.Dequeue();
q.Enqueue(x);
}
ans.Enqueue(q.Peek());
q.Dequeue();
}
return ans;
}
public static void Main(String[] args)
{
Queue< int > q = new Queue< int >();
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
q.Enqueue(5);
q = reverse(q);
while (q.Count!=0)
{
Console.Write(q.Peek() + " " );
q.Dequeue();
}
}
}
|
Javascript
<script>
function reverse(q)
{
let s = q.length;
let ans = [];
for (let i = 0; i < s; i++)
{
for (let j = 0; j < q.length - 1; j++)
{
let x = q.shift();
q.push(x);
}
ans.push(q[0]);
q.shift();
}
return ans;
}
let q = [];
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
q = reverse(q);
while (q.length != 0)
{
document.write(q[0] + " " );
q.shift();
}
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n)
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...