in DS recategorized by
344 views
10 votes
10 votes

Consider a function findCLL that takes a doubly circular linked list head and an integer value as input.

One example of doubly circular linked list is given below –

int findCLL(struct node * first, int n) {
    while (first -> value != n)
        first = first -> next;
        if (first -> value == n)
            return 1;
        else
            return -1;
}


Consider two statements $\text{S1}$ and $\text{S2}$ given below.

  • $\text{S1}:$ Function returns $1$ if there exists a value in linked list
  • $\text{S2}:$ Function returns $-1$ if value does not exist in linked list

Which of the following is the correct option.

  1. $\text{S1}$ is True but $\text{S2}$ is False.
  2. $\text{S2}$ is True but $\text{S1}$ is False.
  3. Both are True.
  4. Both are False.
in DS recategorized by
344 views

2 Answers

13 votes
13 votes

Here let us take the given circular doubly linked list as the example

Say we need to find element 4

in the while loop we move forward 4 nodes as first → value != 4 

when we arrive at 5th node first → value == 4 so we break out from the loop 

if condition is also satisfied as first → value == 4 

so we return 1 

so S1 is TRUE

Now say we need to find element say 6

here when we check the value of each node with 6 in the while loop there is not hit because 6 is not present in the circular doubly linked list and more over the loop will break only when the we can find the element that we need 

So if the element that we need to find is not present in linked list we go into “ INFINITE LOOP “

which means we cant return anything so S2 is FALSE

Finally option A is correct

4 Comments

Here, for when we are at node 9, inside while loop, we go to next node then if condition becomes false so we go to else case then, we should return -1 right? why going for next iteration. One of the if/else must execute and it returns then and there.
0
0
That if else is not a part of the while loop.

While body has only one statement in it, since brackets are missing.
12
12
Nice trap set up by sir, which I fell for.
1
1
Me too 😅😅
4
4
0 votes
0 votes
If n is present in the list, it will return 1.

If n is not present in the list, it will result in an infinite loop. If-else statement is not part of the while loop. So, it won’t be able to return “-1” in case n is not present in the list.

Correct Answer – A
Answer:

Related questions