Kodewick
Start learning →
Doubly linked and circular lists9 / 50

Doubly linked and circular lists

Lesson 9 of 50 · Part 2 · Arrays and linked lists · about 14 minutes

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:

Three nodes of a doubly linked list: next points forward (solid), prior points back (dashed), and both ends are NULL
Choose one

In a doubly linked list you are standing on node p. How long does it take to reach the node before it?

Inserting: 4 pointers, in the right order

To insert a new node s after p (q used to come after p), 4 pointers change:

Inserting into a doubly linked list: 5 (s) goes between 3 (p) and 7 (q), the four pointers change in order 1 to 4, and the dashed lines are the old links
  1. s->next = p->next; links s to q after it, first
  2. p->next->prior = s; points q back at s
  3. s->prior = p; points s back at p
  4. p->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.

main.c
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}
Output
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 in orderDrag, or use the arrows

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.

  1. s->next = p->next;
  2. s->prior = p;
  3. p->next->prior = s;
  4. p->next = s;
Should print
1 2 3
3 2 1
Exercise 1

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.

It should print
10 20 30
30 20 10
exercise1.c
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}
Output
Press Run or Ctrl+Enter

Deleting: change both sides

Deleting the node q after p:

Deleting from a doubly linked list: 5 (q) comes out from between 3 (p) and 7. The dashed lines are the four old pointers through q; 1 makes 3 point forward straight at 7, 2 makes 7 point back straight at 3, so both directions skip q, then free(q)
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.

Guess the outputA wrong guess is fine

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}
Choose one

The node q you are deleting is the last one (q->next is NULL). Which line goes wrong if you don’t check first?

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↓
3a
7b
9c
back to 3

A circular list: the last node's next points back to the first, no NULL

Now walking can't wait for NULL (it never comes); wait until you're back where you started:

main.c
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}
Output
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.

Guess the outputA wrong guess is fine

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}
Choose one

How do you tell that a circular singly linked list L with a dummy head node is empty?

Choose one

A circular singly linked list without a dummy head keeps only the tail pointer rear. Which node is the first one?

Exercise 2

How many in the circle

Finish count: go once around from head and count the nodes (the list isn't empty).

It should print
4
exercise2.c
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}
Output
Press Run or Ctrl+Enter

Summary

Take-away

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;