137 views

int find (struct node * first, int n)
{
while (first data ! = n)
first = first — next;
if (first data = = n)
return(1);
else
return (-1);


in the above code segment if the value of 'n' is 5, then the function return 1, but if the value of 'n' is 9, then what does it do ?

in DS | 137 views
0
goes to infinite loop ???
+1

This is the code.

#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node *next;
};

int find (struct Node* first, int n)
{
while (first->data != n)
first = first->next;
if (first->data == n)
return(1);
else
return (-1);
}

void push(struct Node **head_ref, int data)
{
struct Node *ptr1 = (struct Node *)malloc(sizeof(struct Node));
ptr1->data = data;

{
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;

}

{
{
do
{
printf("%d ", temp->data);
temp = temp->next;
}
}
}

int main()
{

return 0;
} 

Check this out.

+1

from the image, we have to understand that it is a circular linked list.

So, Due to circular linked list and given code, if you are searching a element which is not present in the list, then it will lead to INFINITE LOOP

0
Ans will be D)

na?

because iff it is not found, then will go to infinite loop
0
already questioner supplies the value 9 to search ===> option C is correct
0
oh, I have not seen

thanks
0

It is not cased that it always will go in infinite loop.it also depend on the implementation of the algorithm.But here it will go in the infinite loop.

0

@kumar.dilip brother, that's why i commented as

Due to circular linked list and given code,

0

@kumar.dilip

It is not cased that it always will go in infinite loop.it also depend on the implementation of the algorithm.But here it will go in the infinite loop.

Can u explain what u mean by this?

0

0

thanks