Dada una cola Q que contiene N strings, la tarea es reestructurar la cola para duplicar su tamaño de modo que la segunda mitad represente la imagen especular de la primera mitad.
Ejemplos:
Entrada: Q = {“Hola”, “Mundo”}
Salida: {“Hola”, “Mundo”, “Mundo”, “Hola”}
Explicación:
La segunda mitad de la cola de salida es la imagen reflejada de la primera mitad. Es decir:
“Hola”, “Mundo” | “Mundo”, “Hola”
Entrada: Q = {“Hola”, “Geeks”}
Salida: {“Hola”, “Geeks”, “Geeks”, “Hola”}
Planteamiento: Al observar detenidamente, podemos decir que la segunda mitad de la cola de salida es la inversa de la primera mitad. Eso es:
- Almacene el tamaño de la cola en una variable denominada tamaño .
- Empuje los elementos de la cola en una pila sin perder realmente los elementos. Esto se puede lograr usando emplace() .
- Repita el proceso hasta que el tamaño sea 0.
- Empuje los elementos de la pila de vuelta a la cola.
A continuación se muestra la implementación del enfoque anterior.
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(); } } // Function 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; }
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<String> 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<String> q) { int size = q.size(); // Defining a stack Stack<String> 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<String> q = new inkedList<String>(); q.add("Hello"); q.add("World"); mirrorQueue(q); showq(q); } } // This code is contributed by gauravrajput1
Python3
# Python3 program to arrange the # elements of the queue # to the end such that # the halves are mirror # order of each other # Function to display # the elements of # the queue def showq(q) : # Iterating through the queue # and printing the elements while (len(q) != 0) : print(q[0] , end = " ") q.pop(0) # Function to produce mirror elements def mirrorQueue(q) : size = len(q) # Defining a stack st = [] # Pushing the elements # of a queue # in a stack without # losing them # from the queue while (size > 0) : x = q[0] # Push the element # to the end of the # queue q.append(x) # Push the element # into the stack st.append(x) # Remove the element q.pop(0) size -= 1 # Appending the elements # from the stack # to the queue 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) # This code is contributed by divyeshrabadiya07
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<String> 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<String> q) { int size = q.Count; // Defining a stack Stack<String> st = new Stack<String>(); // 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<String> q = new Queue<String>(); q.Enqueue("Hello"); q.Enqueue("World"); mirrorQueue(q); showq(q); } } // This code is contributed by Rajput-Ji
Javascript
<script> // JavaScript program to arrange the // elements of the queue // to the end such that // the halves are mirror // order of each other // Function to display // the elements of // the queue function showq(q) { // Iterating through the queue // and printing the elements while (q.length!=0) { document.write(q[0] + " "); q.shift(); } } // Function to produce mirror elements function mirrorQueue(q) { size = q.length; // Defining a stack let st = []; // Pushing the elements // of a queue // in a stack without // losing them // from the queue while (size-->0) { let x = q[0]; // Push the element // to the end of the // queue q.push(x); // Push the element // into the stack st.push(x); // Remove the element q.shift(); } // Appending the elements // from the stack // to the queue while (st.length!=0) { let el = st[st.length-1]; q.push(el); st.pop(); } } // Driver Code let q=[]; q.push("Hello"); q.push("World"); mirrorQueue(q); showq(q); // This code is contributed by patel2127 </script>
Hello World World Hello
Complejidad de tiempo: O(N) , donde N es el tamaño de la cola
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por yashbeersingh42 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA