📜  从堆栈中删除所有偶数元素

📅  最后修改于: 2022-05-13 01:57:09.055000             🧑  作者: Mango

从堆栈中删除所有偶数元素

给定一个包含n 个元素的栈,任务是在不影响元素顺序的情况下移除栈中的所有元素。
例子:

方法:

  1. 创建一个临时堆栈temp并开始弹出给定堆栈s的元素。
  2. 对于每个弹出的元素说val ,如果val % 2 == 1则将其推送到temp
  3. 在第 2 步结束时, temp将包含s中的所有奇数元素,但顺序相反。
  4. 现在,要获得原始顺序,从temp中弹出每个元素并将其推送到s

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#include 
#include 
 
using namespace std;
 
// Utility function to print
// the contents of a stack
static void printStack(stack s)
{
    while (!s.empty())
    {
        cout << s.top() << " ";
        s.pop();
    }
}
 
// Function to delete all even
// elements from the stack
static void deleteEven(stack s)
{
    stack 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 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 stack)
    {
        while (!stack.isEmpty())
            System.out.print(stack.pop() + " ");
    }
 
    // Function to delete all even
    // elements from the stack
    static void deleteEven(Stack stack)
    {
        Stack 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 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 stack)
    {
        while (stack.Count != 0)
            Console.Write(stack.Pop() + " ");
    }
 
    // Function to delete all even
    // elements from the stack
    static void deleteEven(Stack stack)
    {
        Stack temp = new Stack();
 
        // 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 stack = new Stack();
        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


输出:
19 29 15

时间复杂度: O(N)
辅助空间: O(N)