What will be the output of following programs :
void fun1(struct node* head) { for(head == NULL) { return; } fun1(head->next); printf("%d ", head->data); }
And this one !
void fun1(struct node* head) { while(head == NULL) { return; } fun1(head->next); printf("%d ", head->data); }
thanks for your response but lets take a simple example 2->5->8->9->23->0 now what will be the output for both as well as for the following (all 3 code segments).
void fun1(struct node* head) { if(head == NULL) { return; } fun1(head->next); printf("%d ", head->data); }
thank you!
@iarnav Veteran
actually, the first one will be compilation error and second one prints the linked list in reverse order. we can't check for null as a condition inside for without the semicolons. if it was like, for(;head == NULL;) it would work.
for(;head == NULL;)
for(
;head
== NULL;)
I think this can be seen in any compiler.
[[email protected] builds]$
gcc
-
ansi
for.c for.c: In function ‘func1’: for.c:5:18: error: expected ‘;’ before ‘)’ token for (ptr == NULL) ^ for.c:5:18: error: expected expression before ‘)’ token
@ junk_mayavi thanks for correcting me!
@_shashi hope you have noticed. :) @iarnav you're welcome.
@junk_mayavi thanks for the correction :)
thanks @junk_mayavi... you cleared my doubt.. i was thinking both will print linked list in reverse order... thanks.