305 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$
edited | 305 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;

+1 vote
111111......
I am getting output 1155
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.