Append the elements of queue in mirror-inverse order
Last Updated :
21 Nov, 2022
Given a queue Q containing N strings, the task is to restructure the queue to double its size such that the second half represents the mirror image of the first half.
Examples:
Input: Q = {“Hello”, “World”}
Output: {“Hello”, “World”, “World”, “Hello”}
Explanation:
The second half of the output queue is the mirror image of the first half. That is:
“Hello”, “World” | “World”, “Hello”
Input: Q = {“Hi”, “Geeks”}
Output: {“Hi”, “Geeks”, “Geeks”, “Hi”}
Approach: On observing carefully, we can say that the second half of the output queue is the reverse of the first half. That is:
- Store the size of the queue in a variable named size.
- Push the Queue elements into a stack without actually losing the elements. This can be achieved by using emplace().
- Repeat the process until size becomes 0.
- Push the elements of the stack back to the queue.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void showq(queue<string> q)
{
while (!q.empty()) {
cout << q.front() << " " ;
q.pop();
}
}
void mirrorQueue(queue<string>& q)
{
int size = q.size();
stack<string> st;
while (size--) {
string x = q.front();
q.emplace(x);
st.push(x);
q.pop();
}
while (!st.empty()) {
string el = st.top();
q.push(el);
st.pop();
}
}
int main()
{
queue<string> q;
q.push( "Hello" );
q.push( "World" );
mirrorQueue(q);
showq(q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void showq(Queue<String> q)
{
while (!q.isEmpty())
{
System.out.print(q.peek() + " " );
q.remove();
}
}
static void mirrorQueue(Queue<String> q)
{
int size = q.size();
Stack<String> st = new Stack<>();
while (size--> 0 )
{
String x = q.peek();
q.add(x);
st.add(x);
q.remove();
}
while (!st.isEmpty())
{
String el = st.peek();
q.add(el);
st.pop();
}
}
public static void main(String[] args)
{
Queue<String> q =
new linkedList<String>();
q.add( "Hello" );
q.add( "World" );
mirrorQueue(q);
showq(q);
}
}
|
Python3
def showq(q) :
while ( len (q) ! = 0 ) :
print (q[ 0 ] , end = " " )
q.pop( 0 )
def mirrorQueue(q) :
size = len (q)
st = []
while (size > 0 ) :
x = q[ 0 ]
q.append(x)
st.append(x)
q.pop( 0 )
size - = 1
while ( len (st) ! = 0 ) :
el = st[ len (st) - 1 ]
q.append(el)
st.pop()
q = []
q.append( "Hello" )
q.append( "World" )
mirrorQueue(q)
showq(q)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void showq(Queue<String> q)
{
while (q.Count != 0)
{
Console.Write(q.Peek() + " " );
q.Dequeue();
}
}
static void mirrorQueue(Queue<String> q)
{
int size = q.Count;
Stack<String> st = new Stack<String>();
while (size --> 0)
{
String x = q.Peek();
q.Enqueue(x);
st.Push(x);
q.Dequeue();
}
while (st.Count != 0)
{
String el = st.Peek();
q.Enqueue(el);
st.Pop();
}
}
public static void Main(String[] args)
{
Queue<String> q = new Queue<String>();
q.Enqueue( "Hello" );
q.Enqueue( "World" );
mirrorQueue(q);
showq(q);
}
}
|
Javascript
<script>
function showq(q)
{
while (q.length!=0)
{
document.write(q[0] + " " );
q.shift();
}
}
function mirrorQueue(q)
{
size = q.length;
let st = [];
while (size-->0)
{
let x = q[0];
q.push(x);
st.push(x);
q.shift();
}
while (st.length!=0)
{
let el = st[st.length-1];
q.push(el);
st.pop();
}
}
let q=[];
q.push( "Hello" );
q.push( "World" );
mirrorQueue(q);
showq(q);
</script>
|
Output:
Hello World World Hello
Time Complexity: O(N), where N is the size of the queue
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...