Dada una pila con n elementos, la tarea es eliminar todos los elementos de la pila sin afectar el orden de los elementos.
Ejemplos:
Entrada: s = 16 <- 15 <- 29 <- 24 <- 19 (TOP)
Salida: 19 29 15
19 29 15 es el orden de los elementos impares en el
que se extraerán de la pila dada.
Entrada: s = 1 <- 2 <- 3 <- 4 <- 5 (ARRIBA)
Salida: 5 3 1
Acercarse:
- Cree una pila temporal temporal y comience a hacer estallar los elementos de la pila dada .
- Para cada elemento reventado diga val , si val % 2 == 1 entonces empújelo a temp .
- Al final del paso 2, temp contendrá todos los elementos impares de s pero en orden inverso.
- Ahora, para obtener el orden original, saque cada elemento de temp y empújelo a s .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <stack> #include <iostream> #include <stdio.h> using namespace std; // Utility function to print // the contents of a stack static void printStack(stack<int> s) { while (!s.empty()) { cout << s.top() << " "; s.pop(); } } // Function to delete all even // elements from the stack static void deleteEven(stack<int> s) { stack<int> temp; // While stack is not empty while (!s.empty()) { int val = s.top(); s.pop(); // If value is odd then push // it to the temporary stack if (val % 2 == 1) temp.push(val); } // Transfer the contents of the temporary stack // to the original stack in order to get // the original order of the elements while (!temp.empty()) { s.push(temp.top()); temp.pop(); } // Print the modified stack content printStack(s); } // Driver Code int main() { stack<int> s; s.push(16); s.push(15); s.push(29); s.push(24); s.push(19); deleteEven(s); return 0; } // This code is contributed by Vivekkumar Singh
Java
// Java implementation of the approach import java.util.Stack; class GFG { // Utility function to print // the contents of a stack static void printStack(Stack<Integer> stack) { while (!stack.isEmpty()) System.out.print(stack.pop() + " "); } // Function to delete all even // elements from the stack static void deleteEven(Stack<Integer> stack) { Stack<Integer> temp = new Stack<>(); // While stack is not empty while (!stack.isEmpty()) { int val = stack.pop(); // If value is odd then push // it to the temporary stack if (val % 2 == 1) temp.push(val); } // Transfer the contents of the temporary stack // to the original stack in order to get // the original order of the elements while (!temp.isEmpty()) stack.push(temp.pop()); // Print the modified stack content printStack(stack); } // Driver code public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); stack.push(16); stack.push(15); stack.push(29); stack.push(24); stack.push(19); deleteEven(stack); } }
Python3
# Python implementation of the approach # Utility function to print # the contents of a stack def printStack(s): while (len(s)!=0): print(s.pop(),end=" ") # Function to delete all even # elements from the stack def deleteEven(s): temp = [] # While stack is not empty while (len(s)!=0): val=s.pop() # If value is odd then push # it to the temporary stack if (val % 2 == 1): temp.append(val) # Transfer the contents of the temporary stack # to the original stack in order to get # the original order of the elements while (len(temp)!=0): s.append(temp.pop()) # Print the modified stack content printStack(s); # Driver Code s = [] s.append(16) s.append(15) s.append(29) s.append(24) s.append(19) deleteEven(s) # This code is contributed by rag2127.
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Utility function to print // the contents of a stack static void printStack(Stack<int> stack) { while (stack.Count != 0) Console.Write(stack.Pop() + " "); } // Function to delete all even // elements from the stack static void deleteEven(Stack<int> stack) { Stack<int> temp = new Stack<int>(); // While stack is not empty while (stack.Count != 0) { int val = stack.Pop(); // If value is odd then push // it to the temporary stack if (val % 2 == 1) temp.Push(val); } // Transfer the contents of the temporary stack // to the original stack in order to get // the original order of the elements while (temp.Count != 0) stack.Push(temp.Pop()); // Print the modified stack content printStack(stack); } // Driver code public static void Main(String[] args) { Stack<int> stack = new Stack<int>(); stack.Push(16); stack.Push(15); stack.Push(29); stack.Push(24); stack.Push(19); deleteEven(stack); } } // This code has been contributed by 29AjayKumar
Javascript
<script> // JavaScript implementation of the approach // Utility function to print // the contents of a stack function printStack(s) { while (s.length!=0) { document.write( s[s.length-1] + " "); s.pop(); } } // Function to delete all even // elements from the stack function deleteEven(s) { var temp = []; // While stack is not empty while (s.length!=0) { var val = s[s.length-1]; s.pop(); // If value is odd then push // it to the temporary stack if (val % 2 == 1) temp.push(val); } // Transfer the contents of the temporary stack // to the original stack in order to get // the original order of the elements while (temp.length!=0) { s.push(temp[temp.length-1]); temp.pop(); } // Print the modified stack content printStack(s); } // Driver Code var s = []; s.push(16); s.push(15); s.push(29); s.push(24); s.push(19); deleteEven(s); </script>
Producción:
19 29 15
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por 29AjayKumar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA