jeudi 14 janvier 2021

How to write stack for a recursive function having two recursive functions?

I have been stuck on a problem having two recursive functions in it. I could not understand the mechanism of loop and stack behind it. This are the lines of code of my program.

    #include<iostream>
     using namespace std;
     int test(int num )
     {   
          if(num!=0 )
          {
            num=num-1;
            test(num);
            cout<<num<<" ";
            test(num);
          }
     }
     int main()
     {
         test(3);
     }

This is the output of the program

  0 1 0 2 0 1 0

Can someone explain me the output of this program using stack?

Aucun commentaire:

Enregistrer un commentaire