Recursive thought and its Application continued (29)
Today we continue to learn about recursion. Let's first review the process of function calls: after the program runs, there is a special memory area for function calls. So what's the use of this memory area? 1, used to save the arguments in the function, local variables, temporary variables, etc.; 2, from the starting address to grow in one direction (such as: high address-> low address); 3, there is a dedicated "pointer" to identify the "top" of the currently used memory.
Then the stack area in the program is a special special memory area. As shown in the following figure
Let's take a look at an example: printing even nodes in a single linked list in reverse order. As shown below
Let's take a look at how the specific source code is implemented, as follows
# include # include # include "DTString.h" # include "LinkList.h" using namespace std;using namespace DTLib;struct Node {int value; Node* next;}; Node* create_list (int v, int len) {Node* ret = NULL; Node* slider = NULL; for (int iodized 0; ivalue = vested cargo; n-> next = NULL; if (slider = = NULL) {slider = n; ret = n } else {slider- > next = n; slider = n;} return ret;} void destory_list (Node* list) {while (list) {Node* del = list; list = list- > next; delete del;}} void print_list (Node* list) {while (list) {cout value next } cout value% 2) = = 0) {cout value 8, go to step 3
-- end: line 8 has room for the queen.
Let's take a look at the specific source code implementation, as follows
# include # include # include "DTString.h" # include "LinkList.h" using namespace std;using namespace DTLib;template
< int SIZE >Class QueueSolution: public Object {protected: enum {N = SIZE + 2}; struct Pos: public Object {Pos (int px = 0, int py = 0): X (px), y (py) {} int x; int y;}; int mchessboard [N] [N]; Pos m_direction [3]; LinkList solution; int m_count Void init () {m_count = 0; for (int iTuno; I)