printf - C: how to print a list in reverse order? -
i trying print list of tree_nodes in reverse order:
current prompt example: /hellofellow/hello/root / >
desired result: root/hello/hellofellow / >
what way efficiently?
// *prints prompt (e.g. /bar/baz/ >) in every iteration of main loop // *show directories on 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("/ >"); }
you use recursion:
void print_path( struct tree_node * cwd ) { if ( cwd->parent != null ) print_path( cwd->parent ); printf( "%s/", cwd->string_buffer ); }
Comments
Post a Comment