Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete_middle_element_from_stack.cpp #846

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions C-Plus-Plus/delete_middle_element_from_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Delete Middle Element From Stack
// Deleting index = (n/2 + 1) = k

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

// small input
// we can also thing with stack size

void DeleteMiddleElement(stack<int> &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<int> 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<sz; i++){
cout<<s.top()<<" ";
s.pop();
}
}