in DS edited by
25,608 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.6k 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

69 votes
69 votes
Best answer

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
by

4 Comments

If I write      while(p->next != NULL || P!=NULL);

then it's valid ???
2
2
typedef struct node { //Structure tag name
int data;
struct node * next;
}node; //typedef identifier

Even though Structure tag name and typedef identifier have same name node, why isn’t this causing a compiler error?

0
0
edited by

@mrinmoyh

Yes it’s valid.

Also note that, while(p->next != NULL || p!=NULL) is equivalent to while(p->next || p)

2
2
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

12 Comments

i am confused about this too but this is my idea :

we will never defereference a null pointer ( with the given code ) unless the given list is null !!
and if the given ponter is null it wouldnt be a valid list isnt it ??
2
2
@Uddipto "dereference" means access the content of memory location. This won't happen for the case you told. But it happens when n is NULL.
8
8
but node * head

head->null is also a valid list
0
0
A pointer having a value NULL doesn't refer to any valid object.
In computing, a null pointer has a value reserved for indicating that the pointer does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.
https://en.wikipedia.org/wiki/Null_pointer.
1
1

A pointer having a value NULL doesn't refer to any valid object.
In computing, a null pointer has a value reserved for indicating that the pointer does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.
https://en.wikipedia.org/wiki/Null_pointer.

0
0
@mkg think over this perspective - If we assume null list as invalid and suppose you have a function append () which adds element at end of list the meaning turns out to be "Appending element to an invalid list"  which is not correct isn't it ? then meaning should be "Appending element at end of empty list". Moreover the examiner should have specified explicitly that the lists are non-null.
0
0
@mkg I think valid object simply means...refering to object which follow given struct node structure.
1
1
@Arjun sir dont u think the question should be like m and n points to heads of the two linked lists then its easy to tick 2 as one of them can be NULL

 ..simply what do they mean by VALID NULL TERMINATED LINKED LISTS where does the list exist when the head is NULL optionA can also be right IIT ROORKEE'S paper setting is not that good even with that set 2 Circular queue question they didnt mention clearly what they meant by it

what should we assume by valid NULL TERMINATED ?? if a pointer is null then how will it be a list and that too a valid one ??
1
1
True - that ambiguity is there. By valid null-terminated I guess they meant an implementation where head points to the first node and when there are no elements it points to NULL.

PS: IITR did not make the GATE paper -- they were only involved in conducting the exam and deciding that 0.33 is better for negative mark than 1/3. GATE question paper is made by all IITs AFAIK.
0
0
ohh i didnt knew it sir so this year every IIT is going to get involved in paper setting EVEN IISC ?

@Arjun sir
0
0
Yes. I do not have solid proof for the same but this is what I heard from genuine sources. Also new IIT professors usually do not form part of question setting. If we look at the 2 sets of 2017 or 3 sets each of 2016 and 2015 they hardly have any similarity. So, it must be true.
3
3

@ sir..

NULL terminated list means the list ends node with next field as NULL

But anyway there is atleast one node ..so how can 'n' points to NULL..it points to node with next field NULL and no element ?

What this means actually 

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

4 Comments

thanx..it resolved my doubt.. now only one question i am uneasy is the control flow question. 1-43... :D
2
2
I ticked B. hope it will be correct ! ..tensed !
3
3
deka, your view on question 1-43?
0
0
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.

by

2 Comments

“ the list n is itself NULL “, what is the significance of this? How can head of the LL points to NULL.

0
0

@himanshud2611

"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 ..

1
1
Answer:

Related questions