The Gateway to Computer Science Excellence
+48 votes

Consider the C code fragment given below.

typedef struct node {
    int data;
    node* next;
} node;

void join(node* m, node* n) {
    node* p = n;
    while(p->next != NULL) {
        p = p->next;
    p->next = m;

Assuming that m and n point to valid NULL-terminated linked lists, invocation of join will

  1. append list m to the end of list n for all inputs.
  2. either cause a null pointer dereference or append list m to the end of list n.
  3. cause a null pointer dereference for all inputs.
  4. append list n to the end of list m for all inputs.
in DS by Loyal (7k points)
edited by | 8k views
please include a tag "gate2017-1"
My interpretation of the question -

It is not mentioned explicitly that the lists are non-null. They are valid null-terminated implies if the list is non-empty the last node pointer successfully points to the null. Thus if the list n is itself NULL then dereferecing will create NULL pointer issue. Thus option b) Either null de-reference issue or appends list
any one challenging this question ??

Option B is correct answer as if node *n is inicially NULL then 

while(p->next != NULL)

will couse an error else list m append to the end of list n .

while(p->next != NULL),

when n is NULL,

the above stmt can be written as,

((*p).next != NULL) - this is null pointer dereference exception.

As it is stated in the question, that m and n are valid Lists but not explicitly specified if the lists are empty or not. We can have two cases:
  1. Case 1: If lists are not NULL : Invocation of join will append list m to the end of list n if the lists are not NULL.      For Example:                                                                                                     Before join operation:          m=1->2->3->4->5->null;    n=6->7->8->9->null;                                                                                                          After join operation :                               6->7->8->9->1->2->3->4->5->null
  2. Case 2: If lists are NULL : If the list n is empty and itself NULL, then joining and referencing would obviously create NULL pointer issue.
Therefore option B is correct

8 Answers

0 votes

The correct answer is (A)


1. void join(node *m, node *n){

    Here we are sending pointer reference of two node type data. It is a linked list.

2. node *p = n;

    Now, new pointer node p defined with its initial value equal to n. So now, p also points to start of n.

3. while (p->next !=NULL){

    start of traversal of linked list. If the next pointer of the node is pointing to NULL then stop there.

4. p = p->next;

    If p->next is not pointing to NULL i.e. it is not end of the list then update value of p = p->next i.e. now it will be pointing to the next node.

5. }

   When the loop ends, the p will be pointing to the last node with p->next = NULL

6. p->next = m;

   p->next which was pointing to NULL now will point to m, which the start of next node.

7. }

   End of program.

So, list m will append to end of list n.

by (35 points)
great explaination
0 votes
As specified in the question m & n are valid lists, but there is no specific condition/ statement tells that lists are empty or not.
So have to consider both the cases.
⇾ Case 1. Lists are not null, invocation of JOIN will append list m to the end of list n.
m = 1, 2, 3
n = 4, 5, 6
After join, it becomes 4, 5, 6, 1, 2, 3, NULL.
⇾Case 2. If lists are null, if the list n is empty, and itself NULL, then joining & referencing would obviously create a null pointer issue.
Hence, it may either cause a NULL pointer dereference or appends the list m to the end of list n.
by (21 points)
–1 vote
may be possible for null pointer derefrences because m and n may point at end of list. which are null.
by (229 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,662 questions
56,122 answers
93,030 users