My search function is giving segmentation fault in a particular case

my code of function is

bool search(node *head, int key)
{
node *temp = head;
while (temp != NULL)
{
if (head->data == key)
{
return true;
}
head = head->next;
}
return false;
}

and if condition in main function is

cin >> key;
if (search(head, key))
{
cout << “found”;
}
else
{
cout << “not found”;
}

i am getting segmentation fault when i give input as something which is not there in my linked list

here you have confused with temp and head
you are moving head but in while loop condition you are checking temp
hence loop will never end and got segmentation fault

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.