1,159 views
1 1 vote

Consider the function f defined below.

    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

  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 elements in the list have the same data value

I m not getting it ,there is no return 1 statment so how thiz will right ?

1 Answer

0 0 votes

Option B)  return ((p == NULL) || (p->next == NULL)|| ((p->data <= p ->next -> data) && f(p->next))). Look at the return condition carefully. F will return true only if the value of all successive nodes are greater than the previous node. If there are no nodes or if there is only one node even then this function will return true. Hence the OR conditions (p == NULL) || (p->next == NULL).

Position:
Show:

Related questions

0 0 votes
0 0 answers
1.1k
1.1k views
hem chandra joshi asked Nov 16, 2017
1,143 views
References : https://gateoverflow.in/893/gate2003-2int *A[10];so can any one describe A [3];A is valid . As it is array of pointer and A third element in this array of ...
0 0 votes
1 1 answer
619
619 views
hem chandra joshi asked Dec 20, 2017
619 views
The cube root of a natural number n is defined as the largest natural number m such that (m^3≤n) . The complexity of computing the cube root of n (n is represented by bin...
0 0 votes
1 1 answer
1.8k
1.8k views
Piyush Kapoor asked Sep 24, 2015
1,820 views
Assume that the operators +,−,× are left associative and ^ is right associative. The order of precedence (from highest to lowest) is ^,×,+,−. The postfix expression corre...
0 0 votes
1 answers 1 answer
258
258 views
IT_021_Manish_Kumar asked Dec 6, 2025
258 views
If definition of complete binary tree is not given in the questions then what should by default I need to consider ?