Linked listinsertion at a position

 SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data, int position) {

    SinglyLinkedListNode *temp,*ptr,*prev_node;
    temp=(SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode));
    temp->data=data;
    temp->next=NULL;
    int pos=0;
    ptr=head;
    while (ptr !=NULL) {
        
        if(pos==position) 
        {
            break;

        }
        prev_node=ptr;
        ptr=ptr->next;
        pos++;
    }
    prev_node->next=temp;
    temp->next=ptr;
    return head;
}

Comments

Popular posts from this blog

Blogging Website using Python-flask,MySql,Bootstrap 4

calculator in java with actionListener and KeyListener