c++ - c++中的多重递归
问题描述
我理解简单的递归,但我很难理解多重递归。有人可以说明此代码多次递归的执行顺序吗?
#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);
}
先感谢您 :)
解决方案
推荐阅读
- c++ - Can a Qt Application close another already launched application?
- java - Postman 中具有 OneToMany 关系的实体的递归 JSON 响应
- python - 下拉“文件资源管理器”菜单是否有 PyQt5 小部件?
- daml - Function for getting today's date?
- git - Atlassian bitbucket 和竹子的区别
- c# - 从 C# 类获取 JSON PropertyName,例如 json 属性的“nameof(class.prop)”?
- flask-sqlalchemy - Alembic sqlalchemy.exc.NoReferencedColumnError:(使用 Flask-sqlalchemy 和 Flask-Migrate)
- android - 我在 Android 中使用新的 Places SDK。AutocompleteSupportFragment 要求我将活动添加到清单中
- node.js - 如何避免“TypeError:popper.js.map 不是有效的 URL”消息
- excel - 如果单元格等于一组值,则删除行