Reverse stack

void rev_stack(Stack <Integer> s)
{
     if( !s.isEmpty() ) return;
     int temp = s.pop();
     rev_stack(s);
     insert_at_bottom(s, temp);
}

public void insert_at_bottom(Stack <Integer> s, int data)
{    
     if (!s.isEmpty()){
          s.push(data);
          return;
     }
     int temp1=s.pop();
     insert_at_bottom(s);
     s.push(temp1);
}

Leave a comment