The code for a doubly linked list is much cleaner if you start with an empty list of two nodes as shown below.

That way you don't have to deal with special cases like if(head==NULL). There's always a node before and after the node that is being inserted (or deleted), so you just hook things up and you're done.

#include <stdio.h>
#include <stdlib.h>

typedef struct s_node Node;
struct s_node
{
    Node *prev;
    Node *next;
    int  data;
};

Node *insertAtBeginning( Node *head, int value )
{
    // allocate memory for the new node
    Node *node = malloc( sizeof(Node) );
    if ( node == NULL )
        return NULL;

    // insert the node at the beginning of the list
    Node *temp = head->next;
    head->next = node;
    temp->prev = node;

    // fill in the fields of the node
    node->prev = head;
    node->next = temp;
    node->data = value;

    return node;
}

void showList( Node *head )
{
    Node *node;

    printf( "The list contains: " );
    for ( node = head->next; node->next != NULL; node = node->next )
        printf( "%d--->", node->data );
    printf( "NULL\n" );
}

int main( void )
{
    // create an empty list with two nodes
    Node head = { NULL , NULL, 0 };
    Node tail = { &head, NULL, 0 };
    head.next = &tail;

    // insert more nodes
    for ( int i = 0; i < 10; i++ )
        insertAtBeginning( &head, i );

    // display the list
    showList( &head );
}
Answer from user3386109 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › doubly-linked-list-in-c
Doubly Linked List in C - GeeksforGeeks
July 23, 2025 - Time Complexity: O(n), as insertion is done in between, so we need to traverse through the list until we reach the desired position. Space Complexity: O(1) Just like insertion, we have three scenarios while deleting a node in a doubly linked list:
🌐
Medium
medium.com › @Dev_Frank › c-linked-list-doubly-linked-list-561851cd732a
C-LINKED LIST (Doubly Linked List) | by Dev Frank | Medium
June 30, 2024 - You go from A to B, B to C, and C to D. And if you want to go back from D to A: You go from D to C, C to B, and B to A. A doubly linked list, resembling a line of friends holding hands, is a useful tool in programming for moving in both directions, though it’s more complex and occupies more space.
Discussions

Doubly Linked List Implementation
Your program is not printing anything because it's segfaulting, meaning you're trying to de-reference a NULL pointer. Print() is not even executed. The program crashes before that. This happens as early as the call to Insert(&head, 3, 2). In line 58 you are checking if temp2->next == NULL. At this point, there is only one node (containing 2) in the list, so temp1 is pointing to it. temp2 points to temp1->next which is NULL so you have to also check if temp2 == NULL before trying to access it. Edit: Actually it's not "also check". The edge case you have to consider is just that temp2 == NULL. You have to rewrite your pointer manipulations a bit (basically appending newNode to temp2 instead of temp2->next) but then everything should work. Another edit: You should also check if temp1 becomes NULL during your for loop on line 52. Otherwise your program will crash if you pass an index that is too big. More on reddit.com
🌐 r/C_Programming
5
10
July 1, 2021
Delete nodes from doubly linked list
You assign *head = remove->next, right BEFORE you check if head == NULL. Might want to check head first... That’s the first thing that I saw. Take a piece of paper and pencil and draw all the various scenarios, and you’ll figure out anything else. More on reddit.com
🌐 r/C_Programming
4
0
January 22, 2019
[C] sorting doubly linked list

A couple things jump out at me at a glance. The big one:

for(int=0;i<10;i++){

You probably meant to make int=0 into int i=0. When you say you can't get it to work, do you mean that it won't build? Because that's probably the issue, then.

Also, it might help to clean the code a bit - in the if clause, you might want to check the logical opposite of what you're checking and just not use the else. This won't change whether or not the code works, but it'll make it easier for you to read.

One last thing: This won't fully sort the list. This will do the first round of a sorting algorithm for a list of up to 10 items.

More on reddit.com
🌐 r/learnprogramming
11
8
February 10, 2013
Doubly linked lists in C. ELI5.

Do you understand a singly linked list?

More on reddit.com
🌐 r/learnprogramming
16
0
February 5, 2013
Top answer
1 of 4
4

The code for a doubly linked list is much cleaner if you start with an empty list of two nodes as shown below.

That way you don't have to deal with special cases like if(head==NULL). There's always a node before and after the node that is being inserted (or deleted), so you just hook things up and you're done.

#include <stdio.h>
#include <stdlib.h>

typedef struct s_node Node;
struct s_node
{
    Node *prev;
    Node *next;
    int  data;
};

Node *insertAtBeginning( Node *head, int value )
{
    // allocate memory for the new node
    Node *node = malloc( sizeof(Node) );
    if ( node == NULL )
        return NULL;

    // insert the node at the beginning of the list
    Node *temp = head->next;
    head->next = node;
    temp->prev = node;

    // fill in the fields of the node
    node->prev = head;
    node->next = temp;
    node->data = value;

    return node;
}

void showList( Node *head )
{
    Node *node;

    printf( "The list contains: " );
    for ( node = head->next; node->next != NULL; node = node->next )
        printf( "%d--->", node->data );
    printf( "NULL\n" );
}

int main( void )
{
    // create an empty list with two nodes
    Node head = { NULL , NULL, 0 };
    Node tail = { &head, NULL, 0 };
    head.next = &tail;

    // insert more nodes
    for ( int i = 0; i < 10; i++ )
        insertAtBeginning( &head, i );

    // display the list
    showList( &head );
}
2 of 4
2

There are mainly two problems here :

  1. free(first) : This is not required as you wish to save the memory you just allocated, not delete it.

  2. Your insertAtBeginning() function returns a pointer to head, so in main(), where you are calling this function change it to head1=insertAtBeginning(i, head1); This way your head is also saved.

Here's the code with the two edits :

http://ideone.com/nXwc8z

🌐
TutorialsPoint
tutorialspoint.com › data_structures_algorithms › doubly_linked_list_program_in_c.htm
Doubly Linked List Program in C
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> struct node { int data; int key; struct node *next; struct node *prev; }; //this link always point to first Link struct node *head = NULL; //this link always point to last Link struct node *last = NULL; struct node *current = NULL; //is list empty bool isEmpty(){ return head == NULL; } //display the list in from first to last void displayForward(){ //start from the beginning struct node *ptr = head; //navigate till the end of the list printf("\n[ "); while(ptr != NULL) { printf("(%d,%d) ",ptr->key,ptr->data); ptr =
🌐
Codecademy
codecademy.com › article › doubly-linked-list
Doubly Linked List: Complete Guide & Implementation | Codecademy
When inserting at the beginning of a doubly linked list, we first need to check if there is a current head to the list. If there isn’t, then the list is empty, and we can simply make our new node both the head and tail of the list and set both pointers to null.
🌐
Scaler
scaler.com › home › topics › doubly linked list in c
Doubly Linked List in C - Scaler Topics
October 26, 2023 - A special node pointer head is used to denote the start of a doubly linked list. The previous pointer in the head node and the next pointer in the last node of a doubly linked list points to a sentinel value or a terminator. In the C programming language, this sentinal value is the null pointer.
🌐
UTK
web.eecs.utk.edu › ~bvanderz › teaching › cs140Fa10 › notes › Dllists
CS140 Lecture notes -- Doubly Linked Lists
The API for doubly linked lists is in dllist.h. Like singly lists, it defines two structs: a container struct called Dllist that holds administrative information about the list, and a node struct called Dllist_Node that contains the information for a single node in the list.
Find elsewhere
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
GitHub
gist.github.com › mycodeschool › 7429492
Doubly Linked List implementation in C · GitHub
Again, hope you don't mind me pointing this out. I only wrote this for some poor undergrad or amateur programmer who happened upon this source code. I agree. Well observed !! ... This data structure is not a doubly linked list since it does not provide O(1) when inserting node at tail.
🌐
Reddit
reddit.com › r/c_programming › doubly linked list implementation
r/C_Programming on Reddit: Doubly Linked List Implementation
July 1, 2021 -

Hey guys, I tried to implement Doubly Linked List in C programming, I implemented Inserting a node at any given position (using head as a local variable and not making it global), the main issue I'm Facing is , the program is just executing and not printing output, I sat for 2 hrs and tried to figure out the error, but still not able to find, I would be grateful, if you guys could help me out

#include<stdio.h>
#include<stdlib.h>

// Doubly Linked List Implementation
struct Node{
	int data;
	struct Node* next;
	struct Node* prev;
};

int main(){
	struct Node* head = NULL;

	Insert(&head,2,1);
	Insert(&head,3,2);
	Insert(&head,4,1);
	Insert(&head,5,2);
	Insert(&head,6,5);

	Print(head);// it's just getting executed, and not showing any output 
	return 0;
}


//Function for creating a Node
struct Node* GetNewNode(int x){
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = x;
	newNode->prev = NULL;
	newNode->next = NULL;
	return newNode;
}

//Inserting a node in nth position
void Insert(struct Node** head, int x, int n){
	struct Node* newNode = GetNewNode(x);
	if(n==1){
		if(*head==NULL){
			*head = newNode;
			return;
		}
		else{
			newNode->next = *head;
		    (*head)->prev = newNode;
		    *head = newNode;
		    return;
		}
		
	}
	struct Node* temp1;
	temp1 = *head;
	for(int i=1;i<n-1;i++){
		temp1= temp1->next;
	}
	//temp1 is pointing to (n-1)th node
	struct Node* temp2 = temp1->next;
	//temp2 is pointing to nth node
	if(temp2->next == NULL){
		temp2->next = newNode;
		newNode->prev = temp2;
		return;
	}
	
	temp1->next = newNode;
	newNode->prev = temp1;
	newNode->next = temp2;
	temp2->prev = newNode;
}

// Function for Printing the whole linked list
void Print(struct Node* node){
	while(node!=NULL){
		printf("%d ",node->data);
		node = node->next;
	}
}
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-create-a-doubly-linked-list-in-c
How to Create a Doubly Linked List in C? - GeeksforGeeks
July 23, 2025 - In C, we can represent a doubly linked list node using the struct type, which allows grouping multiple data types in a single variable. Each node in a doubly linked list should have pointers to both the next node and the previous node along ...
Starred by 466 users
Forked by 144 users
Languages: C 89.6% | Makefile 10.4%
🌐
Programiz
programiz.com › dsa › doubly-linked-list
Doubly Linked List (With code)
Note: In the case of the head node, prev points to null, and in the case of the tail pointer, next points to null. Here, one is a head node and three is a tail node. Pushing a node to a doubly-linked list is similar to pushing a node to a linked list, but extra work is required to handle the pointer to the previous node.
🌐
w3resource
w3resource.com › c-programming-exercises › linked_list › c-linked_list-exercise-11.php
C Program: Create and display a doubly linked list - w3resource
October 16, 2025 - #include <stdio.h> #include <stdlib.h> // Structure for a doubly linked list node struct node { int num; struct node *preptr; struct node *nextptr; } *stnode, *ennode; // Function prototypes void DlListcreation(int n); void displayDlList(); int main() { int n; stnode = NULL; ennode = NULL; printf("\n\n Doubly Linked List: Create and display a doubly linked list:\n"); printf("-------------------------------------------------------------------\n"); printf(" Input the number of nodes : "); scanf("%d", &n); DlListcreation(n); // Create a doubly linked list displayDlList(); // Display the created d
🌐
Dot Net Tutorials
dotnettutorials.net › home › doubly linked list in c
Doubly Linked List in C - Dot Net Tutorials
January 3, 2023 - In this article, we will discuss Doubly Linked List in C Language. In doubly linked list, a node is having a pointer to the next node
🌐
NxtWave
ccbp.in › blog › articles › doubly-linked-list-program-in-c
Doubly Linked List Program in C, Comprehensive Guide
December 19, 2024 - A doubly linked list program in C is a data structure where each node links to both the previous node and the next node. Each node contains three parts: a data part and two addresses.
🌐
Equestionanswers
equestionanswers.com › c › c-doubly-linked-list.php
What is Doubly Linked list? Implement Doubly Linked list with example.
Nodes in a doubly linked list are linked in both directions using next and previous pointer. Two consecutive elements are linked by previous and next pointer. Thus it is possible to traverse any direction from head to tail or from tail to head. The limitation of single linked list thus has ...
🌐
LabEx
labex.io › tutorials › c-implementing-doubly-linked-list-in-c-301499
Implement Doubly Linked List in C | Programming Tutorial | LabEx
Locate insert_after (list, NULL, data); and change to the following code: ... In this step, you will learn how to insert a new node before a given node in the doubly linked list.
🌐
C# Corner
c-sharpcorner.com › home › technologies › c, c++, mfc › the implementation of double linked list with c programming
The implementation of Double Linked List with C Programming
September 26, 2024 - The basic operations in a doubly linked list include inserting nodes at the beginning, end, or a specified position, deleting nodes by updating the adjacent node pointers and traversing the list in either direction. The structure is commonly defined using struct in C, and memory for new nodes is dynamically allocated using malloc() and freed using free() when a node is deleted.
🌐
Intellipaat
intellipaat.com › home › blog › doubly linked list in c – a comprehensive tutorial
Doubly Linked List in C with Examples | Intellipaat
July 30, 2025 - In a doubly linked list, the first node is connected to a NULL reference in the previous pointer because there is no node before it. And the final node is connected to a NULL reference in the next pointer because there is no node after it.