Given a stack with push(), pop(), empty() operations, delete middle of it without using any additional data structure.
Input : Stack = [1, 2, 3, 4, 5] Output : Stack = [1, 2, 4, 5] Input : Stack = [1, 2, 3, 4, 5, 6] Output : Stack = [1, 2, 4, 5, 6]
The idea is to use recursive calls. We first remove all items one by one, then we recur. After recursive calls, we push all items back except the middle item.
7 6 5 3 2 1
- Design a stack with operations on middle element
- Delete all even elements from a stack
- How to delete last element from a set in C++
- How to delete an element from the Set by passing its value in C++
- How to delete last element from a List in C++ STL
- Tracking current Maximum Element in a Stack
- Count the number of pop operations on stack to get each element of the array
- Design a stack which can give maximum frequency element
- Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space
- 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 empty() and stack size() in C++ STL
- Stack | Set 3 (Reverse a string using stack)
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.
Improved By : manishshaw1