Doubly Linked List in C
Doubly Linked List in C
A doubly linked list is a type of linked list in which each node contains 3
parts, a data part and two addresses, one points to the previous node and
one for the next node. It differs from the singly linked list as it has an extra
pointer called previous that points to the previous node, allowing the
traversal in both forward and backward directions.
In this article, we will learn about the doubly linked list implementation in C.
We will also look at the working of the doubly linked list and the basic
operations that can be performed using the doubly linked list in C.
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 1/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
To define a structure of a node in doubly linked list use the below format:
struct Node {
int data;
struct Node* next;
struct Node* prev;
}
We can then dynamically create the node using malloc() and assign the
values to the next, prev and data fields. It is generally preferred to create a
function that creates a node, assign the data field and return the pointer to
that node.
Insertion
Deletion
Traversal
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 2/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
To insert a new node at the beginning of the doubly linked list follow the
below approach:
Approach:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
HIDE AD
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 3/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
To insert a new node at the end of the doubly linked list, follow the below
approach:
Approach:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
HIDE AD
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 4/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
For inserting a node at a specific position in a doubly linked list follow the
below approach:
Approach:
Create a newNode.
Find the size of the list to ensure the position is within valid bounds.
If the position is 0 (or 1 depending on your indexing):
Use the insertion at the beginning approach.
If the position is equal to the size of the list:
Use the insertion at the end approach.
If the position is valid and not at the boundaries:
Traverse the list to find the node immediately before the
desired insertion point (prevNode).
Set newNode->next to prevNode->next.
Set newNode->prev to prevNode.
If prevNode->next is not NULL, set prevNode->next->prev
to newNode.
Set prevNode->next to newNode.
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 5/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
To delete a node at the beginning of the doubly linked list, follow the below
approach:
Approach:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
HIDE AD
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 6/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
To delete a node at the end of the doubly linked list, follow the below
approach:
Approach:
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy
HIDE AD
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 7/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
C C Basics C Data Types C Operators C Input and Output C Control Flow C Functions C Arrays CS
For deleting a node at a specific position in a doubly linked list, follow the
below approach:
Approach:
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 8/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
For traversing in a forward direction in a doubly linked list, follow the below
approach:
Approach:
Create a temporary pointer ptr and copy the head pointer into it.
Traverse through the list using a while loop.
Keep moving the value of temp pointer variable ptr to ptr-
>next until the last node whose next part contains null is found.
During each iteration of the loop, perform the desired operation.
For traversing in a reverse direction in a doubly linked list, follow the below
approach:
Create a temporary pointer ptr and copy the tail pointer into it.
Traverse through the list using a while loop.
Keep moving the value of temp pointer variable ptr to ptr-
>prev until the first node whose prev part contains null is found.
During each iteration of the loop, perform the desired operation .
Time Complexity: O(n), as here also we need to traverse through the whole
list containing n number of nodes.
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 9/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
// defining a node
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 10/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
}
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 11/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
void deleteAtPosition(Node** head, int position)
{
if (*head == NULL) {
printf("The list is already empty.\n");
return;
}
Node* temp = *head;
if (position == 1) {
deleteAtBeginning(head);
return;
}
for (int i = 1; temp != NULL && i < position; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position is greater than the number of "
"nodes.\n");
return;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
}
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 12/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
printf("After Insertions:\n");
printListForward(head);
printListReverse(head);
deleteAtBeginning(&head); // List: 15 10 20
deleteAtEnd(&head); // List: 15 10
deleteAtPosition(&head, 2); // List: 15
printf("After Deletions:\n");
printListForward(head);
return 0;
}
Output
After Insertions:
Forward List: 5 15 10 20
Reverse List: 20 10 15 5
After Deletions:
Forward List: 15
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 13/18
10/03/2025, 22:57 Doubly Linked List in C - GeeksforGeeks
The insertion and deletion code in a doubly linked list is more complex as
we need to maintain the previous links too.
For insertion and deletion at a specific position in a doubly linked list we
need to traverse from a head node to the specified node that can be time-
consuming in case of larger lists.
For each node we have to maintain two pointers in a doubly linked list
that leads to the overhead of maintaining and updating these pointers
during insertions and deletions.
A doubly linked list is a type of linked list in which each node contains
a pointer to the next node as well as the previous node in the
sequence.
The main advantage of a doubly linked list over a singly linked list is
We use cookies
that to
it ensure
can beyoutraversed
have the best browsing
in bothexperience
forwardonandour website.
backwardBy usingdirections. The
our site, you acknowledge that you have read and understood our
delete operation in DLL is more efficient if a pointer Cookie Policy & Privacy
to the node to be
Policy
HIDE AD
https://www.geeksforgeeks.org/doubly-linked-list-in-c/ 14/18