Consider the following piece of ‘C’ code fragment that removes duplicates from…

PYQ GATE 1997

Consider the following piece of ‘C’ code fragment that removes duplicates from an ordered list of integers.

struct node {
int val;
struct node *next;
};
typedef struct node Node;
Node *remove_duplicates(Node *head, int *j)
{
Node *t1, *t2;
*j = 0;
t1 = head;
if (t1 != NULL) t2 = t1->next;
else return head;
*j = 1;
if (t2 == NULL) return head;
while (t2 != NULL)
{
if (t1->val != t2->val) /* S1 */
{
(*j)++; t1->next = t2; t1 = t2; /* S2 */
}
t2 = t2->next;
}
t1->next = NULL;
return head;
}

Assume the list contains n elements (n ≥ 2) in the following questions.

(a) How many times is the comparison in statements S1 made?

(b) What is the minimum and the maximum number of times statements marked S2 get executed?

(c) What is the significance of the value in the integer pointed to by j when the function completes?

Attempted by 2 students.

Sign up free to check your answer

Sign up free

Explore the full course: Zero To Hero

Loading lesson…