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 OverflowDoubly Linked List Implementation
Delete nodes from doubly linked list
[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.comDoubly linked lists in C. ELI5.
Do you understand a singly linked list?
More on reddit.comThe 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 );
}
There are mainly two problems here :
free(first): This is not required as you wish to save the memory you just allocated, not delete it.Your
insertAtBeginning()function returns a pointer tohead, so inmain(), where you are calling this function change it tohead1=insertAtBeginning(i, head1);This way your head is also saved.
Here's the code with the two edits :
http://ideone.com/nXwc8z
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;
}
}