delete middle element of a stack using recursion

Solutions on MaxInterview for delete middle element of a stack using recursion by the best coders in the world

showing results for - "delete middle element of a stack using recursion"
Mohamed-Ali
12 Mar 2017
1#include <bits/stdc++.h>
2
3using namespace std;
4void solve(stack<int>&s,int k)
5{
6   if(k==1)
7   {
8       s.pop();
9       return;
10   }
11   int temp=s.top();
12   s.pop();
13   solve(s,k-1);
14   s.push(temp);
15}
16int main()
17{
18    stack<int>s;
19    int n;
20    cin>>n;
21    for(int i=0;i<n;i++)
22    {
23        int a;
24        cin>>a;
25        s.push(a);
26    }
27    int mid=(s.size()/2)+1;
28    solve(s,mid);
29    while(!s.empty())
30    {
31        cout<<s.top()<<" ";
32        s.pop();
33    }
34    return 0;
35}
36