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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- stack empty() and stack size() in C++ STL
- Sort a stack using a temporary stack
- Stack | Set 3 (Reverse a string using stack)
- Middle To Up-Down Order traversal of a Binary Tree
- Find middle of singly linked list Recursively
- Traverse Linked List from middle to left-right order using recursion
- String formed with middle character of every right substring followed by left sequentially
- Delete elements in C++ STL 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.
Improved By : manishshaw1