A singly linked list can only be followed forward, and its last node leads nowhere. Doubly and circular lists add links that remove these limits.
Doubly Linked List
In a doubly linked list, each node has three fields: prev, data and next. The prev pointer of the first node and the next pointer of the last node are NULL. The list can be traversed in both directions, and a node can be deleted when only its own address is known, since its previous node is reached through prev. The cost is an extra pointer per node, and more pointers to change in each operation. To insert a new node n after node p, we set n->next = p->next, n->prev = p, then if p->next is not NULL set p->next->prev = n, and finally p->next = n. To delete node p, we set p->prev->next = p->next and p->next->prev = p->prev, handling the ends with care.
Definition: Doubly linked list. A linked list in which each node contains a pointer to the next node and a pointer to the previous node, so that it can be traversed in both directions.
Circular Linked List
In a circular linked list, the next pointer of the last node points back to the first node instead of NULL. There is no NULL in the list, so traversal stops when the pointer comes back to the starting node. Any node can be used as a starting point. It is often convenient to keep a pointer to the last node, since last->next then gives the first node, so both insertion at the beginning and at the end take O(1). Circular lists are used in round-robin scheduling, in which processes take turns, in multiplayer games, and in circular buffers. A circular doubly linked list joins both ideas.
Deleting the Last Node of a Circular List
To delete the last node of a circular singly linked list, we traverse to the node just before the last, make it point to the first node, and free the last node. If the list has only one node, deleting it makes the list empty.
Table 2.3: Types of linked lists
| Type | Links per node | End of list |
|---|---|---|
| Singly | next | NULL |
| Doubly | prev, next | NULL at both ends |
| Circular | next | Last points to first |
| Circular doubly | prev, next | Ends joined both ways |
Example 2.3: Circular list operations
Problem: Create a circular list 10 → 20 → 30 → (back to 10), print it, then delete the last node.
Solution:
- Link the three nodes and make 30 point to 10.
- Traverse from head until we return to head: 10 20 30.
- Find the node before the last (20), point it to 10, free 30.
Result: The list is 10 → 20 → (back to 10).
#include <stdio.h>
#include <stdlib.h>
struct node { int data; struct node *next; };
struct node *head = NULL;
void add(int v) {
struct node *n = malloc(sizeof *n), *p = head;
n->data = v;
if (!head) { head = n; n->next = n; return; }
while (p->next != head) p = p->next;
p->next = n; n->next = head;
}
void show(void) {
struct node *p = head;
if (!p) return;
do { printf("%d ", p->data); p = p->next; } while (p != head);
printf("\n");
}
void del_last(void) {
struct node *p = head;
if (head->next == head) { free(head); head = NULL; return; }
while (p->next->next != head) p = p->next;
free(p->next); p->next = head;
}
int main(void) {
add(10); add(20); add(30); show();
del_last(); show();
return 0;
}
Output
10 20 30
10 20
Common mistakes
- In a circular list, stopping at NULL, which never comes, so the loop runs forever.
- In a doubly linked list, updating next but forgetting prev.
- Not handling a circular list with only one node, which points to itself.
Key takeaways
- A doubly linked list has prev and next pointers and can be traversed both ways.
- In a circular list, the last node points to the first.
- Traverse a circular list until the pointer returns to the start.
- Circular lists suit round-robin tasks.
Practice questions
- What is the advantage of a doubly linked list?
- How does traversal end in a circular list?
- Give an application of circular linked lists.
Answers to the practice questions
- It can be traversed in both directions, and a node can be deleted knowing only its own address.
- When the pointer returns to the starting node.
- Round-robin CPU scheduling.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit II
