The Gateway to Computer Science Excellence
+2 votes
210 views

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);
 }



 

in Programming by (287 points) | 210 views
+1
second program will print the data of link list in reverse order.
0

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!

+1
output of data of link list in reverse order.

for the list mentioned by you the o/p will be- 0, 23, 9, 8, 5, 2
+2

@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.

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
0

junk_mayavi thanks for correcting me!

0

@_shashi hope you have noticed. :) @iarnav you're welcome.

+1

@junk_mayavi thanks for the correction :)

0

thanks @junk_mayavi... you cleared my doubt.. i was thinking both will print linked list in reverse order... thanks.

Please log in or register to answer this question.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,492 answers
195,439 comments
100,706 users