Which of the following is the main advantage of a doubly linked list over a singly linked list?
Think about how you can move through the list in different directions.
Doubly linked lists have two pointers per node, allowing easy movement forward and backward. Singly linked lists only allow forward movement.
Given the doubly linked list with nodes containing values 1, 2, 3 linked in order, what is the output when traversing backwards from the tail?
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// List: 1 <-> 2 <-> 3
// Tail points to node with data 3
void traverseBackward(struct Node* tail) {
while (tail != NULL) {
printf("%d ", tail->data);
tail = tail->prev;
}
}Start from the tail and move using the prev pointer.
Traversing backwards from tail using prev pointer prints nodes in reverse order: 3, 2, 1.
What is the error in this code snippet for inserting a new node at the head of a doubly linked list?
void insertAtHead(struct Node** head, int data) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = data; newNode->next = *head; newNode->prev = NULL; if (*head != NULL) { (*head)->prev = newNode; } *head = newNode; }
Check what happens to prev pointers after changing head.
The prev pointer of the old head node should be updated before changing the head pointer. The original code sets *head = newNode before updating the prev pointer of the old head, causing incorrect behavior.
What is the output after deleting the node with value 2 from the doubly linked list 1 <-> 2 <-> 3 and then traversing forward?
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Initial list: 1 <-> 2 <-> 3
// After deleting node with data 2
void traverseForward(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
}Deleting node 2 removes it from the chain, so it won't print.
After deleting node 2, the list links 1 directly to 3, so traversal prints 1 and 3 only.
Which scenario best explains why a doubly linked list is preferred over a singly linked list?
Think about operations that require moving backwards or removing nodes easily.
Doubly linked lists allow moving backward and deleting nodes without needing access to the previous node pointer externally, which singly linked lists cannot do efficiently.
