# Data Structure Questions and Answers-Largest and Smallest Number 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 find the largest and smallest number in a linked list?
 A Recursion B Iteration C Both Recursion and iteration D None of the mentioned

Question 1 Explanation:
Both recursion and iteration can be used to find the largest and smallest number in a linked list.

 Question 2
Consider the following code snippet to find the largest element in a linked list:

struct Node{ int val; struct Node *next; }*head; int get....max() { struct Node* temp = head->next; 	 int max....num = temp->val; 	 while(.....) 	 { 	 if(temp->val > max....num) 		 max....num = temp->val; 		temp = temp->next; 	 } 	 return max....num; }

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

 A temp->next != 0 B temp != 0 C head->next != 0 D head != 0

Question 2 Explanation:
The line "temp != 0" should be inserted to complete the above code.

 Question 3
Consider the following code snippet to find the smallest element in a linked list:

struct Node { int val; struct Node* next; }*head; int get....min() { struct Node* temp = head->next; 	 int min....num = temp->val; 	 while(temp != 0) 	 { 	 if(.....) 		 min....num = temp->val; 		temp = temp->next; 	 } 	 return min....num; }

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

 A temp > min....num B val > min....min C temp->val < min....num D temp->val > min....num

Question 3 Explanation:
The line "temp->val = min....num" should be inserted to complete the above code.

 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 get....max() { struct Node* temp = head->next; 	 int max....num = temp->val; 	 while(temp != 0) 	 { 	 if(temp->val > max....num) 		 max....num = temp->val; 		temp = head->next; 	 } 	 return max....num; } int main() { int n = 9,  arr[9] ={5, 1, 3, 4, 5, 2, 3, 3, 1}, i; struct Node *temp,  *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int max....num = get....max(); printf("%d %d", max....num); return 0; }
 A 5 B 1 C runtime error D garbage value

Question 4 Explanation:
The variable temp will always point to the first element in the linked list due to the line "temp = head->next" in the while loop. So, it will be an infinite while loop and the program will produce a runtime error.

 Question 5
What is the output of the following code?

#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node* next; }*head; int get....max() { struct Node* temp = head->next; 	 int max....num = temp->val; 	 while(temp != 0) 	 { 	 if(temp->val > max....num) 		 max....num = temp->val; 		temp = temp->next; 	 } 	 return max....num; } int get....min() { struct Node* temp = head->next; 	 int min....num = temp->val; 	 while(temp != 0) 	 { 	 if(temp->val < min....num) 		 min....num = temp->val; 		temp = temp->next; 	 } 	 return min....num; } int main() { int i,  n = 9,  arr[9] ={8, 3, 3, 4, 5, 2, 5, 6, 7}; struct Node *temp,  *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head -> next =0; temp = head; for(i=0;i<n;i++) { newNode =(struct Node*)malloc(sizeof(struct Node)); newNode->next = 0; newNode->val = arr[i]; temp->next =newNode; temp = temp->next; } int max....num = get....max(); int min....num = get....min(); printf("%d %d", max....num, min....num); return 0; }
 A 2 2 B 8 8 C 2 8 D 8 2