C++ c+中的多重递归+;
我理解简单的递归,但我很难理解多重递归。有人能举例说明这段代码的多重递归的执行顺序吗C++ c+中的多重递归+;,c++,recursion,C++,Recursion,我理解简单的递归,但我很难理解多重递归。有人能举例说明这段代码的多重递归的执行顺序吗 #include <iostream> using namespace std; struct node{ int data; node * next; node(); node(node* &o):data(o->data){}; node(int a):data(a),next(NULL){}; }; void initialize(node *& he
#include <iostream>
using namespace std;
struct node{
int data;
node * next;
node();
node(node* &o):data(o->data){};
node(int a):data(a),next(NULL){};
};
void initialize(node *& head){
head = new node(0);
node *temp(head);
for(int i = 1; i < 10; i++){
temp->next = new node(i);
temp = temp->next;
}
}
void function(node* head, int index){
if(index >= 3){
cout << "you hit the base case" << endl;
return;
}
else if(index < 3){
cout << "Before first call" << endl;
function(head->next, index+1); //call one
cout << "After he first call" << endl;
function(head->next, index+1); //call two
cout << "After the second call" << endl;
}
}
int main() {
node * head = nullptr;
int index = 1;
initialize(head);
function(head, index);
}
#包括
使用名称空间std;
结构节点{
int数据;
节点*下一步;
node();
节点(node*&o):数据(o->data){};
节点(inta):数据(a),下一个(NULL){};
};
无效初始化(节点*&头){
头=新节点(0);
节点*温度(头部);
对于(int i=1;i<10;i++){
temp->next=新节点(i);
温度=温度->下一步;
}
}
无效函数(节点*头部,整数索引){
如果(索引>=3){
无法在函数()的开头添加输出语句
,这将告诉您head
和index
的当前值。使用IDE的调试器是了解代码如何执行的最佳方法。在那里,您可以逐条执行每个指令。哦,我从来没有想过,这会解决问题。我是在repl上编译的。非常感谢