Doubly-linked-list.cpp
// function to insert a new node in sorted way in 
// a sorted doubly linked list 
function to insert a new node in sorted way in
a sorted doubly linked list
function to insert a new node in sorted way in a sorted doubly linked list
void sortedInsert(struct Node** head_ref, struct Node* newNode) 
{ 
    struct Node* current; 
    if (*head_ref == NULL) 
        *head_ref = newNode; 
    else if ((*head_ref)->data >= newNode->data) { 
        newNode->next = *head_ref; 
        newNode->next->prev = newNode; 
        *head_ref = newNode; 
    } 
    else { 
        current = *head_ref; 
        while (current->next != NULL &&  
               current->next->data < newNode->data) 
            current = current->next; 
        newNode->next = current->next; 
        if (current->next != NULL) 
            newNode->next->prev = newNode; 
        current->next = newNode; 
        newNode->prev = current; 
    } 
}
void sortedInsert ( struct Node * * head_ref , struct Node * newNode ) { struct Node * current ; if ( * head_ref == NULL ) * head_ref = newNode ; else if ( ( * head_ref ) -> data >= newNode -> data ) { newNode -> next = * head_ref ; newNode -> next -> prev = newNode ; * head_ref = newNode ; } else { current = * head_ref ; while ( current -> next != NULL && current -> next -> data < newNode -> data ) current = current -> next ; newNode -> next = current -> next ; if ( current -> next != NULL ) newNode -> next -> prev = newNode ; current -> next = newNode ; newNode -> prev = current ; } }
function to insert a new node in sorted way in
a sorted doubly linked list
function to insert a new node in sorted way in a sorted doubly linked list
// if list is empty  // if the node is to be inserted at the beginning  // of the doubly linked list  // locate the node after which the new node  // is to be inserted  /* Make the appropriate links */ // if the new node is not inserted  // at the end of the list 