Consider the function f defined below.
c
struct item
{
int data;
struct item * next;
};
int f( struct item *p)
{
return (
(p == NULL) ||
(p->next == NULL) ||
(( p->data <= p->next->data) && f(p->next))
);
}
|
For a given linked list p, the function f returns 1 if and only if (GATE CS 2003)
(A)
not all elements in the list have the same data value.
(B)
the elements in the list are sorted in non-decreasing order of data value
(C)
the elements in the list are sorted in non-increasing order of data value
(D)
None of them
Answer: (B)
Explanation:
The function checks if the current element is less than or equal to the next element, and recursively applies the same check to the next element. If the end of the list is reached (i.e., p->next is NULL), or the next element is less than the current element, the function returns 1. Otherwise, it returns 0. Therefore, the function returns 1 only if the linked list is sorted in non-decreasing order.
Quiz of this Question
Please comment below if you find anything wrong in the above post