395 views
1 votes
1 votes

A struct Node(shown below) is used to implement a linked list.

struct Node {
  int data;
  struct Node* next;
}

The function a_function (shown below), takes the head (i.e. the first element of the list), as argument, and reorders the list.The swap function inside the function, swaps the 2 elements of the list, which are passed to it as arguments

void a_function (Node* head) {
  Node* a_ptr = head;
  bool val = true;
  while (a_ptr->next != NULL) {
    if (val)  {
        if (a_ptr->data > a_ptr->next->data)
            swap(a_ptr->data, a_ptr->next->data);
    }
    else {
        if (a_ptr->data < a_ptr->next->data)  {
            swap(a_ptr->data, a_ptr->next->data);
        }
    a_ptr = a_ptr->next;
    val = !val;
    }
  }
}

Choose all options, that are correct about the above function a_function

a>The function,a_function, sorts the linked lest in ascending order., 

b>The function, a_function, rearranges the list in zigzag fashion (i.e. a,b,c,d,e,f... be the data in the list after the list is rearranged, then the list is of the form a < b > c < d > e < f...)

c>Given a list 4, 3, 7, 8, 6, 2, 1. The function, a_function, rearranges it to 3, 7, 4, 8, 2, 6, 1.

d>Given a list 11, 15, 20, 16, 21, 23, 17. The function, a_function, rearranges it to 11, 15, 16, 17, 20, 21, 23.

2 Answers

1 votes
1 votes
ans would be C. 4 3 7 8 6 2 1

val=1, a_ptr at 4,  first two element has been swapped : 3 4 7 8 6 2 1

val=0, a_ptr at 4 in updated list ,so 4 and 7 is swapped : 3 7 4 8 6 2 1

val=1,a_ptr at 4 in updated list, but condition failed : 3 7 4 8 6 2 1

val=0 , a_ptr at 8 in updated list, but condition failed : 3 7 4 8 6 2 1

val=1,a_ptr at 6 in updated list  , so 6 and 2 is swapped: 3 7 4 8 2 6 1

val=0,a_ptr at 6 in updated list , but condition failed : 3 7 4 8 2 6 1

val=1 , a_ptr at 1 in updated list but while loop condition fails

so 3 7 4 8 2 6 1 is final ans:

plz correct me...
0 votes
0 votes

As told in question more than one options may be correct.

Here Option B & C are Correct.

How to Approach this Question:-

Step1:- Read all options

Step2:- Directly apply Option C's example into given Code.
and Option C is Correct  so Option A is wrong now verify Option B.

Result of Option C satisfies Option B as shown below

3 < 7 > 4 < 8 > 2< 6 > 1

Step3:- Now Apply Option 4 in Code

We get resultant list as below

11 < 20 > 15 < 21> 16< 23> 17

So option D is incorrect But it

Satisfies Option B.

 So option B & C are Correct.

edited by

Related questions

0 votes
0 votes
0 answers
1
Kartavya Kothari 1 asked Mar 22, 2019
504 views
It maybe silly but I am facing uncertainty in the procedure to apply to IITbI am not sure if the portal I get from Google search is the right place to do it. Also I am co...
0 votes
0 votes
1 answer
2
syncronizing asked Feb 12, 2019
652 views
I’m getting 25.99 in gate 2019, OBC. can I get RA in IIT Bombay?
1 votes
1 votes
0 answers
4
Deborah asked Nov 16, 2018
958 views
The number of distinct rearrangement of the 6 letters in the name RAJAJI is .....