Let be a datatype for an implementation of linked list defined as follows:
typedef struct list {
int data;
struct list *next;
} LIST;Suppose a program has created two linked lists, and , whose contents are given in the figure below (code for creating and is not provided here). contains nodes, and contains nodes.
Consider the following C program segment that modifies the list . The number of nodes that will be there in after the execution of the code segment is __________. (Answer in integer)


int find (int query, LIST *list) {
while (list != NULL) {
if(list->data == query) return 1 ;
list = list->next;
}
return 0 ;
}
int main (){
... ... ...
ptr1=L1; ptr2=L2;
while (ptr1->next != NULL){
query = ptr1->next->data;
if (find (query, L2))
ptr1->next = ptr1->next->next;
else ptr1 = ptr1->next;
}
... .... ...
return 0;
}
View worked solution
Answer: 5:5
Based on the figures provided in the exam:
- contains nodes:
- contains nodes:
The function find(query, L2) returns 1 if the query value exists in , and 0 otherwise.
In the main function, the loop traverses using ptr1 starting at the head ():
- The loop condition is
ptr1->next != NULL, and it checksquery = ptr1->next->data. This means the first node of (value ) is never checked or deleted. - For any subsequent node, if its data is present in , it is bypassed (deleted) via
ptr1->next = ptr1->next->next. - If the data is not in ,
ptr1simply advances to the next node.
Tracing the deletion process for each node in :
- Node 1: Head node, skipped (kept).
- Node 2: Present in deleted.
- Node 3: Not in kept.
- Node 4: Present in deleted.
- Node 5: Not in kept.
- Node 6: Present in deleted.
- Node 7: Not in kept.
- Node 8: Present in deleted.
- Node 9: Not in kept.
After execution, the remaining nodes in are . Thus, the number of nodes in is 5.
Free practice · Your answers on this page are not saved.
