+ 9
OUTPUT: RECURSION STARTED! + 1 PUSHED TO STACK (A) TOP + RECURSIVE CALL FROM STACK (A) + 2 PUSHED TO STACK (A) TOP + RECURSIVE CALL FROM STACK (A) + 3 PUSHED TO STACK (A) TOP + RECURSIVE CALL FROM STACK (A) >> \0 triggered BY STACK (A)! ! STACK (A) MIRRORED 3 TO STACK (B) ! RECURSIVE CALL FROM STACK (B) >> \0 triggered BY STACK (B)! ! STACK (B) POPPED 3 [OUT] ! STACKS (A) STATE 12 STACKS (B) STATE (EMPTY) ! STACK (A) MIRRORED 2 TO STACK (B) ! RECURSIVE CALL FROM STACK (B) + 3 PUSHED TO STACK (A) TOP + RECURSIVE CALL FROM STACK (A) >> \0 triggered BY STACK (A)! ! STACK (A) MIRRORED 3 TO STACK (B) ! RECURSIVE CALL FROM STACK (B) >> \0 triggered BY STACK (B)! ! STACK (B) POPPED 3 [OUT] ! STACKS (A) STATE 12 STACKS (B) STATE 2 ! STACK (B) POPPED 2 [OUT] ! STACKS (A) STATE 1 STACKS (B) STATE (EMPTY) ! STACK (A) MIRRORED 1 TO STACK (B) ! RECURSIVE CALL FROM STACK (B) + 2 PUSHED TO STACK (A) TOP + RECURSIVE CALL FROM STACK (A) + 3 PUSHED TO STACK (A) TOP + RECURSIVE CALL FROM STACK (A) >> \0 triggered BY STACK (A)! ! STACK (A) MIRRORED 3 TO STACK (B) ! RECURSIVE CALL FROM STACK (B) >> \0 triggered BY STACK (B)! ! STACK (B) POPPED 3 [OUT] ! STACKS (A) STATE 12 STACKS (B) STATE 1 ! STACK (A) MIRRORED 2 TO STACK (B) ! RECURSIVE CALL FROM STACK (B) + 3 PUSHED TO STACK (A) TOP + RECURSIVE CALL FROM STACK (A) >> \0 triggered BY STACK (A)! ! STACK (A) MIRRORED 3 TO STACK (B) ! RECURSIVE CALL FROM STACK (B) >> \0 triggered BY STACK (B)! ! STACK (B) POPPED 3 [OUT] ! STACKS (A) STATE 12 STACKS (B) STATE 12 ! STACK (B) POPPED 2 [OUT] ! STACKS (A) STATE 1 STACKS (B) STATE 1 ! STACK (B) POPPED 1 [OUT] ! STACKS (A) STATE (EMPTY) STACKS (B) STATE (EMPTY)
22nd Feb 2018, 6:41 AM
Babak
Babak - avatar
+ 8
Dear Kenji, Before any explanation by me, I strongly recommend you to take your time and visit the following link which comprehensively illustrates the whole process. [http://cryptroix.com/2016/10/10/understanding-multiple-recursion/] I heavily tweaked the code in order to log each step of the process to console. #include <cstdio> #include <string> #include <iostream> std::string gStack_A = ""; // STACK A std::string gStack_B = ""; // STACK B void abc(char *s, bool A = false, bool B = false); void abc(char *s, bool A, bool B) { if(A) printf("\nRECURSIVE CALL FROM STACK (A)"); else if(B) printf("\nRECURSIVE CALL FROM STACK (B)"); else printf("\nRECURSION STARTED!\n"); if (s[0] == '\0') { // poping condition if(A) printf("\n>> \\0 triggered BY STACK (A)!"); else printf("\n>> \\0 triggered BY STACK (B)!"); return; } gStack_A += s[0]; printf("\n+ %c PUSHED TO STACK (A) TOP +", s[0]); abc(s+1, A = true, B = false); // STACK A CALLER printf("\n! STACK (A) MIRRORED %c TO STACK (B) !", s[0]); gStack_B += s[0]; abc(s+1, A = false, B = true); // STACK B CALLER printf("\n! STACK (B) POPPED %c [OUT] !", s[0]); // What you see in the output. gStack_A.resize(gStack_A.length() - 1); gStack_B.resize(gStack_B.length() - 1); std::cout << "\n\nSTACKS (A) STATE " << (gStack_A != "" ? gStack_A : "(EMPTY)"); std::cout << "\nSTACKS (B) STATE " << (gStack_B != "" ? gStack_B : "(EMPTY)"); printf("\n"); } // poping point int main() { abc("123"); } CODE : [https://code.sololearn.com/cy7qWXQtFoY0]
22nd Feb 2018, 6:41 AM
Babak
Babak - avatar
+ 2
Are you asking for the output or for the output explanation? The output is 3323321. If you don't understand the output I recommended you to debug step by step, you can use a C online compiler.
21st Feb 2018, 2:29 PM
J Domingo Jiménez J
J Domingo Jiménez J - avatar