我正试图按相反顺序打印tree_nodes
的列表:
当前提示示例:/helloFellow/hello/root / >
期望结果:root/hello/helloFellow / >
如何有效地做到这一点?
// *prints the prompt (e.g. /bar/baz/ >) in every iteration of the main loop
// *show all directories on the path in correct order
void show_prompt(struct tree_node *cwd) {
struct tree_node *parDir = cwd->parent;
struct tree_node *originalDir = cwd;
while (cwd->parent != NULL) {
printf("/");
printf("%s", cwd->parent->string_buffer);
cwd = cwd->parent;
}
cwd = originalDir;
printf("/ >");
}
最佳答案
可以使用递归:
void print_path( struct tree_node * cwd ) {
if ( cwd->parent != NULL )
print_path( cwd->parent );
printf( "%s/", cwd->string_buffer );
}
关于c - C:如何以相反的顺序打印列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35244594/