Insert a node in Doubly linked list - GeeksforGeeks
/*
http://www.practice.geeksforgeeks.org/problem-page.php?pid=700232
http://quiz.geeksforgeeks.org/doubly-linked-list/
*/
/* a node of the doubly linked list
struct node
{
int data;
struct node *next;
struct node *prev;
}; */
/* Function to reverse a Doubly Linked List */
void addNode(struct node **head_ref,int pos,int data)
{
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if(*head_ref == NULL){
*head_ref = newNode;
return;
}
struct node* current = *head_ref;
struct node* nextNode = *head_ref;
while(pos > 0){
current = current->next;
pos--;
}
nextNode = current->next;
newNode->next = nextNode;
newNode->prev = current;
current->next = newNode;
if(nextNode != NULL)
nextNode->prev = newNode;
}