-
Consider the function f defined below :
struct item {
int data;
struct item * next:
};
int f (struct item * p) {
return ((p = = NULL) P(p – > next = = NULL) P
((p– > data < = p – > next –> data) &&
f (p–> next)));
}
For a given linked list p, the function f returns 1, if and only, if
-
- the list is empty or has exactly one element
- the elements in the list are sorted in non-decreasing order of data value
- the elements in the list are sorted in non-increasing order of data value
- not all element in the list have the same data value
- the list is empty or has exactly one element
Correct Option: B
Here the return 1 any 1 of the following should be correct.
(a) P == NULL i.e the list is empty (ends)
(b) P → next = NULL i.e., have one element.
(c) P->data <= p->next ->data i.e., the element is smaller than its next element also. This is true for whole list. Since &&f(p “next”) is also there. So overall it gives that the elements should be in sorted order.