Data Structure Questions and Answers-Length of a Linked List using Recursion

 

 Help authour, Buy PDF Ebook   >>>Click Here<<<

Data Structure Questions and Answers-Length of a Linked List using Recursion

Click on any option to know the CORRECT ANSWERS

Question 1
Consider the following iterative implementation used to find the length of a linked list:

struct Node { int val; struct Node *next; }*head; int get....len() { struct Node *temp = head->next; int len = 0; while(.....) { len++; temp = temp->next; } return len; }

Which of the following conditions should be checked to complete the above code?

A
temp->next != 0
B
temp == 0
C
temp != 0
D
none of the mentioned
UPSC test Questions answers

Question 1 Explanation: 
The condition "temp != 0" should be checked to complete the above code.

Question 2
What is the output of the following code?

#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node *next; }*head; int get....len() { struct Node *temp = head->next; int len = 0; while(temp != 0) { len++; temp = temp->next; } return len; } int main() { int arr[10] = {1, 2, 3, 4, 5},  n = 5,  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->val = arr[i]; newNode->next = 0; temp->next = newNode; temp = temp->next; } int len = get....len(); printf("%d", len); return 0; }
A
4
B
5
C
6
D
7
Reasoning Questions answers

Question 2 Explanation: 
The program prints the length of the linked list, which is 5.

Question 3
What is the time complexity of the above iterative implementation used to find the length of a linked list?
A
O(1)
B
O(n)
C
O(n2)
D
O(logn)
Puzzles Questions answers

Question 3 Explanation: 
The time complexity of the above iterative implementation used to find the length of a linked list is O(n).

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....len() { struct Node *temp = head->next; int len = 0; while(temp != 0) { len++; temp = temp->next; } return len; } int main() { int arr[10] = {1, 2, 3, 4, 5},  n = 5,  i; struct Node *temp,  *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head->next = 0; int len = get....len(); printf("%d", len); return 0; }
A
0
B
Garbage value
C
Compile time error
D
Runtime error
UPSC Questions answers

Question 4 Explanation: 
The program prints the length of the linked list, which is 0.

Question 5
Which of the following can be the base case for the recursive implementation used to find the length of linked list?
A
if(current....node == 0) return 1
B
if(current....node->next == 0) return 1
C
if(current....node->next == 0) return 0
D
if(current....node == 0) return 0
GK Questions answers

Question 5 Explanation: 
The line "if(current....node == 0) return 0" can be used as a base case in the recursive implementation used to find the length of a linked list. Note: The line "if(current....node->next == 0) return 1" cannot be used because it won't work when the length of the linked list is zero.

There are 5 questions to complete.