# 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:

- Store the size of 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**become 0. - 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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to display ` `// the elements of ` `// the queue ` `void` `showq(queue<string> q) ` `{ ` ` ` `// Iterating through the queue ` ` ` `// and printing the elements ` ` ` `while` `(!q.empty()) { ` ` ` `cout << q.front() << ` `" "` `; ` ` ` `q.pop(); ` ` ` `} ` `} ` ` ` `// Fuction to produce mirror elements ` `void` `mirrorQueue(queue<string>& q) ` `{ ` ` ` `int` `size = q.size(); ` ` ` ` ` `// Defining a stack ` ` ` `stack<string> 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<string> q; ` ` ` `q.push(` `"Hello"` `); ` ` ` `q.push(` `"World"` `); ` ` ` `mirrorQueue(q); ` ` ` `showq(q); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Hello World World Hello

**Time Complexity:** *O(N)*, where N is the size of the queue.

## Recommended Posts:

- Level order traversal in spiral form | Using one stack and one queue
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Reversing the first K elements of a Queue
- Check if Queue Elements are pairwise consecutive | Set-2
- Check if Queue Elements are pairwise consecutive
- Permute the elements of an array following given order
- Maximum sum by picking elements from two arrays in order
- Maximum sum by picking elements from two arrays in order | Set 2
- Rearrange array such that difference of adjacent elements is in descending order
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- Sort array such that absolute difference of adjacent elements is in increasing order
- Flatten binary tree in order of post-order traversal
- Reversing a Queue using another Queue
- Insertion in n-ary tree in given order and Level order traversal
- Form N-copy string with add, remove and append operations
- Perform append, update, delete and range sum queries on the given array
- Queue using Stacks
- Reversing a Queue
- Queue in Python

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.