Given a stack with n elements, the task is to remove all the elements of the stack without affecting the order of elements.
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
- 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:
19 29 15
- Delete middle element of a stack
- Print Stack Elements from Bottom to Top
- Check if stack elements are pairwise consecutive
- Sort the given stack elements based on their modulo with K
- Check if the elements of stack are pairwise sorted
- 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
- Stack Permutations (Check if an array is stack permutation of other)
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- 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 odd or even positioned nodes from Circular Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.