Doubly linked and circular lists
In a doubly linked list each node also has a prior pointer to the one before, so you can walk back, and inserting changes 4 pointers with p->next = s last; in a circular list the last node points back to the first.
A singly linked list only goes forward: standing on a node, finding the one before it means walking from the head again. A doubly linked list gives each node one more pointer, to the one before; a circular list has the last node point back to the first. In this lesson you write both in C.
A doubly linked node
1typedef struct DNode {2 int data;3 struct DNode *prior; // the one before4 struct DNode *next; // the one after5} DNode;
One more pointer, a little more memory, and in return: from any node, both neighbours are O(1) away.
The first node’s prior and the last node’s next are both NULL. Three nodes linked together look like this:
In a doubly linked list you are standing on node p. How long does it take to reach the node before it?
p->prior gets there in one step: O(1). Only a singly linked list, which has no prior, makes you walk from the head again: O(n).Inserting: 4 pointers, in the right order
To insert a new node s after p (q used to come after p), 4 pointers change:
s->next = p->next;links s to q after it, firstp->next->prior = s;points q back at ss->prior = p;points s back at pp->next = s;changes p's forward pointer, only now
The key is that step 4 comes after steps 1 and 2: both find q through p->next, and once p->next changes, q can't be found. Same as the singly linked list: link the back first, then change the front.
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct DNode {5 int data;6 struct DNode *prior;7 struct DNode *next;8} DNode;9 10DNode *new_dnode(int data) {11 DNode *n = malloc(sizeof(DNode));12 n->data = data;13 n->prior = n->next = NULL;14 return n;15}16 17void show(DNode *head) {18 for (DNode *p = head; p != NULL; p = p->next)19 printf("%d ", p->data);20 printf("\n");21}22 23int main(void) {24 DNode *p = new_dnode(3), *q = new_dnode(7);25 p->next = q;26 q->prior = p;27 28 DNode *s = new_dnode(5);29 s->next = p->next; // 130 p->next->prior = s; // 231 s->prior = p; // 332 p->next = s; // 433 show(p);34 // walk back: from q along prior35 for (DNode *r = q; r != NULL; r = r->prior)36 printf("%d ", r->data);37 printf("\n");38 return 0;39}
3 5 7 7 5 3
(If p is the last node, p->next is NULL and there's no q: step 2 needs if (p->next != NULL) first.)
Put these 4 lines in order to insert s (node 2) after p (node 1), so the program prints 1 2 3 forwards and 3 2 1 backwards.
s->next = p->next;s->prior = p;p->next->prior = s;p->next = s;
1 2 3 3 2 1
p->next, so p->next = s; has to come after them; s->prior = p; can go anywhere. Change p->next first and node 3 is lost.Doubly linked: insert after p
✓Finish insert_after: insert x after p (there is always a node after p). Change the 4 pointers in order so the program prints the list forwards and backwards.
10 20 30 30 20 10
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct DNode {5 int data;6 struct DNode *prior;7 struct DNode *next;8} DNode;9 10DNode *new_dnode(int data) {11 DNode *n = malloc(sizeof(DNode));12 n->data = data;13 n->prior = n->next = NULL;14 return n;15}16 17void show(DNode *head) {18 for (DNode *p = head; p != NULL; p = p->next)19 printf("%d ", p->data);20 printf("\n");21}22 23void insert_after(DNode *p, int x) {24 DNode *s = new_dnode(x);25 // 1 s->next 2 p->next->prior 3 s->prior 4 p->next26}27 28int main(void) {29 DNode *a = new_dnode(10), *b = new_dnode(30);30 a->next = b;31 b->prior = a;32 insert_after(a, 20);33 show(a);34 for (DNode *r = b; r != NULL; r = r->prior)35 printf("%d ", r->data);36 printf("\n");37 return 0;38}
Deleting: change both sides
Deleting the node q after p:
1DNode *q = p->next;2p->next = q->next;3if (q->next != NULL)4 q->next->prior = p;5free(q);
Only 2 pointers change, then free. Again, relink everything before the free.
After deleting b, the node after a, what does it print?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct DNode {5 int data;6 struct DNode *prior;7 struct DNode *next;8} DNode;9 10int main(void) {11 DNode a = {1, NULL, NULL}, b = {2, NULL, NULL}, c = {3, NULL, NULL};12 a.next = &b; b.prior = &a; b.next = &c; c.prior = &b;13 DNode *p = &a, *q = p->next;14 p->next = q->next;15 q->next->prior = p;16 printf("%d %d\n", a.next->data, c.prior->data);17 return 0;18}
p->next = q->next makes a point forward straight at c; q->next->prior = p makes c point back straight at a. Both directions skip b: it prints 3 1. Change only one direction and walking back still lands on the deleted b.The node q you are deleting is the last one (q->next is NULL). Which line goes wrong if you don’t check first?
q->next is NULL and q->next->prior tries to change something through a null pointer; the program fails (the engine here stops on that line). That is why it needs if (q->next != NULL) first. p->next = q->next; just copies NULL into p, which is fine.Circular lists: the last points back to the head
Change the last node's next from NULL to the first node and you have a circular list. Starting from any node, you can reach every node.
head↓Now walking can't wait for NULL (it never comes); wait until you're back where you started:
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int main(void) {17 Node *c = new_node(9, NULL);18 Node *head = new_node(3, new_node(7, c));19 c->next = head; // the last points back to the head20 21 Node *p = head;22 do {23 printf("%d ", p->data);24 p = p->next;25 } while (p != head); // stop once back at the start26 printf("\n");27 return 0;28}
3 7 9
It's a do … while because p starts equal to head: take a step first, then check.
In practice a circular singly linked list often keeps no head, only a tail pointer rear (pointing at the last node): the first node is just rear->next, so inserting at the front and at the back are both O(1).
Often asked: an empty list with a header node L is L->next == NULL for a singly linked list and L->next == L for a circular one; in the 4 steps of a doubly linked insert, the last one is always changing p->next.
Swap the do … while for a plain while. What does this print?
1#include <stdio.h>2 3typedef struct Node {4 int data;5 struct Node *next;6} Node;7 8int main(void) {9 Node c = {9, NULL}, b = {7, &c}, a = {3, &b};10 c.next = &a;11 Node *head = &a, *p = head;12 int n = 0;13 while (p != head) {14 n++;15 p = p->next;16 }17 printf("%d\n", n);18 return 0;19}
p starts out equal to head, and while checks first, so the condition is false straight away and the loop never runs: it prints 0. That is why going round once needs do … while: take a step, then check.How do you tell that a circular singly linked list L with a dummy head node is empty?
L->next == L. L->next == NULL is the test for an ordinary singly linked list.A circular singly linked list without a dummy head keeps only the tail pointer rear. Which node is the first one?
rear->next. With rear you reach both the front and the back in one step; that is the point of keeping just a tail pointer.How many in the circle
✓Finish count: go once around from head and count the nodes (the list isn't empty).
4
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10 Node *n = malloc(sizeof(Node));11 n->data = data;12 n->next = next;13 return n;14}15 16int count(Node *head) {17 int n = 0;18 Node *p = head;19 // do … while: until back at head20 return n;21}22 23int main(void) {24 Node *last = new_node(4, NULL);25 Node *head = new_node(1, new_node(2, new_node(3, last)));26 last->next = head;27 printf("%d\n", count(head));28 return 0;29}
Summary
Doubly linked lists walk back; circular lists join end to start
- Doubly linked insert, 4 steps: s->next, p->next->prior, s->prior, and p->next last
- Doubly linked delete: skip it in both directions, then free
- Walking a circular list: stop when back at the start, with do … while
1s->next = p->next;2p->next->prior = s;3s->prior = p;4p->next = s;