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

Best answer
69 votes
69 votes

Here is the implemented code in c (-std=c99).

#include <stdio.h>
#include <stdlib.h>

#define M 5
#define N 4

int M_data[M] = {1,2,3,4,5};
int N_data[N] = {6,7,8,9};

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

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

node * bulk_insert(int list_data[],int size) {
    node * head = NULL;
    for(int i=0;i<size;i++) {
        node * newnode = malloc(sizeof(node));
        newnode->data = list_data[i];
        newnode->next = NULL;

        if(head == NULL) {
            head = newnode;
        }else {
            node * temp = head;
            while(temp->next != NULL) temp = temp->next;
            temp->next = newnode;
        }
    }
    return head;
}
void display(node *);
void list_dealloc(node *); /*deallocation prototype*/

int main() {
    node * m = NULL;
    node * n = NULL;
    // insert m_list data
    m = bulk_insert(M_data,M);
    n = bulk_insert(N_data,N); // commenting this causes runtime error
    // is list n is empty
    printf("\n before join operation :\n");
    display(m);
    display(n);

    join(m,n);

    printf("\n after join operation :\n");
    display(m);
    display(n);

    //list_dealloc(m); no need now
    list_dealloc(n); // OK
    return 0;

}

void display(node *head) {
    while(head != NULL) {
        printf("%d->",head->data);
        head = head->next;
    }
    printf("null\n");
}
void list_dealloc(node * head) {
    while(head != NULL) {
        node * temp = head;
        head = head->next;
        free(temp);
    }
}


With both n and m and n being non-empty linked list, then,

O/P:


 before join operation :
1->2->3->4->5->null
6->7->8->9->null

 after join operation :
1->2->3->4->5->null
6->7->8->9->1->2->3->4->5->null

With n being empty linked list, then,

O/P:


Correct Answer: $B$

edited by
46 votes
46 votes
here it is explicitely mentioned that LL are terminated by NULL

so suppose i have m as 1->2-> NULL

and n= 3->4->NULL

so according to the code fragement we need to append M to list N

but in the process is the lsit N is empty but valid like this

node*head;

head->NULL:

it may dereferrence a NULL pointer
so

i think

B is correct answer here

correct me if i am wrong
edited by
15 votes
15 votes
here WHILE loop in Void Join() takes the pointer P to last node of list N since it was pointing to list N head initially hence once we reach last node p->next=m will append the list M to end of list N as ADDRESS of M's head is saved in the last node of N so correct option should be

OPTION (A)
14 votes
14 votes

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 should be correct ans.

Answer:

Related questions

38 votes
38 votes
7 answers
1
Arjun asked Feb 14, 2017
18,659 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,619 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...