Data Structure Questions and Answers-Singly Linked List Operations

YOU CAN DOWNLOAD 200+ SUBJECTS PDF BOOK FOR COMPETITIVE EXAMINATIONS

CLICK HERE TO DOWNLOAD

Data Structure Questions and Answers-Singly Linked List Operations

Question 1 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function.
/* Link list node */ 
struct node { 
 int data; struct node* next; 
}; 
/* head_ref is a double pointer which points to head (or start) pointer of linked list */ 
static void reverse(struct node** head....ref) {
 struct node* prev = NULL; 
struct node* 
current = *head_ref; 
struct node* next; 
while (current != NULL) {
 next = current->next; 
current->next = prev; 
prev = current; 
current = next; 
} 
/*ADD A STATEMENT HERE*/ 
}
What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function correctly reverses a linked list.
A
*head....ref = prev;
B
*head....ref = current;
C
*head....ref = next;
D
*head....ref = NULL;
Question 1 Explanation: 
*head....ref = prev; At the end of while loop, the prev pointer points to the last node of original linked list.

We need to change *head....ref so that the head pointer now starts pointing to the last node.

Question 2 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the output of following function for start pointing to first node of following linked list?

1->2->3->4->5->6 void fun(struct node* start) { if(start == NULL) return; printf("%d ", start->data); if(start->next != NULL ) fun(start->next->next); printf("%d ", start->data); }
A
1 4 6 6 4 1
B
1 3 5 1 3 5
C
1 2 3 5
D
1 3 5 5 3 1
Question 2 Explanation: 
fun() prints alternate nodes of the given Linked List, first from head to end, and then from end to head.

If Linked List has even number of nodes, then skips the last node.

Question 3 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
The following C function takes a simply-linked list as input argument.

It modifies the list by moving the last element to the front of the list and returns

the modified list. Some part of the code is left blank. Choose the correct alternative

to replace the blank line.

typedef struct node { int value; struct node *next; }Node; Node *move....to....front(Node *head) { Node *p, *q; if ((head == NULL: || (head->next == NULL)) return head; q = NULL; p = head; while (p-> next !=NULL) { q = p; p = p->next; } ..... return head; }
A
q = NULL; p->next = head; head = p;
B
q->next = NULL; head = p; p->next = head;
C
head = p; p->next = q; q->next = NULL;
D
q->next = NULL; p->next = head; head = p;
Question 4 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list.

The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?

struct node { int value; struct node *next; }; void rearrange(struct node *list) { struct node *p, * q; int temp; if ((!list) || !list->next) return; p = list; q = list->next; while(q) { temp = p->value; p->value = q->value; q->value = temp; p = q->next; q = p?p->next:0; } }
A
1, 2, 3, 4, 5, 6, 7
B
2, 1, 4, 3, 6, 5, 7
C
1, 3, 2, 5, 4, 7, 6
D
2, 3, 4, 5, 6, 7, 1
Question 4 Explanation: 
The function rearrange() exchanges data of every node with its next node. It starts exchanging data from the first node itself.

Question 5 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is
A
log 2 n
B
n2
C
log 2 n - 1
D
n
Question 5 Explanation: 
In the worst case, the element to be searched has to be compared with all elements of linked list.

There are 5 questions to complete.