A doubly linked list node is declared as shown below (illustrated in multiple…

2018

A doubly linked list node is declared as shown below (illustrated in multiple languages for reference):

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;
    }
}

Here, Fwd and Bwd represent the forward and backward links to the adjacent elements of the list. Let X be a pointer to a node in this list (declared in C/C++ style, as above) such that X points to neither the first nor the last node (that is, X refers to an interior/middle node). Which of the following C/C++ code segments correctly deletes the node that X points to, i.e. removes that node from the list while preserving the links between its neighbours? Evaluate each segment strictly under C/C++ pointer semantics.

Answer: A. X->Bwd->Fwd = X->Fwd; X->Fwd->Bwd = X->Bwd ;Concept: To delete an interior node from a doubly linked list, you reconnect its two neighbours to each other so the node is bypassed — the node before it…

  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 429 students.

Show answer & explanation

Correct answer: A

Concept: To delete an interior node from a doubly linked list, you reconnect its two neighbours to each other so the node is bypassed — the node before it must have its forward pointer moved past it, and the node after it must have its backward pointer moved past it. This two-pointer reconnection works only when the node being removed has both a valid previous and a valid next neighbour, which the question guarantees for X.

Application: For node X with previous neighbour X->Bwd and next neighbour X->Fwd, deleting X requires exactly two pointer reassignments:

  1. Point the previous neighbour's forward pointer past X, to X's next node: X->Bwd->Fwd = X->Fwd;

  2. Point the next neighbour's backward pointer past X, to X's previous node: X->Fwd->Bwd = X->Bwd;

After these two assignments, X's former neighbours reference each other directly, so X is fully bypassed (and its memory can then be freed).

Cross-check: The remaining segments fail for two different reasons. Two of them use the '.' operator on a pointer expression (e.g. X.Fwd or X->Bwd.Fwd) — in C, member access through a pointer requires '->', so these segments do not even compile. The remaining segment uses valid '->' syntax but assigns each neighbour's pointer back to itself instead of to the sibling node, so the neighbours never end up linked to each other and X is not actually bypassed. Only the pointer-correct, logic-correct segment removes X while preserving the chain.

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

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…