Delete all even elements from a stack
Given a stack with n elements, the task is to remove all the elements of the stack without affecting the order of elements.
Examples:
Input : s = 16 <- 15 <- 29 <- 24 <- 19 (TOP)
Output: 19 29 15
19 29 15 is the order of odd elements in which
they will be popped from the given stack.
Input : s = 1 <- 2 <- 3 <- 4 <- 5 (TOP)
Output: 5 3 1
Approach:
- Create a temporary stack temp and start popping the elements of the given stack s.
- For every popped element say val, if val % 2 == 1 then push it to temp.
- At the end of step 2, temp will contain all the odd elements from s but in reverse order.
- Now, to get the original order, pop every element from temp and push it to s.
Below is the implementation of the above approach:
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); } // Tranfer 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); } // Tranfer 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); } } |
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); } // Tranfer 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 |
19 29 15
Recommended Posts:
- Delete middle element of a stack
- Print Stack Elements from Bottom to Top
- Check if the elements of stack are pairwise sorted
- Check if stack elements are pairwise consecutive
- Sort the given stack elements based on their modulo with K
- Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space
- Delete array elements which are smaller than next or become smaller
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Stack Permutations (Check if an array is stack permutation of other)
- Find maximum in stack in O(1) without using additional stack
- Sort a stack using a temporary stack
- Stack | Set 3 (Reverse a string using stack)
- Delete consecutive same words in a sequence
- Delete all the even nodes from a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
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.



