From cbad20353214efe28832d66f1c65dccfb56d7846 Mon Sep 17 00:00:00 2001 From: Aayush Pratap Singh <52466865+singhpratapaayush07@users.noreply.github.com> Date: Tue, 11 Oct 2022 07:50:05 +0530 Subject: [PATCH] delete_middle_element_from_stack.cpp Clean and optimized code. Beats 97%. --- .../delete_middle_element_from_stack.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 C-Plus-Plus/delete_middle_element_from_stack.cpp diff --git a/C-Plus-Plus/delete_middle_element_from_stack.cpp b/C-Plus-Plus/delete_middle_element_from_stack.cpp new file mode 100644 index 00000000..13945e80 --- /dev/null +++ b/C-Plus-Plus/delete_middle_element_from_stack.cpp @@ -0,0 +1,42 @@ +// Delete Middle Element From Stack +// Deleting index = (n/2 + 1) = k + +#include +#include +using namespace std; + +// small input +// we can also thing with stack size + +void DeleteMiddleElement(stack &s, int k){ + if(k==1){ + s.pop(); + return; + } + + int val = s.top(); + s.pop(); + + DeleteMiddleElement(s, k-1); + + s.push(val); +} + +int main(){ + stack s; + s.push(3); + s.push(33); + s.push(13); + s.push(30); + s.push(8); + + int k = s.size()/2 + 1; // Middle Element of Stack + + DeleteMiddleElement(s, k); + + int sz = s.size(); + for(int i=0; i