Doubly Linked Lists
Two-way links.
Two-way links
A doubly linked list gives each node two pointers: one to the next node and one to the prev (previous) node.
This lets you walk the list in both directions and simplifies deletion.
#include <stdio.h>
struct Node {
int value;
struct Node *prev;
struct Node *next;
};
int main(void) {
printf("Each node links forward and backward\n");
return 0;
}Defining the node
The struct adds a prev pointer alongside next. Both are NULL at the ends of the list.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *prev; struct Node *next; };
int main(void) {
struct Node *n = malloc(sizeof(struct Node));
n->value = 1; n->prev = NULL; n->next = NULL;
printf("%d\n", n->value);
free(n);
return 0;
}All lessons in this course
- Singly Linked Lists
- Insertion and Deletion
- Traversal and Search
- Doubly Linked Lists