301 views

A doubly linked list is declared as:

struct Node {
int Value;
struct Node *prev;
struct Node *next;
};


Which of the following segment of code deletes the node pointed to by $\text{X}$ from the doubly linked list, if it is assumed that $\text{X}$ points to neither the first nor the last node of the list?

1. $\text{X} \rightarrow$ prev $\rightarrow$ next $= \text{X} \rightarrow$ next $; \text{X} \rightarrow$ next $\rightarrow$ prev $= \text{X} \rightarrow$ prev; free(X);
2. $\text{X} \rightarrow$ prev.next $= \text{X} \rightarrow$ next $; X.$ next $\rightarrow$ prev $= \text{X} \rightarrow$ prev; free(X);
3. $\text{X} \rightarrow.$ prev $\rightarrow$ next $= \text{X}.$prev $; \text{X} \rightarrow$ next.prev $= \text{X}.$prev; free(X);
4. $\text{X} \rightarrow$ prev $\rightarrow$ next $= \text{X} \rightarrow$ prev $; \text{X} \rightarrow$ next $\rightarrow$ prev $= \text{X} \rightarrow$ next; free(X);

Answer is option A and we will see why other options are not correct

Answer to the question is Option A

by