linked_list/linked_list1.cpp
// combine function to insert at any given position including
// before the first node
combine function to insert at any given position including
before the first node
combine function to insert at any given position including before the first node
void insert_at_pos(struct Node *p, int index, int data)
{
    if (index<0 || index > count_nodes(p))
    {
        return;
    }
    struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
    new_node->data = data;
    if (index == 0)
    {
        new_node->next = first;
        first = new_node;
    }
    else
    {
        for (int i=0; i<index-1; i++)
        {
            p =p->next;
        }
        new_node->next = p->next;
        p->next = new_node;
    }
}
void insert_at_pos ( struct Node * p , int index , int data ) { if ( index < 0 || index > count_nodes ( p ) ) { return ; } struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = data ; if ( index == 0 ) { new_node -> next = first ; first = new_node ; } else { for ( int i = 0 ; i < index - 1 ; i ++ ) { p = p -> next ; } new_node -> next = p -> next ; p -> next = new_node ; } }
combine function to insert at any given position including
before the first node
combine function to insert at any given position including before the first node
//struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); // index validation // create new node // store data to new_node's data section // points to frist node // make it first node // move p to the node just before the position // move p to next // make new node point to p