edited by
25,774 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

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
0 votes
0 votes

The correct answer is (A)

Explanation:

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.

Answer:

Related questions

38 votes
38 votes
7 answers
6
Arjun asked Feb 14, 2017
18,893 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
7
Arjun asked Feb 14, 2017
16,798 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...