• edited by
364 views
0 0 votes

A doubly linked list is declared as:

class Node:
    def __init__(self, value=0, prev=None, next=None):
        self.value = value
        self.prev = prev
        self.next = 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}$.prev.next $= \text{X}$.next $; \text{X}$.next.prev $= \text{X}$.prev; free(X);
  2. $\text{X}$.prev.next $ = \text{X}$.next $; X.$ next.prev $ = \text{X}$.prev; free(X);
  3. $\text{X}$.prev.next $= \text{X}.$prev $; \text{X}$.next.prev $= \text{X}.$prev; free(X);
  4. $\text{X}$.prev.next $= \text{X}$.prev $; \text{X}$.next.prev $= \text{X}$.next; free(X);

1 Answer

2 2 votes

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

Answer:
Position:
Show:

Related questions

3 3 votes
1 1 answer
363
363 views
GO Classes asked Sep 15, 2024
363 views
class Node: def __init__(self, data, next=None): self.data = data self.next = next def print_nodes(ptr): if ptr: print(ptr.data, end=' ') while ptr.next: ptr = ptr.next p...
1 1 vote
1 1 answer
453
453 views
GO Classes asked Sep 15, 2024
453 views
Consider the following code fragment.class Node: def __init__(self, data, next=None): self.data = data self.next = next def invert_list(front): curr = front prev = None n...
3 3 votes
1 1 answer
602
602 views
GO Classes asked Sep 15, 2024
602 views
The following code is intended to remove a node p from a doubly linked list. Assume that we know that p is in the list, so the list is not empty.class Node: def __init__(...
4 4 votes
2 2 answers
442
442 views
GO Classes asked Sep 15, 2024
442 views
Consider a mutual pair of recursive functions g() and h().class Node: def __init__(self, value, next=None): self.value = value self.next = next def g(l): if l is None or ...