The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
326 views
struct node{
    int num;
    struct node *next;
}

void print(struct node *ptr)
{
    if(ptr)
    {
        printf("%d",ptr->data);
        do{
            printf("%d",ptr->data);
        }
        while(ptr->next);
    }
}

What is the output, if a $SLL : 1\rightarrow 2\rightarrow 3\rightarrow 4\rightarrow 5$ is passed in the above $C$ code ?

  • Assume Head pointer at Node $1$ 
asked in DS by Veteran (50.5k points)
edited by | 326 views
0
something is missing in ques here & if not then

if(p) --> compilaton error [p not declared]

so i assume if(ptr) ...

now the while condition is always true. so o/p is 1 111... infinite loop
0

P

That was a typo. Corrected now !!

0
11111..... Ifnite loop
0
Yes ptr is not increased so prints only 1 in each iteration
0
ptr is never increased rt??
0
@Prajwal

When I m running on my compiler, I am getting different output.
0

@Kapil 

since, while(ptr.next); // ptr is not increased so just prints node 1 infinetly
if, while(ptr=ptr.next); // Then first node twice and all subsequent nodes once

What you are getting as o/p?

0
I will tell the output afterwards :P
+1

Kapil ... To access the structure are you sure that procedure is correct?

AFAIK, to access structure members either (*ptr).data or ptr->data should be correct syntax

0
@prajwal

That was a mistake. check now !!

And I am getting the output as 1234554321.
0
Not getting any logic why you are getting that output! :(
0
I am getting as 11111..........
0
I think inside the do while it should be only print not printf as it is doing recursion rt???
0
Even if you take it that way then "%d" is not proper paramater. It is infinite loop.
0
need a line

ptr=ptr->next;

3 Answers

+1 vote
111111......
answered by Boss (30k points)
0 votes
I am getting output 1155
answered by Active (2.9k points)
0 votes
Output : 1 1 1 1 1........

reason : after enetring into while loop the "condition" always become true it is not making progress it is just checking whether next of node1 is their or not which implies true because it is their so it will print 1 "infinite" time.
answered by Junior (841 points)

Related questions

0 votes
0 answers
6
asked Nov 4 in DS by srestha Veteran (103k points) | 164 views


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

44,275 questions
49,766 answers
164,262 comments
65,852 users