From my observation,
- Your code fails when
index = size/2
.
When there are two elements(size == 2) and when you try to insert at position 1, then current->prev->next = n;
is meaningless
Do one of these changes else if (index <= size/2)
or else if (index >= size/2)