ISRO Scientist/Engineer CSE 2018Data Structures › Linked Lists
ISRO Scientist/Engineer CSE 2018 PYQ: A doubly linked list is declared as: struct Node { int Value; struct Node *Fwd; struct Node *Bwd; }; Where…
A doubly linked list is declared as:
struct Node {
int Value;
struct Node *Fwd;
struct Node *Bwd;
}; Where Fwd and Bwd represent forward and backward link to the adjacent elements of the list. Which of the following segment of code deletes the node pointed to by $X$ from the doubly linked list, if it is assumed that $X$ points to neither the first nor the last node of the list?
- AX $\rightarrow$ Bwd $\rightarrow$ Fwd = X $\rightarrow$ Fwd; X $\rightarrow$ Fwd $\rightarrow$ Bwd = X $\rightarrow$ Bwd;Correct answer
- BX $\rightarrow$ Bwd.Fwd = X $\rightarrow$ Fwd; X.Fwd $\rightarrow$ Bwd = X $\rightarrow$ Bwd;
- CX.Bwd $\rightarrow$ Fwd = X.Bwd; x $\rightarrow$ Fwd.Bwd = X.Bwd;
- DX $\rightarrow$ Bwd $\rightarrow$ Fwd = X $\rightarrow$ Bwd; X $\rightarrow$ Fwd $\rightarrow$ Bwd = X $\rightarrow$ Fwd;
Answer
(A) X $\rightarrow$ Bwd $\rightarrow$ Fwd = X $\rightarrow$ Fwd; X $\rightarrow$ Fwd $\rightarrow$ Bwd = X $\rightarrow$ Bwd;
Approach
To delete an interior node X from a doubly linked list, we must bypass X by connecting its predecessor to its successor and its successor to its predecessor. The predecessor is X->Bwd and the successor is X->Fwd. Therefore, set the predecessor's forward link to the successor, and the successor's backward link to the predecessor.
Reviewed by ISRO IQ Editorial Team.