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

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

 Question 6 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
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 -> next != 0) { if(temp->val == value) return 1; temp = temp->next; } return 0; } int main() { int arr[6] = {1, 2, 3, 4, 5, 6}; int n = 6, 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 Found B Not found C Compile time error D Runtime error
Question 6 Explanation:
The condition in the while loop "temp->next == 0", checks if the current element is the last element. If the current element is the last element, the value of the current element is not compared with the value to be searched. So, even though the number 6 is present in the linked list, it will print not found.

 Question 7 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Can binary search be applied on a sorted linked list in O(Logn) time?
 A No B Yes
Question 7 Explanation:
Since linked list doesn't allow random access, binary search cannot be applied on a sorted linked list in O(Logn) time.

 Question 8 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What will be time complexity when binary search is applied on a linked list?
 A O(1) B O(n) C O(n2) D O(n3)
Question 8 Explanation:
The time complexity will be O(n) when binary search is applied on a linked list.

 Question 9 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Consider the following recursive implementation of linear search on a linked list:

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

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

 A 1 B 0 C linear....search(temp, value) D linear....search(temp->next, value)
Question 9 Explanation:
The line "linear....search(temp->next, value)", should be inserted to complete the above code.

 Question 10 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
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(struct Node *temp, int value) { if(temp == 0) return 0; if(temp->val == value) return 1; return linear....search(temp->next,  value); } int main() { int arr[6] = {1, 2, 3, 4, 5, 6}; int n = 6, 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(head->next, 6); if(ans == 1) printf("Found"); else printf("Not found"); return 0; }`
 A Found B Not found C Compile time error D Runtime error
Question 10 Explanation:
Since the element 6 is present in the linked list, the program prints "Found".

There are 10 questions to complete.