Data Structures - Doubly Linked List
A doubly linked list (Doubly Linked List) is an extension of a singly linked list; besides pointing to the next node, each node additionally stores a pointer to the previous node, forming a two-way traversable chained structure.
Structure and characteristics of a doubly linked list
Doubly Linked List - node structure and two-way traversal
→
→
next pointer direction (forward traversal)
prev
NULL
data
10
next
→
prev
←
data
20
next
→
prev
←
data
30
next
NULL
←
←
prev pointer direction (backward traversal)
struct Node { int data; struct Node* prev; struct Node* next; };
prev points to the previous node | data stores the data | next points to the next node
Core advantage: can directly delete any node (no need to keep the predecessor) and supports two-way traversal
blue = next pointer (forward) | purple = prev pointer (backward) | each node = prev + data + next
As shown in the figure above, each node of a doubly linked list has two pointers, prev and next.
In C, the definition of a doubly linked list node is as follows:
Example
struct
Node
{
int
data
;
/* data field */
struct
Node
prev
;
/* predecessor pointer: points to the previous node */
struct
Node
next
;
/* successor pointer: points to the next node */
}
;
Compared with a singly linked list, the greatest advantage of a doubly linked list is that it can directly access both the predecessor and successor of any node, so deletion no longer needs to keep an extra reference to the predecessor node.
In a singly linked list, you must know the predecessor node to adjust the pointers when deleting a given node. In a doubly linked list, since each node already stores the prev pointer, deletion can be done directly.
Basic operations
Insertion operation
Inserting into a doubly linked list requires adjusting the new node's prev/next pointers as well as those of its predecessor and successor, involving four pointer adjustments in total.
Example
#include
#include
struct
Node
{
int
data
;
struct
Node
prev
;
struct
Node
next
;
}
;
/* Create a new node */
struct
Node
createNode
(
int
value
)
{
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
newNode
->
data
=
value
;
newNode
->
prev
=
NULL
;
newNode
->
next
=
NULL
;
return
newNode
;
}
/* insert a node at the head
steps: the new node's next points to the original head, and the original head's prev points to the new node */
struct
Node
insertAtHead
(
struct
Node
head
,
int
value
)
{
struct
Node
newNode
=
createNode
(
value
)
;
if
(
head
!=
NULL
)
{
newNode
->
next
=
head
;
head
->
prev
=
newNode
;
}
return
newNode
;
/* the new node becomes the new head node */
}
/* insert a new node after a specified node
need to manipulate 4 pointers:
1-2: the new node's prev and next
3: the successor node's prev (if it exists)
4: the predecessor node's next */
void
insertAfter
(
struct
Node
prevNode
,
int
value
)
{
if
(
prevNode
==
NULL
)
return
;
struct
Node
newNode
=
createNode
(
value
)
;
newNode
->
next
=
prevNode
->
next
;
/* the new node's next points to the original successor */
newNode
->
prev
=
prevNode
;
/* the new node's prev points to the predecessor */
if
(
prevNode
->
next
!=
NULL
)
{
prevNode
->
next
->
prev
=
newNode
;
/* the original successor's prev points to the new node */
}
prevNode
->
next
=
newNode
;
/* the predecessor's next points to the new node */
}
Delete operation
Example
/* delete a specified node (no need to know the predecessor node, which is the biggest advantage of a doubly linked list)
steps: connect the surrounding nodes, skip the current node, then free the current node */
void
deleteNode
(
struct
Node
**
headRef
,
struct
Node
del
)
{
if
(
headRef
==
NULL
||
del
==
NULL
)
return
;
/* if the deleted node is the head, update the head pointer */
if
(
headRef
==
del
)
{
headRef
=
del
->
next
;
}
/* make the predecessor node's next point to the successor node */
if
(
del
->
prev
!=
NULL
)
{
del
->
prev
->
next
=
del
->
next
;
}
/* make the successor node's prev point to the predecessor node */
if
(
del
->
next
!=
NULL
)
{
del
->
next
->
prev
=
del
->
prev
;
}
free
(
del
)
;
/* free the deleted node */
}
Traversal operation (two-way)
A doubly linked list supports both forward and backward traversal.
Example
#include
/* forward traversal: start from head, follow the next direction */
void
traverseForward
(
struct
Node
head
)
{
printf
(
"Forward traversal: "
)
;
struct
Node
cur
=
head
;
while
(
cur
!=
NULL
)
{
printf
(
"%d "
,
cur
->
data
)
;
cur
=
cur
->
next
;
}
printf
(
"\n"
)
;
}
/* backward traversal: start from tail, follow the prev direction
need to find the tail node first */
void
traverseBackward
(
struct
Node
head
)
{
if
(
head
==
NULL
)
return
;
/* first move to the last node (i.e., the tail) */
struct
Node
cur
=
head
;
while
(
cur
->
next
!=
NULL
)
{
cur
=
cur
->
next
;
}
/* return from the tail to the head along the prev direction */
printf
(
"Reverse traversal: "
)
;
while
(
cur
!=
NULL
)
{
printf
(
"%d "
,
cur
->
data
)
;
cur
=
cur
->
prev
;
}
printf
(
"\n"
)
;
}
int
main
(
)
{
struct
Node
head
=
NULL
;
/* build linked list: 10 <-> 20 <-> 30 */
head
=
insertAtHead
(
head
,
30
)
;
head
=
insertAtHead
(
head
,
20
)
;
head
=
insertAtHead
(
head
,
10
)
;
traverseForward
(
head
)
;
/* output: forward traversal: 10 20 30 */
traverseBackward
(
head
)
;
/* output: backward traversal: 30 20 10 */
return
0
;
}
Doubly linked list vs singly linked list
| Comparison Dimensions | Singly linked list | Doubly linked list |
|---|---|---|
| Node structure | data + next | data + prev + next |
| Extra memory per node | 8 bytes (64-bit system) | 16 bytes (64-bit system) |
| Forward traversal | Supported | Supported |
| Backward traversal | Not supported | Supported |
| Delete node | Need to know the predecessor node | No predecessor needed; can delete directly |
| Insertion operation | Adjust 1~2 pointers | Adjust 4 pointers |