# Data Structure Questions and Answers-Search an Element in a Linked List using Recursion

## Click on any option to know the CORRECT ANSWERS

 Question 1
Which of the following methods can be used to search an element in a linked list?
 A Iterative linear search B Iterative binary search C Recursive binary search D All of the mentioned

Question 1 Explanation:
Iterative linear search can be used to search an element in a linked list. Binary search can be used only when the list is sorted.

 Question 2
Consider the following code snippet to search an element in a linked list:

struct Node { int val; struct Node* next; }*head; int linear....search(int value) { struct Node *temp = head->next; while(temp != 0) { if(temp->val == value) return 1; .....; } return 0; }

Which of the following lines should be inserted to complete the above code?

 A temp = next B temp->next = temp C temp = temp->next D return 0

Question 2 Explanation:
The line "temp = temp->next" should be inserted to complete the above code.

 Question 3
What does the following code do?

#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int linear....search(int value) { struct Node *temp = head->next; while(temp != 0) { if(temp->val == value) return 1; temp = temp->next; } return 0; } int main() { int arr = {1, 2, 3, 4, 5}; int n = 5, i; head = (struct Node*)malloc(sizeof(struct Node)); head->next = 0; struct Node *temp; temp = head; for(i=0; i<n; i++) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next = newNode; temp = temp->next; } int ans = linear....search(60); if(ans == 1) printf("Found"); else printf("Not found"); return 0; }
 A Finds the index of the first occurrence of a number in a linked list B Finds the index of the last occurrence of a number in a linked list C Checks if a number is present in a linked list D None of the mentioned

Question 3 Explanation:
The above code checks if a number is present in a linked list.

 Question 4
What is the output of the following code?

#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int linear....search(int value) { struct Node *temp = head->next; while(temp != 0) { if(temp->val == value) return 1; temp = temp->next; } return 0; } int main() { int arr = {1, 2, 3, 4, 5}; int n = 5, i; head = (struct Node*)malloc(sizeof(struct Node)); head->next = 0; struct Node *temp; temp = head; for(i=0; i<n; i++) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next = newNode; temp = temp->next; } int ans = linear....search(-1); if(ans == 1) printf("Found"); else printf("Not found"); return 0; }
 A Found B Not found C Compile time error D Runtime error

Question 4 Explanation: