# Append the elements of queue in mirror-inverse order

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:

1. Store the size of the queue in a variable named size.
2. Push the Queue elements into a stack without actually losing the elements. This can be achieved by using emplace().
3. Repeat the process until size becomes 0.
4. Push the elements of the stack back to the queue.

Below is the implementation of the above approach.

## C++

 `// C++ program to arrange the` `// elements of the queue` `// to the end such that` `// the halves are mirror` `// order of each other` `#include ` `using` `namespace` `std;`   `// Function to display` `// the elements of` `// the queue` `void` `showq(queue q)` `{` `    ``// Iterating through the queue` `    ``// and printing the elements` `    ``while` `(!q.empty()) {` `        ``cout << q.front() << ``" "``;` `        ``q.pop();` `    ``}` `}`   `// Function to produce mirror elements` `void` `mirrorQueue(queue& q)` `{` `    ``int` `size = q.size();`   `    ``// Defining a stack` `    ``stack st;`   `    ``// Pushing the elements` `    ``// of a queue` `    ``// in a stack without` `    ``// losing them` `    ``// from the queue` `    ``while` `(size--) {` `        ``string x = q.front();` `        ``// Push the element` `        ``// to the end of the` `        ``// queue` `        ``q.emplace(x);` `        ``// Push the element` `        ``// into the stack` `        ``st.push(x);` `        ``// Remove the element` `        ``q.pop();` `    ``}`   `    ``// Appending the elements` `    ``// from the stack` `    ``// to the queue` `    ``while` `(!st.empty()) {` `        ``string el = st.top();` `        ``q.push(el);` `        ``st.pop();` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``queue q;` `    ``q.push(``"Hello"``);` `    ``q.push(``"World"``);` `    ``mirrorQueue(q);` `    ``showq(q);` `    ``return` `0;` `}`

## Java

 `// Java program to arrange the` `// elements of the queue` `// to the end such that` `// the halves are mirror` `// order of each other` `import` `java.util.*;` `class` `GFG{`   `// Function to display` `// the elements of` `// the queue` `static` `void` `showq(Queue q)` `{` `    ``// Iterating through the queue` `    ``// and printing the elements` `    ``while` `(!q.isEmpty()) ` `    ``{` `        ``System.out.print(q.peek() + ``" "``);` `        ``q.remove();` `    ``}` `}`   `// Function to produce mirror elements` `static` `void` `mirrorQueue(Queue q)` `{` `    ``int` `size = q.size();`   `    ``// Defining a stack` `    ``Stack st = ``new` `Stack<>();`   `    ``// Pushing the elements` `    ``// of a queue` `    ``// in a stack without` `    ``// losing them` `    ``// from the queue` `    ``while` `(size-->``0``) ` `    ``{` `        ``String x = q.peek();` `      `  `        ``// Push the element` `        ``// to the end of the` `        ``// queue` `        ``q.add(x);` `      `  `        ``// Push the element` `        ``// into the stack` `        ``st.add(x);` `      `  `        ``// Remove the element` `        ``q.remove();` `    ``}`   `    ``// Appending the elements` `    ``// from the stack` `    ``// to the queue` `    ``while` `(!st.isEmpty()) ` `    ``{` `        ``String el = st.peek();` `        ``q.add(el);` `        ``st.pop();` `    ``}` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``Queue q = ` `          ``new` `inkedList();` `    ``q.add(``"Hello"``);` `    ``q.add(``"World"``);` `    ``mirrorQueue(q);` `    ``showq(q);` `}` `}`   `// This code is contributed by gauravrajput1`

## C#

 `// C# program to arrange the` `// elements of the queue` `// to the end such that` `// the halves are mirror` `// order of each other` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{`   `// Function to display` `// the elements of` `// the queue` `static` `void` `showq(Queue q)` `{` `  ``// Iterating through the queue` `  ``// and printing the elements` `  ``while` `(q.Count != 0) ` `  ``{` `    ``Console.Write(q.Peek() + ``" "``);` `    ``q.Dequeue();` `  ``}` `}`   `// Function to produce mirror elements` `static` `void` `mirrorQueue(Queue q)` `{` `  ``int` `size = q.Count;`   `  ``// Defining a stack` `  ``Stack st = ``new` `Stack();`   `  ``// Pushing the elements` `  ``// of a queue` `  ``// in a stack without` `  ``// losing them` `  ``// from the queue` `  ``while` `(size --> 0) ` `  ``{` `    ``String x = q.Peek();`   `    ``// Push the element` `    ``// to the end of the` `    ``// queue` `    ``q.Enqueue(x);`   `    ``// Push the element` `    ``// into the stack` `    ``st.Push(x);`   `    ``// Remove the element` `    ``q.Dequeue();` `  ``}`   `  ``// Appending the elements` `  ``// from the stack` `  ``// to the queue` `  ``while` `(st.Count != 0) ` `  ``{` `    ``String el = st.Peek();` `    ``q.Enqueue(el);` `    ``st.Pop();` `  ``}` `}`   `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` `  ``Queue q = ``new` `Queue();` `  ``q.Enqueue(``"Hello"``);` `  ``q.Enqueue(``"World"``);` `  ``mirrorQueue(q);` `  ``showq(q);` `}` `}`   `// This code is contributed by Rajput-Ji`

Output:

```Hello World World Hello

```

Time Complexity: O(N), where N is the size of the queue. My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.