Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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
    1. the list is empty or has exactly one element
    2. the elements in the list are sorted in non-decreasing order of data value
    3. the elements in the list are sorted in non-increasing order of data value
    4. not all element in the list have the same data value
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.



Your comments will be displayed only after manual approval.