in DS edited by
25,353 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.
in DS edited by
25.4k views

4 Comments

"Assuming that m and n point to valid NULL-terminated linked lists" – the line says  that the linked lists are not infinite they are finite ..

hence option B is correct.

0
0

@Rajatagrawal   firstly you need to have something to NULL-terminate. The statement points to the fact that the linked list will have atleast one node. (in my opinion, of course).

If $n$ is $NULL$, then it’s not even a linked list, but a just a pointer.

0
0

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 Comments

empty list are also NULL terminated right ?
1
1
@dd you cannot call just a null pointer i.e if n=NULL ,then n is not a “valid” list terminated with null pointer, the ambiguity arised due to the word valid!

You definitely cannot call a NULL as a Valid list terminated by null !
0
0
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!

by

1 comment

Leave such ambiguous questions in exam.
1
1
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
by
Answer:

Related questions