linked_list/linked_list1.cpp
// insert in the sorted linked list
insert in the sorted linked list
insert in the sorted linked list
void insert_in_sorted(struct Node * p, int data)
{
    Node *t, *q = NULL;
    t = new Node;
    t->data = data;
    t->next = NULL;
    if (first == NULL)
    {
        first = t;
    }
    else
    {
        while (p && p->data < data)
        {
            q=p;
            p=p->next;
        }
        if(p == first)
        {
            t->next = first;
            first = t;
        }
        else
        {
            t->next=p->next;
            q->next=t;
        }
    }
}
void insert_in_sorted ( struct Node * p , int data ) { Node * t , * q = NULL ; t = new Node ; t -> data = data ; t -> next = NULL ; if ( first == NULL ) { first = t ; } else { while ( p && p -> data < data ) { q = p ; p = p -> next ; } if ( p == first ) { t -> next = first ; first = t ; } else { t -> next = p -> next ; q -> next = t ; } } }
insert in the sorted linked list
insert in the sorted linked list
// init two pointer // create node to be inserted // store data in new node's data // new_node's next to null // if linked list is empty // make new node as first // check if p is not null // q points to p // move p forward // if p is the head node // new_node points to head_node // make new_node as first // new_node's next points to present node's next // q points to new_node which is added