edited by
25,741 views
85 votes
85 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.
edited by

11 Answers

2 votes
2 votes
Here they clearly mention m and n are null terminate list i.e. some of element are already persent in both list => append list m to the end of list n for all inputs option(A) is correct
2 votes
2 votes

If the list itself would be null, how can that list be called as a valid null-terminated linked list?

I am not able to perceive the question depending on the language! As per me, it should be 'A'.

Please help!

0 votes
0 votes
Here it is explicitely mentioned that this is a NULL terminated Linked list

for example i have two list 1->2->NULL

and 3->4->NUll

now when i am appending list m to list n(as per the code fragement)

i may need to dereference the last pointer of list n and point it to the first element of list m

 so i think B is the answer

correct me if wrong
0 votes
0 votes
It is given that list are valid and null terminated. According to my interpretation NULL list is also valid.

Thus if n is NULL. n->next is equivalent to null->next. Hence either null dereference issue or appends list m at end of n
Answer:

Related questions

38 votes
38 votes
7 answers
1
Arjun asked Feb 14, 2017
18,877 views
Let $T$ be a tree with $10$ vertices. The sum of the degrees of all the vertices in $T$ is ________
33 votes
33 votes
10 answers
2
Arjun asked Feb 14, 2017
16,773 views
Let $T$ be a binary search tree with $15$ nodes. The minimum and maximum possible heights of $T$ are:Note: The height of a tree with a single node is $0$.$4$ and $15$ res...