Let LIST 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, L1 and L2, whose contents are given in the figure below. L1 contains 9 nodes, and L2 contains 7 nodes.
Consider the following C program segment that modifies the list L1. The number of nodes that will be there in L1 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) {
int query = ptr1->next->data;
if (find(query, L2)) {
ptr1->next = ptr1->next->next;
} else {
ptr1 = ptr1->next;
}
}
...
return 0;
}
✅ Final Answer:
The correct answer is 5.
🔹 Short Answer:
The code iterates through L1 using ptr1 and checks if the *next* node's data exists in L2. If it does, the next node is deleted by bypassing it. If not, ptr1 advances. The data values from L1 that are also in L2 are {12, 9, 11, 15}. These four nodes get deleted from L1. L1 initially has 9 nodes, so after deleting 4 nodes, it is left with 9 - 4 = 5 nodes.
🔸 Long Answer:
Let's trace the execution of the C code step-by-step to understand how list `L1` is modified.
Code Logic Breakdown
ptr1: A pointer that traverses list L1.
while (ptr1->next != NULL): The loop runs as long as `ptr1` is not pointing to the last node. This is key, as the code always inspects the node *after* `ptr1`.
query = ptr1->next->data;: We get the data from the node we are about to potentially delete.
if (find(query, L2)): If the data exists in L2, we execute the `if` block.
ptr1->next = ptr1->next->next;: This is the deletion step. We make the current node's `next` pointer skip over the next node, effectively removing it from the list. Crucially, `ptr1` does **not** advance in this case, because its new `next` node needs to be checked in the next iteration.
else { ptr1 = ptr1->next; }: If the data is not in L2, the node is safe. We simply advance `ptr1` to the next node to continue the scan.
Execution Trace
Values in L2 for easy lookup: {1, 11, 6, 9, 15, 12, 4}
Bibliography:
- Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language. Prentice Hall.
- Cormen, T. H., et al. (2009). Introduction to Algorithms. MIT Press. (Chapter 10: Elementary Data Structures).
Ask your doubts in comment form our GURUJEE will reply ASAP, click notify me button so that you get notified on reply.
Get link
Facebook
X
Pinterest
Email
Other Apps
Comments
Popular posts from this blog
GATE 2025 CS/IT Question 1 GATE2025_CS1_Q1 Question: Ravi had ____ younger brother who taught at ____ university. He was widely regarded as ____ honorable man. Select the option with the correct sequence of articles to fill in the blanks. A. a; a; an B. the; an; a C. a; an; a D. an; an; a 👁️ View Full Answer ✅ Final Answer: The correct sequence of articles is (a; a; an) , which corresponds to option (A) . 🔹 Short Answer: The selection of the indefinite article ('a' or 'an') is based on the initial sound of the word that follows. 'a' is used before consonant sounds, and 'an' is used before vowel sounds. 1. " a younger brother" - 'younger' starts with a consonant sound (/j/). 2. " a university" - 'university' starts with a consonant sound (/juː/). 3. " an honorable man"...
GATE 2025 CS/IT Question 64 GATE2025_CS1_Q64 The maximum value of x such that the edge between the nodes B and C is included in every minimum spanning tree of the given graph is ______. (answer in integer) Check Answer 👁️ View Full Answer ✅ Final Answer: The correct answer is 5 . 🔹 Short Answer: For edge (B,C) to be in *every* MST, its weight 'x' must be strictly less than the bottleneck (heaviest edge) of any alternative path between B and C. The alternative paths and their bottlenecks are: B-A-C (bottleneck 7), B-D-C (bottleneck 8), and B-D-A-C (bottleneck 6). To satisfy all conditions, 'x' must be less than the minimum of these bottlenecks, so `x 🔸 Long Answer: Graph for MST Question A B C D 7 6 x ...
GATE 2025 CS/IT Question 65 GATE2025_CS1_Q65 In a double hashing scheme, h₁(k) = k mod 11 and h₂(k) = 1 + (k mod 7) are the auxiliary hash functions. The size m of the hash table is 11. The hash function for the i-th probe in the open address table is [h₁(k) + i h₂(k)] mod m. The following keys are inserted in the given order: 63, 50, 25, 79, 67, 24. The slot at which key 24 gets stored is ______. (Answer in integer) Check Answer 👁️ View Full Answer ✅ Final Answer: The correct answer is 10 . 🔹 Short Answer: To find the slot for key 24, we calculate its hash values: h₁(24) = 24 mod 11 = 2 and h₂(24) = 1 + (24 mod 7) = 1 + 3 = 4. We then probe the hash table. Probe 0: `(2 + 0*4) mod 11 = 2` (collision). Probe 1: `(2 + 1*4) mod 11 = 6` (collision). Probe 2: `(2 + 2*4) mod 11 = 10` (empty). Key 24 is placed in slot 10. 🔸 Long Answer: This problem requi...
Comments
Post a Comment
Ask you doubt here