A doubly linked list is declared as C++struct Node { int Value; struct Node…

2018

A doubly linked list is declared as

C++
struct Node {
       int Value;
       struct Node *Fwd;
       struct Node *Bwd;
);
C
// Struct definition in C
struct Node {
    int Value;
    struct Node *Fwd;
    struct Node *Bwd;
};
Java
// Struct definition in Java
class Node {
    int Value;
    Node Fwd;
    Node Bwd;
}
Python
# Class definition in Python
class Node:
    def __init__(self, value):
        self.Value = value
        self.Fwd = None
        self.Bwd = None
JavaScript
// Class definition in JavaScript
class Node {
    constructor(value) {
        this.Value = value;
        this.Fwd = null;
        this.Bwd = null;
    }
}

Where Fwd and Bwd represent forward and backward link to the adjacent elements of the list. Which of the following segments 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?

  1. A.

    X->Bwd->Fwd = X->Fwd; X->Fwd->Bwd = X->Bwd ;

  2. B.

    X->Bwd.Fwd = X->Fwd ; X.Fwd->Bwd = X->Bwd ;

  3. C.

    X.Bwd->Fwd = X.Bwd ; X->Fwd.Bwd = X.Bwd ;

  4. D.

    X->Bwd->Fwd = X->Bwd ; X->Fwd->Bwd = X->Fwd;

Attempted by 270 students.

Show answer & explanation

Correct answer: A

To delete node X from a doubly linked list, update the pointers of its adjacent nodes to bypass it. Since X is not an endpoint, set the previous node's forward pointer to point to X's next node: X->Bwd->Fwd = X->Fwd. Then set the next node's backward pointer to point to X's previous node: X->Fwd->Bwd = X->Bwd. This links the neighbors directly, effectively removing X from the chain.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Isro