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
- Stack | Set 3 (Reverse a string using stack)
- Sort a stack using a temporary stack
- Stack Permutations (Check if an array is stack permutation of other)
- stack empty() and stack size() in C++ STL
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- 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
- Middle To Up-Down Order traversal of a Binary Tree
- Print the middle nodes of each level of a Binary Tree
- 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
- Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space
- Count the number of pop operations on stack to get each element of the array
- Design a stack which can give maximum frequency element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : manishshaw1