Which of the following is the main advantage of a doubly linked list over a singly linked list?
Think about how the nodes are connected and how you can move through the list.
A doubly linked list has two pointers per node, allowing traversal forwards and backwards. A singly linked list only allows forward traversal.
Given a doubly linked list with nodes containing values 1, 2, 3 in order, what is the output when traversing from tail to head?
Node tail = new Node(3); Node middle = new Node(2); Node head = new Node(1); head.Next = middle; middle.Previous = head; middle.Next = tail; tail.Previous = middle; Node current = tail; while (current != null) { Console.Write(current.Value + " "); current = current.Previous; }
Start from the tail and move using the previous pointers.
Starting from the tail node (3), moving backwards through previous pointers visits nodes 3, 2, then 1.
What is the error in the following code snippet that inserts a new node after a given node in a doubly linked list?
void InsertAfter(Node node, Node newNode) {
newNode.Next = node.Next;
node.Next = newNode;
newNode.Previous = node;
if (newNode.Next != null) {
newNode.Next.Previous = newNode;
}
}Consider what happens if the given node is the last node in the list.
If node.Next is null (node is tail), accessing node.Next.Previous causes a null reference error.
What is the state of the doubly linked list after removing the middle node (value 2) from 1 <-> 2 <-> 3?
Node head = new Node(1); Node middle = new Node(2); Node tail = new Node(3); head.Next = middle; middle.Previous = head; middle.Next = tail; tail.Previous = middle; // Remove middle node head.Next = middle.Next; middle.Next.Previous = head; // Traverse from head Node current = head; string result = ""; while (current != null) { result += current.Value + " "; current = current.Next; } Console.WriteLine(result);
After removal, the list should skip the removed node.
Removing the middle node links head directly to tail, so traversal prints 1 then 3.
Why might a doubly linked list be less memory efficient than a singly linked list?
Think about the number of pointers stored in each node.
Doubly linked list nodes have two pointers (next and previous), doubling pointer storage compared to singly linked lists.
