Delete all even elements from a stack
Last Updated :
18 Aug, 2021
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++
#include <stack>
#include <iostream>
#include <stdio.h>
using namespace std;
static void printStack(stack< int > s)
{
while (!s.empty())
{
cout << s.top() << " " ;
s.pop();
}
}
static void deleteEven(stack< int > s)
{
stack< int > temp;
while (!s.empty())
{
int val = s.top();
s.pop();
if (val % 2 == 1)
temp.push(val);
}
while (!temp.empty())
{
s.push(temp.top());
temp.pop();
}
printStack(s);
}
int main()
{
stack< int > s;
s.push(16);
s.push(15);
s.push(29);
s.push(24);
s.push(19);
deleteEven(s);
return 0;
}
|
Java
import java.util.Stack;
class GFG {
static void printStack(Stack<Integer> stack)
{
while (!stack.isEmpty())
System.out.print(stack.pop() + " " );
}
static void deleteEven(Stack<Integer> stack)
{
Stack<Integer> temp = new Stack<>();
while (!stack.isEmpty()) {
int val = stack.pop();
if (val % 2 == 1 )
temp.push(val);
}
while (!temp.isEmpty())
stack.push(temp.pop());
printStack(stack);
}
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
def printStack(s):
while ( len (s)! = 0 ):
print (s.pop(),end = " " )
def deleteEven(s):
temp = []
while ( len (s)! = 0 ):
val = s.pop()
if (val % 2 = = 1 ):
temp.append(val)
while ( len (temp)! = 0 ):
s.append(temp.pop())
printStack(s);
s = []
s.append( 16 )
s.append( 15 )
s.append( 29 )
s.append( 24 )
s.append( 19 )
deleteEven(s)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printStack(Stack< int > stack)
{
while (stack.Count != 0)
Console.Write(stack.Pop() + " " );
}
static void deleteEven(Stack< int > stack)
{
Stack< int > temp = new Stack< int >();
while (stack.Count != 0)
{
int val = stack.Pop();
if (val % 2 == 1)
temp.Push(val);
}
while (temp.Count != 0)
stack.Push(temp.Pop());
printStack(stack);
}
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);
}
}
|
Javascript
<script>
function printStack(s)
{
while (s.length!=0)
{
document.write( s[s.length-1] + " " );
s.pop();
}
}
function deleteEven(s)
{
var temp = [];
while (s.length!=0)
{
var val = s[s.length-1];
s.pop();
if (val % 2 == 1)
temp.push(val);
}
while (temp.length!=0)
{
s.push(temp[temp.length-1]);
temp.pop();
}
printStack(s);
}
var s = [];
s.push(16);
s.push(15);
s.push(29);
s.push(24);
s.push(19);
deleteEven(s);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...