3.2Doubly Linked List
Inserting at the head
Inserting at the head of a doubly linked list is almost the same as inserting at the head of a singly linked list. The difference is that every node also has a link that points backwards. When the new node moves to the front, that link in the old first node has to be updated too.
- Which two links change when inserting at the head, and in which order they are written
- Why the empty list needs an extra check
- Why the operation does not depend on the length of the list, that is, why it is O(1)
1Idea
The new node becomes the first element, so there is nothing in front of it: its prev field stays NULL. Its next field must point to the old first node, the one head points to.
In a singly linked list the job would be done here. In a doubly linked list the old first node's prev field is still NULL; we have to point it at the new node. Otherwise code that walks the list from the end stops at 12 and never sees 5.
If the list is empty, head is NULL and there is no old first node to update. That is why the line head->prev = node is guarded by a NULL check. In the last step head is moved to the new node.
2Step by step
The drawing adds 5 to the front of a three-node list holding 12, 7 and 3. Blue arrows are prev links. At each step the matching line of code lights up.
- Start. head points to 12. The prev field of 12 is NULL and the next field of 3 is NULL.
- Memory is allocated for the new node, 5 is written into it and its prev field is set to NULL. Its
nextfield holds nothing meaningful yet. - The value of head is written into the new node's next field. 5 now points to 12.
- The list is not empty, so the old first node's prev field is pointed at the new node. Going back from 12 now reaches 5.
- head is moved to the new node. 5 is now the first element.
- Result: 5 ⇄ 12 ⇄ 7 ⇄ 3. None of the other nodes were touched.
struct Node { int data; struct Node* prev; struct Node* next;}; void insertAtHead(struct Node** head, int value) { struct Node* node = malloc(sizeof(struct Node)); node->data = value; node->prev = NULL; node->next = *head; if (*head != NULL) (*head)->prev = node; *head = node;}class Node { int data; Node prev, next; Node(int data) { this.data = data; }} Node insertAtHead(Node head, int value) { Node node = new Node(value); // prev and next are null node.next = head; if (head != null) head.prev = node; return node; // the new head}class Node: def __init__(self, data): self.data = data self.prev = None self.next = None def insert_at_head(head, value): node = Node(value) # prev and next are None node.next = head if head is not None: head.prev = node return node # the new headThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Independent of the number of nodes: one allocation, one comparison and at most four assignments.
Space is allocated only for the new node.
Same complexity as inserting at the head of a singly linked list; the only difference is one more link to change.
4Common mistake
The most common mistake is forgetting to update the old first node's prev field:
node->next = *head;*head = node; // the old first node's prev is still NULLWalking forward gives the right answer, so the bug does not show at once. But code that walks back from the end stops at the old first node and skips the new one. The opposite is also a bug: writing (*head)->prev = node without the NULL check writes through NULL on an empty list and the program crashes.
5Try it
If this function is called on an empty list (head = NULL), which line does not run?
The answer is B. An empty list has no old first node to update. The NULL check skips this line; the other lines build a correct one-node list and both links of the new node stay NULL.
Show the answer
The answer is B. An empty list has no old first node to update. The NULL check skips this line; the other lines build a correct one-node list and both links of the new node stay NULL.
After inserting at the head, in the list 5 ⇄ 12 ⇄ 7, what does the prev field of node 12 point to?
The answer is B. 12 is no longer the first node; 5 is in front of it. The line (*head)->prev = node makes this link. The only node whose prev field is NULL is the new first node.
Show the answer
The answer is B. 12 is no longer the first node; 5 is in front of it. The line (*head)->prev = node makes this link. The only node whose prev field is NULL is the new first node.
Lessons you mark show up in the curriculum. This is kept on this device only.