The Gateway to Computer Science Excellence
0 votes
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 by Boss (35.7k points) | 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)); 
    struct Node *temp = *head_ref; 
    ptr1->data = data; 
    ptr1->next = *head_ref; 
  
    if (*head_ref != NULL) 
    { 
        while (temp->next != *head_ref) 
            temp = temp->next; 
        temp->next = ptr1; 
    } 
    else
        ptr1->next = ptr1; 
  
    *head_ref = ptr1; 
} 
  
void printList(struct Node *head) 
{ 
    struct Node *temp = head; 
    if (head != NULL) 
    { 
        do
        { 
            printf("%d ", temp->data); 
            temp = temp->next; 
        } 
        while (temp != head); 
    } 
} 
  
int main() 
{ 
    struct Node *head = NULL; 
  
    push(&head, 3); 
    push(&head, 5); 
    push(&head, 12); 
    push(&head, 18); 
  
   printf ("%d ", find(head, 9));
  
    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

Please reply,  what's the answer to the final conclusion.

0

 your answer is correct 

thanks 

Please log in or register to answer this question.

Related questions

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,708 users