只是试图让一些涉及指针、函数和递归的代码工作:
Node * root = malloc(sizeof(Node));
root->data = "1";
root->next = NULL;
root->child = NULL;
Node * G = NULL;
BuildGraph(root, &G);
printf("Root is: %d\n", root->data);
Print(G, ">>"); // Custom Print function
和构建图:
void BuildGraph(Node * head, Node ** G) {
if (head->child == NULL) { // No child
printf("Head has no child! %d\n", head->data);
Push(head, &G);
Print(G, ">>");
return;
}
BuildGraph(head->child, &G);
return;
}
所以当我运行程序时,我的输出是这样的:
Head has no child! 1 // printf in BuildGraph
G: 1>> // Print(G, ">>") in BuildGraph
Root is: 1
G is empty! // Print(G, ">>") in main
任何人都知道 G 没有进入 main 的原因吗?
谢谢。