One very depressing thing, I already wrote it. Not only did the webpage jump away, but it was written in white.
Let's just talk about it.
Stack has the "advanced and later" principle, while nested functions also have the following principle: '"Then call and return first"
This is in line with the stack entry and exit.
Here we will analyze the problems of the Hanoi Tower. (If you don't want to draw a picture, you need to read the book that views the data structure of the graph)
// The following is an algorithm.
******************* **/
# Include <stdio. h>
Void Hanoi (int n, char X, char y, char Z)
{// Move the N discs on the tower X in diameter ranging from small to large and numbered 1 to n from top to bottom
// On the tower Z, the y tower can be used as an auxiliary move () // The moving function.
If (n = 1)
{
Move (x, 1, Z); // when n = 1, move the disc on X to Z.
}
Else
{
Hanoi (n-1, X, Z, Y); // move the disc numbered 1 to n-1 on X to Y, and use Z as the auxiliary tower.
Move (x, N, Z); // execute the move operation
Hanoi (n-1, Y, x, z); // move the disc numbered 1 to n-1 on the Y tower to Z, and X serves as the auxiliary tower.
}
}
Main ()
{
Int N, A, B, C;
...........
...........
Hanoi (n, A, B, C );
}
// The following is the analysis.
******************* **/
# Include <stdio. h>
Void Hanoi (int n, char a, char B, char C)
{If (n = 1)
{
Move (x, 1, Z); // when n = 1, move the disc on X to Z.
}
Else
{
Hanoi (2, A, B, C); // 2 layer push (6, 2, a, B, c) ====>>>>
{
Void Hanoi (INT 2, char a, char B, char C)
{
If (n = 1) Move (A, 1, C );//
Else
{
/************* Layer 3 push (8, 1, A, C, B) **************/
Hanoi (1, A, C, B );
==================================>>>>>>>
Void Hanoi (INT 1, char a, char C, char B)
{
If (n = 1) Move (A, 1, B) // conforms to n = 1, and is not executed after the if statement is executed.
} // After execution, two layers are returned
/************ POP (8, 1, A, C, B) **********************/
Move (A, 2, c); // move 2nd Disks
/*************** Go to Layer 3 push (8, 1, B, A, C) ******************/
Hanoi (1, B, A, C)
======================================>>>
Void Hanoi (INT 1, char B, char a, char C)
{
If (n = 1) Move (B, 1, C); // The following is not executed
}
/*********** Returns two layers of POP (8, 1, B, A, C) ******************/
}
}
} // Return to layer 1, pop (6, 2, A, B, C) of the stack)
Move (x, 3, Z); // execute the move operation
Hanoi (2, B, A, C); // The operation is the same as that before entering Layer 2. I will not write it here
}
}
Main ()
{
Int N, A, B, C;
...........
...........
Hanoi (n, a, B, c); // Layer 1 Push (, A, B, C );
}
The program is not difficult. The main idea is thinking.
Here I will fill out the program. We do not have a compiler at hand, so we are sorry for the poor layout ~
# Include <stdio. h>
Void Hanoi (int n, char X, char y, char Z );
Void move (char one, char three );
Void move (char one, char Three)
{
Printf ("% C ---> % C/N", one, three );
}
Void Hanoi (int n, char X, char y, char Z)
{
If (n = 1) Move (x, z );
Else
{
Hanoi (n-1, X, Z, y );
Move (x, z );
Hanoi (n-1, Y, x, z );
}
}
Main ()
{
Int N;
Printf ("input N:");/* n disks */
Scanf ("% d", & N );
Hanoi (n, a, B, c);/* Move n disks on A to C by B */
Return 0;
}
After a real understanding of the stack, I have a clear understanding of the relationship between the stack and functions.