Answers for "delete middle element of a stack using recursion"

C++
0

delete middle element of a stack using recursion

#include <bits/stdc++.h>

using namespace std;
void solve(stack<int>&s,int k)
{
   if(k==1)
   {
       s.pop();
       return;
   }
   int temp=s.top();
   s.pop();
   solve(s,k-1);
   s.push(temp);
}
int main()
{
    stack<int>s;
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        s.push(a);
    }
    int mid=(s.size()/2)+1;
    solve(s,mid);
    while(!s.empty())
    {
        cout<<s.top()<<" ";
        s.pop();
    }
    return 0;
}
Posted by: Guest on September-22-2021

Code answers related to "delete middle element of a stack using recursion"

Browse Popular Code Answers by Language